Skip to content

Commit 02b3f63

Browse files
authored
Merge pull request #2400 from sharwell/case-pattern-switch
Fix handling of colon after case in new pattern-matching switch statement
2 parents fa86f85 + 5449941 commit 02b3f63

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1024CSharp7UnitTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,52 @@ public void TestMethod()
4949
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5050
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5151
}
52+
53+
[Fact]
54+
public async Task TestSpacingAroundColonInCasePatternSwitchLabelAsync()
55+
{
56+
const string testCode = @"
57+
public class Foo
58+
{
59+
public void TestMethod()
60+
{
61+
switch (new object())
62+
{
63+
case int a when (a > 0):
64+
case short b when (b > 0) :
65+
case int x:
66+
case short y :
67+
default:
68+
break;
69+
}
70+
}
71+
}";
72+
const string fixedCode = @"
73+
public class Foo
74+
{
75+
public void TestMethod()
76+
{
77+
switch (new object())
78+
{
79+
case int a when (a > 0):
80+
case short b when (b > 0):
81+
case int x:
82+
case short y:
83+
default:
84+
break;
85+
}
86+
}
87+
}";
88+
89+
DiagnosticResult[] expected =
90+
{
91+
this.CSharpDiagnostic().WithLocation(9, 35).WithArguments(" not", "preceded", string.Empty),
92+
this.CSharpDiagnostic().WithLocation(11, 22).WithArguments(" not", "preceded", string.Empty),
93+
};
94+
95+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
96+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
97+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
98+
}
5299
}
53100
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1024ColonsMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace StyleCop.Analyzers.SpacingRules
55
{
66
using System;
77
using System.Collections.Immutable;
8-
using System.Linq;
98
using Microsoft.CodeAnalysis;
109
using Microsoft.CodeAnalysis.CSharp;
1110
using Microsoft.CodeAnalysis.Diagnostics;
1211
using StyleCop.Analyzers.Helpers;
12+
using StyleCop.Analyzers.Lightup;
1313

1414
/// <summary>
1515
/// A colon within a C# element is not spaced correctly.
@@ -112,6 +112,7 @@ private static void HandleColonToken(SyntaxTreeAnalysisContext context, SyntaxTo
112112
case SyntaxKind.LabeledStatement:
113113
case SyntaxKind.CaseSwitchLabel:
114114
case SyntaxKind.DefaultSwitchLabel:
115+
case SyntaxKindEx.CasePatternSwitchLabel:
115116
// NameColon is not explicitly listed in the description of this warning, but the behavior is inferred
116117
case SyntaxKind.NameColon:
117118
requireBefore = false;

0 commit comments

Comments
 (0)