Skip to content

Commit

Permalink
Update SA1000 to handle checked operator declarations correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed Jul 22, 2022
1 parent 6478404 commit a2f73ff
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@

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()
{
// NOTE: A checked operator requires a non-checked operator as well
// NOTE: Implicit conversion operators can not be checked
var testCode = @"
public class MyClass
{
public static MyClass operator {|#0:checked|}-(MyClass x) => x;
public static MyClass operator -(MyClass x) => x;
public static explicit operator {|#1:checked|}@MyClass(int i) => new MyClass();
public static explicit operator MyClass(int i) => new MyClass();
}";

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

var expected = new[]
{
Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(0),
Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(1),
};
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,23 @@ 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:
case SyntaxKind.ConversionOperatorDeclaration:
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 a2f73ff

Please sign in to comment.