Skip to content

Commit 05d5907

Browse files
authored
Merge pull request #2461 from sharwell/sa1515-case-pattern
Fix SA1515 handling of comment after pattern-matching case label
2 parents 42ff24a + bd3dad5 commit 05d5907

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1515CSharp7UnitTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.LayoutRules;
9+
using Xunit;
710

811
public class SA1515CSharp7UnitTests : SA1515UnitTests
912
{
13+
[Fact]
14+
public async Task TestCommentAfterCasePatternSwitchLabelAsync()
15+
{
16+
var testCode = @"
17+
public class ClassName
18+
{
19+
public void Method()
20+
{
21+
switch (new object())
22+
{
23+
case int x:
24+
// Single line comment after pattern-matching case statement is valid
25+
break;
26+
}
27+
}
28+
}
29+
";
30+
31+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
32+
}
1033
}
1134
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1515SingleLineCommentMustBePrecededByBlankLine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.LayoutRules
1212
using Microsoft.CodeAnalysis.Diagnostics;
1313
using Microsoft.CodeAnalysis.Text;
1414
using StyleCop.Analyzers.Helpers;
15+
using StyleCop.Analyzers.Lightup;
1516

1617
/// <summary>
1718
/// A single-line comment within C# code is not preceded by a blank line.
@@ -259,6 +260,7 @@ private static bool IsAtStartOfScope(SyntaxTrivia trivia)
259260
var prevToken = token.GetPreviousToken();
260261
return prevToken.IsKind(SyntaxKind.OpenBraceToken)
261262
|| prevToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel)
263+
|| prevToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel)
262264
|| prevToken.Parent.IsKind(SyntaxKind.DefaultSwitchLabel);
263265
}
264266

0 commit comments

Comments
 (0)