Skip to content

Commit 045aba1

Browse files
authored
Merge pull request #3823 from bjornhellander/feature/sa1003-null-coalescing-assignment-operator-3822
Update SA1003 to handle the null-coalescing assignment operator, the unsigned right-shift operator, the unsigned right-shift assignment operator and the null-forgiving operator
2 parents 2081d85 + 9a680bd commit 045aba1

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,92 @@
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.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>;
715

816
public partial class SA1003CSharp11UnitTests : SA1003CSharp10UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
20+
public async Task TestUnsignedRightShiftAssignmentOperatorAsync()
21+
{
22+
var testCode = @"
23+
namespace TestNamespace
24+
{
25+
public class TestClass
26+
{
27+
public void TestMethod(int x)
28+
{
29+
x{|#0:>>>=|}0;
30+
}
31+
}
32+
}
33+
";
34+
35+
var fixedCode = @"
36+
namespace TestNamespace
37+
{
38+
public class TestClass
39+
{
40+
public void TestMethod(int x)
41+
{
42+
x >>>= 0;
43+
}
44+
}
45+
}
46+
";
47+
48+
var expected = new[]
49+
{
50+
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments(">>>="),
51+
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments(">>>="),
52+
};
53+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
54+
}
55+
56+
[Fact]
57+
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
58+
public async Task TestUnsignedRightShiftOperatorAsync()
59+
{
60+
var testCode = @"
61+
namespace TestNamespace
62+
{
63+
public class TestClass
64+
{
65+
public int TestMethod(int x)
66+
{
67+
return x{|#0:>>>|}0;
68+
}
69+
}
70+
}
71+
";
72+
73+
var fixedCode = @"
74+
namespace TestNamespace
75+
{
76+
public class TestClass
77+
{
78+
public int TestMethod(int x)
79+
{
80+
return x >>> 0;
81+
}
82+
}
83+
}
84+
";
85+
86+
var expected = new[]
87+
{
88+
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments(">>>"),
89+
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments(">>>"),
90+
};
91+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
92+
}
1093
}
1194
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1003CSharp8UnitTests.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,81 @@ public void TestMethod()
6666
FixedCode = fixedCode,
6767
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
6868
}
69+
70+
[Fact]
71+
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
72+
public async Task TestNullCoalescingAssignmentOperatorAsync()
73+
{
74+
var testCode = @"
75+
namespace TestNamespace
76+
{
77+
public class TestClass
78+
{
79+
public void TestMethod(int? x)
80+
{
81+
x{|#0:??=|}0;
82+
}
83+
}
84+
}
85+
";
86+
87+
var fixedCode = @"
88+
namespace TestNamespace
89+
{
90+
public class TestClass
91+
{
92+
public void TestMethod(int? x)
93+
{
94+
x ??= 0;
95+
}
96+
}
97+
}
98+
";
99+
100+
var expected = new[]
101+
{
102+
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments("??="),
103+
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments("??="),
104+
};
105+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
106+
}
107+
108+
[Fact]
109+
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
110+
public async Task TestNullForgivingOperatorAsync()
111+
{
112+
var testCode = @"
113+
namespace TestNamespace
114+
{
115+
public class TestClass
116+
{
117+
public string TestMethod(string? x)
118+
{
119+
return x {|#0:!|} ;
120+
}
121+
}
122+
}
123+
";
124+
125+
var fixedCode = @"
126+
namespace TestNamespace
127+
{
128+
public class TestClass
129+
{
130+
public string TestMethod(string? x)
131+
{
132+
return x!;
133+
}
134+
}
135+
}
136+
";
137+
138+
var expected = new[]
139+
{
140+
Diagnostic(DescriptorNotPrecededByWhitespace).WithLocation(0).WithArguments("!"),
141+
Diagnostic(DescriptorNotFollowedByWhitespace).WithLocation(0).WithArguments("!"),
142+
};
143+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
144+
}
69145
}
70146
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ internal static class SyntaxKindEx
2727
public const SyntaxKind IsPatternExpression = (SyntaxKind)8657;
2828
public const SyntaxKind RangeExpression = (SyntaxKind)8658;
2929
public const SyntaxKind ImplicitObjectCreationExpression = (SyntaxKind)8659;
30+
public const SyntaxKind UnsignedRightShiftExpression = (SyntaxKind)8692;
3031
public const SyntaxKind CoalesceAssignmentExpression = (SyntaxKind)8725;
32+
public const SyntaxKind UnsignedRightShiftAssignmentExpression = (SyntaxKind)8726;
3133
public const SyntaxKind IndexExpression = (SyntaxKind)8741;
3234
public const SyntaxKind DefaultLiteralExpression = (SyntaxKind)8755;
3335
public const SyntaxKind LocalFunctionStatement = (SyntaxKind)8830;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1003SymbolsMustBeSpacedCorrectly.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
8686
SyntaxKind.GreaterThanOrEqualExpression,
8787
SyntaxKind.LeftShiftExpression,
8888
SyntaxKind.RightShiftExpression,
89+
SyntaxKindEx.UnsignedRightShiftExpression,
8990
SyntaxKind.AddExpression,
9091
SyntaxKind.SubtractExpression,
9192
SyntaxKind.MultiplyExpression,
@@ -105,7 +106,10 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
105106
SyntaxKind.AddressOfExpression);
106107

107108
private static readonly ImmutableArray<SyntaxKind> PostfixUnaryExpressionKinds =
108-
ImmutableArray.Create(SyntaxKind.PostIncrementExpression, SyntaxKind.PostDecrementExpression);
109+
ImmutableArray.Create(
110+
SyntaxKind.PostIncrementExpression,
111+
SyntaxKind.PostDecrementExpression,
112+
SyntaxKindEx.SuppressNullableWarningExpression);
109113

110114
private static readonly ImmutableArray<SyntaxKind> AssignmentExpressionKinds =
111115
ImmutableArray.Create(
@@ -114,11 +118,13 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
114118
SyntaxKind.ExclusiveOrAssignmentExpression,
115119
SyntaxKind.LeftShiftAssignmentExpression,
116120
SyntaxKind.RightShiftAssignmentExpression,
121+
SyntaxKindEx.UnsignedRightShiftAssignmentExpression,
117122
SyntaxKind.AddAssignmentExpression,
118123
SyntaxKind.SubtractAssignmentExpression,
119124
SyntaxKind.MultiplyAssignmentExpression,
120125
SyntaxKind.DivideAssignmentExpression,
121126
SyntaxKind.ModuloAssignmentExpression,
127+
SyntaxKindEx.CoalesceAssignmentExpression,
122128
SyntaxKind.SimpleAssignmentExpression);
123129

124130
private static readonly Action<SyntaxNodeAnalysisContext> ConstructorDeclarationAction = HandleConstructorDeclaration;

0 commit comments

Comments
 (0)