Skip to content

Commit bc00245

Browse files
authored
Merge pull request #3265 from sharwell/paren-pattern
Fix SA1008 handling of parenthesized patterns
2 parents 3414e7a + f09cd8d commit bc00245

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1008CSharp9UnitTests.cs

+55
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,64 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.SpacingRules.SA1008OpeningParenthesisMustBeSpacedCorrectly;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.SpacingRules.SA1008OpeningParenthesisMustBeSpacedCorrectly,
15+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
716

817
public class SA1008CSharp9UnitTests : SA1008CSharp8UnitTests
918
{
19+
[Fact]
20+
[WorkItem(3230, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3230")]
21+
public async Task TestParenthesizedPatternAsync()
22+
{
23+
const string testCode = @"
24+
class C
25+
{
26+
void Method(int b)
27+
{
28+
_ = b is{|#0:(|} >= 0 and <= 31) or 127;
29+
_ = b is{|#1:(|}>= 0 and <= 31) or 127;
30+
_ = b is {|#2:(|} >= 0 and <= 31) or 127;
31+
}
32+
}";
33+
const string fixedCode = @"
34+
class C
35+
{
36+
void Method(int b)
37+
{
38+
_ = b is (>= 0 and <= 31) or 127;
39+
_ = b is (>= 0 and <= 31) or 127;
40+
_ = b is (>= 0 and <= 31) or 127;
41+
}
42+
}";
43+
44+
await new CSharpTest(LanguageVersion.CSharp9)
45+
{
46+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
47+
ExpectedDiagnostics =
48+
{
49+
// /0/Test0.cs(6,17): warning SA1008: Opening parenthesis should be preceded by a space.
50+
Diagnostic(DescriptorPreceded).WithLocation(0),
51+
52+
// /0/Test0.cs(6,17): warning SA1008: Opening parenthesis should not be followed by a space.
53+
Diagnostic(DescriptorNotFollowed).WithLocation(0),
54+
55+
// /0/Test0.cs(7,17): warning SA1008: Opening parenthesis should be preceded by a space.
56+
Diagnostic(DescriptorPreceded).WithLocation(1),
57+
58+
// /0/Test0.cs(8,18): warning SA1008: Opening parenthesis should not be followed by a space.
59+
Diagnostic(DescriptorNotFollowed).WithLocation(2),
60+
},
61+
TestCode = testCode,
62+
FixedCode = fixedCode,
63+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
64+
}
1065
}
1166
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ internal static class SyntaxKindEx
4444
public const SyntaxKind SwitchExpression = (SyntaxKind)9025;
4545
public const SyntaxKind SwitchExpressionArm = (SyntaxKind)9026;
4646
public const SyntaxKind VarPattern = (SyntaxKind)9027;
47+
public const SyntaxKind ParenthesizedPattern = (SyntaxKind)9028;
4748
public const SyntaxKind DeclarationExpression = (SyntaxKind)9040;
4849
public const SyntaxKind RefExpression = (SyntaxKind)9050;
4950
public const SyntaxKind RefType = (SyntaxKind)9051;

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
189189
|| prevToken.IsKind(SyntaxKind.CommaToken);
190190
break;
191191

192+
case SyntaxKindEx.ParenthesizedPattern:
193+
var partOfCastExpression = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
194+
haveLeadingSpace = !partOfCastExpression;
195+
break;
196+
192197
case SyntaxKind.ArgumentList:
193198
case SyntaxKind.AttributeArgumentList:
194199
case SyntaxKind.CheckedExpression:
@@ -216,7 +221,7 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
216221

217222
partOfUnaryExpression = prevToken.Parent is PrefixUnaryExpressionSyntax;
218223
startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken);
219-
var partOfCastExpression = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
224+
partOfCastExpression = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
220225

221226
haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !partOfCastExpression;
222227
break;

0 commit comments

Comments
 (0)