Skip to content

Commit d48383d

Browse files
committed
Make SA1011 work with exclamation (!) (#3052)
1 parent 59adc83 commit d48383d

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,64 @@ 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 o = arguments[0]!;
71+
string s = arguments[0]!.ToString();
72+
}
73+
}
74+
}
75+
";
76+
77+
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
78+
}
79+
80+
[Fact]
81+
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
82+
public async Task TestInvalidClosingSquareBracketFollowedByExclamationAsync()
83+
{
84+
var testCode = @"namespace TestNamespace
85+
{
86+
public class TestClass
87+
{
88+
public void TestMethod(object?[] arguments)
89+
{
90+
object o = arguments[0] !;
91+
string s = arguments[0] !.ToString();
92+
}
93+
}
94+
}
95+
";
96+
97+
var fixedCode = @"namespace TestNamespace
98+
{
99+
public class TestClass
100+
{
101+
public void TestMethod(object?[] arguments)
102+
{
103+
object o = arguments[0]!;
104+
string s = arguments[0]!.ToString();
105+
}
106+
}
107+
}
108+
";
109+
110+
DiagnosticResult[] expected =
111+
{
112+
Diagnostic().WithArguments(" not", "followed").WithLocation(7, 35),
113+
Diagnostic().WithArguments(" not", "followed").WithLocation(8, 35),
114+
};
115+
116+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
117+
}
59118
}
60119
}

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)