Skip to content

Commit 6b7973e

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

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1008CSharp9UnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
77
{
8+
using System.Linq;
89
using System.Threading;
910
using System.Threading.Tasks;
11+
using Microsoft.CodeAnalysis;
1012
using Microsoft.CodeAnalysis.Testing;
1113
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
1214
using Xunit;
@@ -81,5 +83,32 @@ void Method((int, int) c)
8183

8284
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
8385
}
86+
87+
[Theory]
88+
[InlineData("")]
89+
[InlineData(" ")]
90+
[InlineData("\n")]
91+
[InlineData("\n ")]
92+
[WorkItem(2354, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2354")]
93+
public async Task TestDeconstructionInTopLevelProgramAsync(string prefix)
94+
{
95+
var testCode = $@"{prefix}{{|#0:(|}} var a, var b) = (1, 2);";
96+
var fixedCode = $@"{prefix}(var a, var b) = (1, 2);";
97+
98+
await new CSharpTest()
99+
{
100+
TestState =
101+
{
102+
OutputKind = OutputKind.ConsoleApplication,
103+
Sources = { testCode },
104+
},
105+
ExpectedDiagnostics =
106+
{
107+
// /0/Test0.cs(1,1): warning SA1008: Opening parenthesis should not be followed by a space.
108+
Diagnostic(DescriptorNotFollowed).WithLocation(0),
109+
},
110+
FixedCode = fixedCode,
111+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
112+
}
84113
}
85114
}

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
142142
var leadingTriviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, token.LeadingTrivia);
143143

144144
var isFirstOnLine = false;
145-
if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
145+
if (prevToken.IsKind(SyntaxKind.None))
146+
{
147+
isFirstOnLine = true; // This means that it doesn't matter if there are spaces before or not
148+
}
149+
else if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
146150
{
147151
var done = false;
148152
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--)

0 commit comments

Comments
 (0)