Skip to content

Commit dea9473

Browse files
Update SA1008 to not crash if there is no token before the analyzed opening parenthesis
#2354
1 parent be49652 commit dea9473

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1008UnitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,5 +2158,22 @@ public void TestMethod()
21582158

21592159
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
21602160
}
2161+
2162+
[Fact]
2163+
[WorkItem(2354, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2354")]
2164+
public async Task TestNoPreviousTokenAsync()
2165+
{
2166+
var testCode = "(";
2167+
2168+
var test = new CSharpTest()
2169+
{
2170+
TestCode = testCode,
2171+
2172+
// Compiler diagnostics differ between Roslyn versions. The main thing is that the analyzer doesn't throw an exception.
2173+
CompilerDiagnostics = CompilerDiagnostics.None,
2174+
};
2175+
2176+
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
2177+
}
21612178
}
21622179
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
100100
bool precededByKeyword;
101101
switch (prevToken.Kind())
102102
{
103+
case SyntaxKind.None:
104+
return;
105+
103106
case SyntaxKind.AwaitKeyword:
104107
case SyntaxKind.CaseKeyword:
105108
case SyntaxKind.CatchKeyword:

0 commit comments

Comments
 (0)