Skip to content

Commit 0c31e29

Browse files
authored
Merge pull request #2742 from vweijsters/fix-2735
Allow generic type to be followed by colon token for switch pattern m…
2 parents d96f68a + 07a1320 commit 0c31e29

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,33 @@ public void TestMethod()
5050
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5151
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5252
}
53+
54+
/// <summary>
55+
/// Verifies that a pattern in a switch statement that ends with a generic type does not produce a diagnostic.
56+
/// </summary>
57+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
58+
[Fact]
59+
[WorkItem(2735, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2735")]
60+
public async Task TestPatternMatchingInSwitchStatementWithGenericTypesAsync()
61+
{
62+
var testCode = @"using System.Collections.Generic;
63+
64+
public class TestClass
65+
{
66+
public void TestMethod(int i, object value)
67+
{
68+
switch (i)
69+
{
70+
case 1 when value is List<TestClass>:
71+
break;
72+
}
73+
74+
var x = i == 3 ? value as List<TestClass> : null;
75+
}
76+
}
77+
";
78+
79+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
80+
}
5381
}
5482
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.SpacingRules
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Diagnostics;
1111
using StyleCop.Analyzers.Helpers;
12+
using StyleCop.Analyzers.Lightup;
1213

1314
/// <summary>
1415
/// A closing generic bracket within a C# element is not spaced correctly.
@@ -103,6 +104,7 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy
103104
case SyntaxKind.OpenBracketToken:
104105
// SemicolonToken isn't listed above, but it's required for reasonable using alias declaration formatting
105106
case SyntaxKind.SemicolonToken:
107+
case SyntaxKind.ColonToken when nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel):
106108
allowTrailingNoSpace = true;
107109
allowTrailingSpace = false;
108110
break;

0 commit comments

Comments
 (0)