Skip to content

Commit 320f616

Browse files
Update SA1015 to not care about trailing spaces in a number of cases: typically last in dictionary initializer items, collection initializers and collection expressions. #3856
1 parent b9a30bc commit 320f616

File tree

6 files changed

+80
-10
lines changed

6 files changed

+80
-10
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/SpacingRules/SA1015CSharp11UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
75
{
86
using System.Threading;

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,40 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp12.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using Microsoft.CodeAnalysis.Testing;
79
using StyleCop.Analyzers.Test.CSharp11.SpacingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.SpacingRules.SA1015ClosingGenericBracketsMustBeSpacedCorrectly,
13+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
814

915
public partial class SA1015CSharp12UnitTests : SA1015CSharp11UnitTests
1016
{
17+
[Theory]
18+
[InlineData(" M<int> ")]
19+
[InlineData("M<int>")]
20+
[WorkItem(3856, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3856")]
21+
public async Task TestSpacingAfterGenericMethodGroupInCollectionExpressionAsync(string item)
22+
{
23+
var testCode = $@"
24+
using System;
25+
using System.Collections.Generic;
26+
27+
public class TestClass
28+
{{
29+
private List<Action> values = [{item}];
30+
31+
private static void M<T>()
32+
{{
33+
}}
34+
}}
35+
";
36+
37+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
38+
}
39+
1140
protected override DiagnosticResult[] GetExpectedResultMissingToken()
1241
{
1342
return new[]

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1015CSharp7UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules
75
{
86
using System.Threading;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
75
{
86
using System.Threading;

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.SpacingRules
75
{
86
using System.Threading;
@@ -430,6 +428,55 @@ public void TestMethod2(object input)
430428
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
431429
}
432430

431+
[Theory]
432+
[InlineData(" M<int> ")]
433+
[InlineData("M<int>")]
434+
[WorkItem(3856, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3856")]
435+
public async Task TestSpacingAfterGenericMethodGroupInCollectionInitializerAsync(string item)
436+
{
437+
var testCode = $@"
438+
using System;
439+
using System.Collections.Generic;
440+
441+
public class TestClass
442+
{{
443+
private List<Action> values = new List<Action> {{{item}}};
444+
445+
private static void M<T>()
446+
{{
447+
}}
448+
}}
449+
";
450+
451+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
452+
}
453+
454+
[Theory]
455+
[InlineData(" 1, M<int> ")]
456+
[InlineData("1, M<int>")]
457+
[WorkItem(3856, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3856")]
458+
public async Task TestSpacingAfterGenericMethodGroupInDictionaryInitializerItemAsync(string item)
459+
{
460+
var testCode = $@"
461+
using System;
462+
using System.Collections.Generic;
463+
464+
public class TestClass
465+
{{
466+
private Dictionary<int, Action> values = new Dictionary<int, Action>
467+
{{
468+
{{{item}}}
469+
}};
470+
471+
private static void M<T>()
472+
{{
473+
}}
474+
}}
475+
";
476+
477+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
478+
}
479+
433480
protected virtual DiagnosticResult[] GetExpectedResultMissingToken()
434481
{
435482
return new[]

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.SpacingRules
75
{
86
using System;
@@ -127,6 +125,8 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy
127125

128126
case SyntaxKind.CloseParenToken:
129127
case SyntaxKind.GreaterThanToken:
128+
case SyntaxKind.CloseBraceToken:
129+
case SyntaxKind.CloseBracketToken when nextToken.Parent.IsKind(SyntaxKindEx.CollectionExpression):
130130
allowTrailingNoSpace = true;
131131
allowTrailingSpace = true;
132132
break;

0 commit comments

Comments
 (0)