Skip to content

Commit

Permalink
Update SA1011 to forbid trailing space before the end of a switch case
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Jun 24, 2023
1 parent 14245e2 commit e098ddc
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 e098ddc

Please sign in to comment.