Skip to content

Commit e098ddc

Browse files
Update SA1011 to forbid trailing space before the end of a switch case
#3673
1 parent 14245e2 commit e098ddc

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1011CSharp11UnitTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,55 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.SpacingRules.SA1011ClosingSquareBracketsMustBeSpacedCorrectly,
13+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
714

815
public class SA1011CSharp11UnitTests : SA1011CSharp10UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3673, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3673")]
19+
public async Task TestListPatternInSwitchCaseAsync()
20+
{
21+
var testCode = @"public class TestClass
22+
{
23+
public void TestMethod(object[] arg)
24+
{
25+
switch (arg)
26+
{
27+
case [string s{|#0:]|} :
28+
break;
29+
}
30+
}}
31+
";
32+
33+
var fixedCode = @"public class TestClass
34+
{
35+
public void TestMethod(object[] arg)
36+
{
37+
switch (arg)
38+
{
39+
case [string s]:
40+
break;
41+
}
42+
}}
43+
";
44+
45+
await new CSharpTest
46+
{
47+
ReferenceAssemblies = ReferenceAssemblies.Net.Net70,
48+
TestCode = testCode,
49+
ExpectedDiagnostics =
50+
{
51+
Diagnostic().WithLocation(0).WithArguments(" not", "followed"),
52+
},
53+
FixedCode = fixedCode,
54+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
55+
}
1056
}
1157
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
135135
break;
136136

137137
case SyntaxKind.ColonToken:
138-
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.InterpolationFormatClause);
138+
precedesSpecialCharacter =
139+
nextToken.Parent.IsKind(SyntaxKind.InterpolationFormatClause) ||
140+
nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel);
139141
suppressFollowingSpaceError = false;
140142
break;
141143

0 commit comments

Comments
 (0)