Skip to content

Commit 2d7e105

Browse files
Update SA1008 to require a space before the opening parenthesis if it's the left operand of a range expression. Also add test for SA1008 in collection expression.
DotNetAnalyzers#3894
1 parent 2dc4507 commit 2d7e105

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1008CSharp12UnitTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Test.CSharp12.SpacingRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
89
using StyleCop.Analyzers.Test.CSharp11.SpacingRules;
910
using Xunit;
1011

@@ -28,5 +29,48 @@ public async Task TestTupleUsingAliasAsync()
2829
var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
2930
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
3031
}
32+
33+
[Fact]
34+
[WorkItem(3894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3894")]
35+
public async Task TestCollectionExpressionAsync()
36+
{
37+
var testCode = @"
38+
namespace TestNamespace
39+
{
40+
public class TestClass
41+
{
42+
public void TestMethod()
43+
{
44+
int[] x = [ {|#0:(|} 0 + 0)];
45+
}
46+
}
47+
}
48+
";
49+
50+
var fixedCode = @"
51+
namespace TestNamespace
52+
{
53+
public class TestClass
54+
{
55+
public void TestMethod()
56+
{
57+
int[] x = [(0 + 0)];
58+
}
59+
}
60+
}
61+
";
62+
63+
DiagnosticResult[] expectedResults =
64+
{
65+
Diagnostic(DescriptorNotPreceded).WithLocation(0),
66+
Diagnostic(DescriptorNotFollowed).WithLocation(0),
67+
};
68+
69+
await VerifyCSharpFixAsync(
70+
testCode,
71+
expectedResults,
72+
fixedCode,
73+
CancellationToken.None).ConfigureAwait(false);
74+
}
3175
}
3276
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,48 @@ await VerifyCSharpFixAsync(
194194
fixedCode,
195195
CancellationToken.None).ConfigureAwait(false);
196196
}
197+
198+
[Fact]
199+
[WorkItem(3894, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3894")]
200+
public async Task TestLeftOperandInRangeExpressionAsync()
201+
{
202+
var testCode = @"
203+
namespace TestNamespace
204+
{
205+
public class TestClass
206+
{
207+
public void TestMethod()
208+
{
209+
var x ={|#0:(|} 0 + 0)..1;
210+
}
211+
}
212+
}
213+
";
214+
215+
var fixedCode = @"
216+
namespace TestNamespace
217+
{
218+
public class TestClass
219+
{
220+
public void TestMethod()
221+
{
222+
var x = (0 + 0)..1;
223+
}
224+
}
225+
}
226+
";
227+
228+
DiagnosticResult[] expectedResults =
229+
{
230+
Diagnostic(DescriptorPreceded).WithLocation(0),
231+
Diagnostic(DescriptorNotFollowed).WithLocation(0),
232+
};
233+
234+
await VerifyCSharpFixAsync(
235+
testCode,
236+
expectedResults,
237+
fixedCode,
238+
CancellationToken.None).ConfigureAwait(false);
239+
}
197240
}
198241
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
223223
case SyntaxKind.ParenthesizedExpression:
224224
case SyntaxKindEx.TupleExpression:
225225
if (prevToken.Parent.IsKind(SyntaxKind.Interpolation)
226-
|| token.Parent.Parent.IsKind(SyntaxKindEx.RangeExpression))
226+
|| (token.Parent.Parent.IsKind(SyntaxKindEx.RangeExpression) && ((RangeExpressionSyntaxWrapper)token.Parent.Parent).RightOperand == token.Parent))
227227
{
228228
haveLeadingSpace = false;
229229
break;

0 commit comments

Comments
 (0)