Skip to content

Commit b72b3f7

Browse files
authored
Merge pull request #3934 from bjornhellander/feature/sa1008-parent-in-collection-expression-3931
Update SA1008OpeningParenthesisMustBeSpacedCorrectly to disallow a leading space before a lambda in a collection expression
2 parents 40f4cca + b76e5d9 commit b72b3f7

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,88 @@ await VerifyCSharpFixAsync(
7272
fixedCode,
7373
CancellationToken.None).ConfigureAwait(false);
7474
}
75+
76+
[Fact]
77+
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
78+
public async Task TestParenthesizedLambdaInCollectionExpressionAsync()
79+
{
80+
var testCode = @"
81+
class TestClass
82+
{
83+
private System.Action[] actions = [ [|(|]) => {}];
84+
}
85+
";
86+
87+
var fixedCode = @"
88+
class TestClass
89+
{
90+
private System.Action[] actions = [() => {}];
91+
}
92+
";
93+
94+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
95+
}
96+
97+
[Fact]
98+
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
99+
public async Task TestLambdaAfterCommaInCollectionExpressionAsync()
100+
{
101+
var testCode = @"
102+
class TestClass
103+
{
104+
private System.Func<int, int, int>[] functions = [(x, y) => x + y,{|#0:(|}x, y) => x - y];
105+
}
106+
";
107+
108+
var fixedCode = @"
109+
class TestClass
110+
{
111+
private System.Func<int, int, int>[] functions = [(x, y) => x + y, (x, y) => x - y];
112+
}
113+
";
114+
115+
var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
116+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
117+
}
118+
119+
[Fact]
120+
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
121+
public async Task TestLambdaAfterSpreadInCollectionExpressionAsync()
122+
{
123+
var testCode = @"
124+
class TestClass
125+
{
126+
private static System.Func<int, int, int>[] existing = [(x, y) => x + y];
127+
private System.Func<int, int, int>[] functions = [..existing,{|#0:(|}x, y) => x - y];
128+
}
129+
";
130+
131+
var fixedCode = @"
132+
class TestClass
133+
{
134+
private static System.Func<int, int, int>[] existing = [(x, y) => x + y];
135+
private System.Func<int, int, int>[] functions = [..existing, (x, y) => x - y];
136+
}
137+
";
138+
139+
var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
140+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
141+
}
142+
143+
[Fact]
144+
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
145+
public async Task TestLambdaWithBracketOnPreviousLineAsync()
146+
{
147+
var testCode = @"
148+
class TestClass
149+
{
150+
private System.Func<int, int, int>[] functions = [
151+
(x, y) => x + y
152+
];
153+
}
154+
";
155+
156+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
157+
}
75158
}
76159
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,34 @@ public void TestMethod1()
18951895
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
18961896
}
18971897

1898+
[Fact]
1899+
[WorkItem(3931, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3931")]
1900+
public async Task TestLambdaInCollectionInitializerAsync()
1901+
{
1902+
var testCode = @"
1903+
using System;
1904+
using System.Collections.Generic;
1905+
1906+
class TestClass
1907+
{
1908+
private List<Action<int, int>> actions = new List<Action<int, int>>() {{|#0:(|}x, y) => { } };
1909+
}
1910+
";
1911+
1912+
var fixedCode = @"
1913+
using System;
1914+
using System.Collections.Generic;
1915+
1916+
class TestClass
1917+
{
1918+
private List<Action<int, int>> actions = new List<Action<int, int>>() { (x, y) => { } };
1919+
}
1920+
";
1921+
1922+
var expected = Diagnostic(DescriptorPreceded).WithLocation(0);
1923+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
1924+
}
1925+
18981926
/// <summary>
18991927
/// Verifies that multi-line comments are handled properly.
19001928
/// </summary>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
248248

249249
case SyntaxKind.ParameterList:
250250
var partOfLambdaExpression = token.Parent.Parent.IsKind(SyntaxKind.ParenthesizedLambdaExpression);
251-
haveLeadingSpace = partOfLambdaExpression;
251+
var startOfCollectionExpression = prevToken.IsKind(SyntaxKind.OpenBracketToken) && prevToken.Parent.IsKind(SyntaxKindEx.CollectionExpression);
252+
haveLeadingSpace = partOfLambdaExpression && !startOfCollectionExpression;
252253
break;
253254

254255
case SyntaxKindEx.TupleType:

0 commit comments

Comments
 (0)