Skip to content

Commit

Permalink
Updated SA1000 to handle checked operator declarations correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed May 13, 2022
1 parent 7476a19 commit 9288008
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using Xunit;

using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public class SA1000CSharp11UnitTests : SA1000CSharp10UnitTests
{
[Fact]
public async Task TestCheckedOperatorDeclarationAsync()
{
var testCode = @"
public class MyClass
{
public static MyClass operator {|#0:checked|}-(MyClass x) => x;
public static MyClass operator -(MyClass x) => x;
}";

var fixedCode = @"
public class MyClass
{
public static MyClass operator checked -(MyClass x) => x;
public static MyClass operator -(MyClass x) => x;
}";

var expected = Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0-4.final" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0-1.final" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,22 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)

case SyntaxKind.CheckedKeyword:
case SyntaxKind.UncheckedKeyword:
if (token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken))
switch (token.Parent.Kind())
{
case SyntaxKind.CheckedStatement:
case SyntaxKind.UncheckedStatement:
case SyntaxKind.OperatorDeclaration:
HandleRequiredSpaceToken(ref context, token);
}
else
{
break;

case SyntaxKind.CheckedExpression:
case SyntaxKind.UncheckedExpression:
HandleDisallowedSpaceToken(ref context, token);
break;

default:
// So far an unknown case, so we have no opinion yet
break;
}

break;
Expand Down

0 comments on commit 9288008

Please sign in to comment.