Skip to content

Commit 2d6dd5e

Browse files
Update SA1000 to handle checked operator declarations correctly
#3478
1 parent e36f105 commit 2d6dd5e

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

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

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

44
namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
9+
using Xunit;
10+
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly,
13+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
714

815
public class SA1000CSharp11UnitTests : SA1000CSharp10UnitTests
916
{
17+
[Fact]
18+
public async Task TestCheckedOperatorDeclarationAsync()
19+
{
20+
// NOTE: A checked operator requires a non-checked operator as well
21+
// NOTE: Implicit conversion operators can not be checked
22+
var testCode = @"
23+
public class MyClass
24+
{
25+
public static MyClass operator {|#0:checked|}-(MyClass x) => x;
26+
public static MyClass operator -(MyClass x) => x;
27+
28+
public static explicit operator {|#1:checked|}@MyClass(int i) => new MyClass();
29+
public static explicit operator MyClass(int i) => new MyClass();
30+
}";
31+
32+
var fixedCode = @"
33+
public class MyClass
34+
{
35+
public static MyClass operator checked -(MyClass x) => x;
36+
public static MyClass operator -(MyClass x) => x;
37+
38+
public static explicit operator checked @MyClass(int i) => new MyClass();
39+
public static explicit operator MyClass(int i) => new MyClass();
40+
}";
41+
42+
var expected = new[]
43+
{
44+
Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(0),
45+
Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(1),
46+
};
47+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
48+
}
1049
}
1150
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,23 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
119119

120120
case SyntaxKind.CheckedKeyword:
121121
case SyntaxKind.UncheckedKeyword:
122-
if (token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken))
122+
switch (token.Parent.Kind())
123123
{
124+
case SyntaxKind.CheckedStatement:
125+
case SyntaxKind.UncheckedStatement:
126+
case SyntaxKind.OperatorDeclaration:
127+
case SyntaxKind.ConversionOperatorDeclaration:
124128
HandleRequiredSpaceToken(ref context, token);
125-
}
126-
else
127-
{
129+
break;
130+
131+
case SyntaxKind.CheckedExpression:
132+
case SyntaxKind.UncheckedExpression:
128133
HandleDisallowedSpaceToken(ref context, token);
134+
break;
135+
136+
default:
137+
// So far an unknown case, so we have no opinion yet
138+
break;
129139
}
130140

131141
break;

0 commit comments

Comments
 (0)