Skip to content

Commit aa8fb8c

Browse files
Merge a2f73ff into 6478404
2 parents 6478404 + a2f73ff commit aa8fb8c

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
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.Test.CSharp11/StyleCop.Analyzers.Test.CSharp11.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0-4.final" />
20+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.0-1.final" />
2121
<PackageReference Include="xunit" Version="2.4.1" />
2222
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" PrivateAssets="all" />
2323
</ItemGroup>

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

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

116116
case SyntaxKind.CheckedKeyword:
117117
case SyntaxKind.UncheckedKeyword:
118-
if (token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken))
118+
switch (token.Parent.Kind())
119119
{
120+
case SyntaxKind.CheckedStatement:
121+
case SyntaxKind.UncheckedStatement:
122+
case SyntaxKind.OperatorDeclaration:
123+
case SyntaxKind.ConversionOperatorDeclaration:
120124
HandleRequiredSpaceToken(ref context, token);
121-
}
122-
else
123-
{
125+
break;
126+
127+
case SyntaxKind.CheckedExpression:
128+
case SyntaxKind.UncheckedExpression:
124129
HandleDisallowedSpaceToken(ref context, token);
130+
break;
131+
132+
default:
133+
// So far an unknown case, so we have no opinion yet
134+
break;
125135
}
126136

127137
break;

0 commit comments

Comments
 (0)