Skip to content

Commit

Permalink
Merge pull request #3674 from bjornhellander/feature/sa1011-case-list…
Browse files Browse the repository at this point in the history
…-pattern

Update SA1011 to forbid trailing space before the end of a switch case
  • Loading branch information
sharwell authored Jun 26, 2023
2 parents 14245e2 + e098ddc commit 0f30141
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,55 @@

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1011ClosingSquareBracketsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public class SA1011CSharp11UnitTests : SA1011CSharp10UnitTests
{
[Fact]
[WorkItem(3673, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3673")]
public async Task TestListPatternInSwitchCaseAsync()
{
var testCode = @"public class TestClass
{
public void TestMethod(object[] arg)
{
switch (arg)
{
case [string s{|#0:]|} :
break;
}
}}
";

var fixedCode = @"public class TestClass
{
public void TestMethod(object[] arg)
{
switch (arg)
{
case [string s]:
break;
}
}}
";

await new CSharpTest
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net70,
TestCode = testCode,
ExpectedDiagnostics =
{
Diagnostic().WithLocation(0).WithArguments(" not", "followed"),
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
break;

case SyntaxKind.ColonToken:
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.InterpolationFormatClause);
precedesSpecialCharacter =
nextToken.Parent.IsKind(SyntaxKind.InterpolationFormatClause) ||
nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel);
suppressFollowingSpaceError = false;
break;

Expand Down

0 comments on commit 0f30141

Please sign in to comment.