Skip to content

Commit 31030c7

Browse files
authored
Merge pull request #3071 from pantosha/fix-3052
Make SA1011 work with exclamation (`!`)
2 parents 620e3df + 0a930b5 commit 31030c7

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

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

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

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1002SemicolonsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public class SA1002CSharp8UnitTests : SA1002CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
20+
public async Task TestClosingSquareBracketFollowedByExclamationAsync()
21+
{
22+
var testCode = @"namespace TestNamespace
23+
{
24+
public class TestClass
25+
{
26+
public void TestMethod(object?[] arguments)
27+
{
28+
object o1 = arguments[0] !;
29+
object o2 = arguments[0]! ;
30+
object o3 = arguments[0] ! ;
31+
}
32+
}
33+
}
34+
";
35+
36+
var fixedCode = @"namespace TestNamespace
37+
{
38+
public class TestClass
39+
{
40+
public void TestMethod(object?[] arguments)
41+
{
42+
object o1 = arguments[0] !;
43+
object o2 = arguments[0]!;
44+
object o3 = arguments[0] !;
45+
}
46+
}
47+
}
48+
";
49+
50+
DiagnosticResult[] expected =
51+
{
52+
Diagnostic().WithArguments(" not", "preceded").WithLocation(8, 39),
53+
Diagnostic().WithArguments(" not", "preceded").WithLocation(9, 40),
54+
};
55+
56+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
57+
}
1058
}
1159
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,54 @@ public class TestClass
5656

5757
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5858
}
59+
60+
[Fact]
61+
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
62+
public async Task TestClosingSquareBracketFollowedByExclamationAsync()
63+
{
64+
var testCode = @"namespace TestNamespace
65+
{
66+
public class TestClass
67+
{
68+
public void TestMethod(object?[] arguments)
69+
{
70+
object o1 = arguments[0] !;
71+
object o2 = arguments[0]! ;
72+
object o3 = arguments[0] ! ;
73+
string s1 = arguments[0] !.ToString();
74+
string s2 = arguments[0]! .ToString();
75+
string s3 = arguments[0] ! .ToString();
76+
}
77+
}
78+
}
79+
";
80+
81+
var fixedCode = @"namespace TestNamespace
82+
{
83+
public class TestClass
84+
{
85+
public void TestMethod(object?[] arguments)
86+
{
87+
object o1 = arguments[0]!;
88+
object o2 = arguments[0]! ;
89+
object o3 = arguments[0]! ;
90+
string s1 = arguments[0]!.ToString();
91+
string s2 = arguments[0]! .ToString();
92+
string s3 = arguments[0]! .ToString();
93+
}
94+
}
95+
}
96+
";
97+
98+
DiagnosticResult[] expected =
99+
{
100+
Diagnostic().WithArguments(" not", "followed").WithLocation(7, 36),
101+
Diagnostic().WithArguments(" not", "followed").WithLocation(9, 36),
102+
Diagnostic().WithArguments(" not", "followed").WithLocation(10, 36),
103+
Diagnostic().WithArguments(" not", "followed").WithLocation(12, 36),
104+
};
105+
106+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
107+
}
59108
}
60109
}

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

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

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1019MemberAccessSymbolsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public class SA1019CSharp8UnitTests : SA1019CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
20+
public async Task TestClosingSquareBracketFollowedByExclamationAsync()
21+
{
22+
var testCode = @"namespace TestNamespace
23+
{
24+
public class TestClass
25+
{
26+
public void TestMethod(object?[] arguments)
27+
{
28+
string s1 = arguments[0] !.ToString();
29+
string s2 = arguments[0]! .ToString();
30+
string s3 = arguments[0] ! .ToString();
31+
string s4 = arguments[0] !?.ToString();
32+
string s5 = arguments[0]! ?.ToString();
33+
string s6 = arguments[0] ! ?.ToString();
34+
}
35+
}
36+
}
37+
";
38+
39+
var fixedCode = @"namespace TestNamespace
40+
{
41+
public class TestClass
42+
{
43+
public void TestMethod(object?[] arguments)
44+
{
45+
string s1 = arguments[0] !.ToString();
46+
string s2 = arguments[0]!.ToString();
47+
string s3 = arguments[0] !.ToString();
48+
string s4 = arguments[0] !?.ToString();
49+
string s5 = arguments[0]!?.ToString();
50+
string s6 = arguments[0] !?.ToString();
51+
}
52+
}
53+
}
54+
";
55+
56+
DiagnosticResult[] expected =
57+
{
58+
Diagnostic().WithArguments(".", "preceded").WithLocation(8, 39),
59+
Diagnostic().WithArguments(".", "preceded").WithLocation(9, 40),
60+
Diagnostic().WithArguments("?", "preceded").WithLocation(11, 39),
61+
Diagnostic().WithArguments("?", "preceded").WithLocation(12, 40),
62+
};
63+
64+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
65+
}
1066
}
1167
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
106106
case SyntaxKind.MinusGreaterThanToken:
107107
precedesSpecialCharacter = true;
108108
break;
109+
110+
case SyntaxKind.ExclamationToken:
109111
case SyntaxKind.PlusPlusToken:
110112
case SyntaxKind.MinusMinusToken:
111113
precedesSpecialCharacter = true;

0 commit comments

Comments
 (0)