Skip to content

Commit 7a823a8

Browse files
committed
Fix SA1008 and SA1012 for pattern matching tuples
Fixes #3141
1 parent dc163bf commit 7a823a8

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,41 @@ await VerifyCSharpFixAsync(
7373
fixedCode,
7474
CancellationToken.None).ConfigureAwait(false);
7575
}
76+
77+
[Fact]
78+
[WorkItem(3141, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3141")]
79+
public async Task TestInPropertyPatternsAsync()
80+
{
81+
var testCode = @"
82+
class C
83+
{
84+
void M((string A, string B) tuple)
85+
{
86+
_ = tuple is{|#0:(|} { Length: 1 }, { Length: 2 });
87+
}
88+
}
89+
";
90+
var fixedCode = @"
91+
class C
92+
{
93+
void M((string A, string B) tuple)
94+
{
95+
_ = tuple is ({ Length: 1 }, { Length: 2 });
96+
}
97+
}
98+
";
99+
DiagnosticResult[] expectedResults =
100+
{
101+
Diagnostic(DescriptorPreceded).WithLocation(0),
102+
Diagnostic(DescriptorNotFollowed).WithLocation(0),
103+
};
104+
105+
await VerifyCSharpFixAsync(
106+
LanguageVersion.CSharp8,
107+
testCode,
108+
expectedResults,
109+
fixedCode,
110+
CancellationToken.None).ConfigureAwait(false);
111+
}
76112
}
77113
}

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

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

44
namespace StyleCop.Analyzers.Test.CSharp8.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.CSharp7.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public class SA1012CSharp8UnitTests : SA1012CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3141, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3141")]
20+
public async Task TestInPropertyPatternsAsync()
21+
{
22+
var testCode = @"
23+
class C
24+
{
25+
void M((string A, string B) tuple)
26+
{
27+
_ = tuple is ( {|#0:{|}Length: 1 },{|#1:{|}Length: 2 });
28+
}
29+
}
30+
";
31+
var fixedCode = @"
32+
class C
33+
{
34+
void M((string A, string B) tuple)
35+
{
36+
_ = tuple is ({ Length: 1 }, { Length: 2 });
37+
}
38+
}
39+
";
40+
41+
DiagnosticResult[] expectedResults =
42+
{
43+
// /0/Test0.cs(6,24): warning SA1012: Opening brace should be followed by a space
44+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"),
45+
46+
// /0/Test0.cs(6,24): warning SA1012: Opening brace should not be preceded by a space
47+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
48+
49+
// /0/Test0.cs(6,37): warning SA1012: Opening brace should be followed by a space
50+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "followed"),
51+
52+
// /0/Test0.cs(6,37): warning SA1012: Opening brace should be preceded by a space
53+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
54+
};
55+
56+
await VerifyCSharpFixAsync(
57+
LanguageVersion.CSharp8,
58+
testCode,
59+
expectedResults,
60+
fixedCode,
61+
CancellationToken.None).ConfigureAwait(false);
62+
}
1063
}
1164
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
184184
haveLeadingSpace = true;
185185
break;
186186

187+
case SyntaxKindEx.PositionalPatternClause:
188+
haveLeadingSpace = prevToken.IsKind(SyntaxKind.IsKeyword);
189+
break;
190+
187191
case SyntaxKind.ArgumentList:
188192
case SyntaxKind.AttributeArgumentList:
189193
case SyntaxKind.CheckedExpression:

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1012OpeningBracesMustBeSpacedCorrectly.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.SpacingRules
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using Microsoft.CodeAnalysis.Diagnostics;
1212
using StyleCop.Analyzers.Helpers;
13+
using StyleCop.Analyzers.Lightup;
1314

1415
/// <summary>
1516
/// An opening brace within a C# element is not spaced correctly.
@@ -88,13 +89,22 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
8889
return;
8990
}
9091

92+
bool expectPrecedingSpace = true;
93+
if (token.Parent.IsKind(SyntaxKindEx.PropertyPatternClause)
94+
&& token.GetPreviousToken() is { RawKind: (int)SyntaxKind.OpenParenToken, Parent: { RawKind: (int)SyntaxKindEx.PositionalPatternClause } })
95+
{
96+
// value is ({ P: 0 }, { P: 0 })
97+
expectPrecedingSpace = false;
98+
}
99+
91100
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
92101

93-
if (!precededBySpace)
102+
if (precededBySpace != expectPrecedingSpace)
94103
{
95104
// Opening brace should{} be {preceded} by a space.
96-
var properties = TokenSpacingProperties.InsertPreceding;
97-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "preceded"));
105+
// Opening brace should{ not} be {preceded} by a space.
106+
var properties = expectPrecedingSpace ? TokenSpacingProperties.InsertPreceding : TokenSpacingProperties.RemovePrecedingPreserveLayout;
107+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, expectPrecedingSpace ? string.Empty : " not", "preceded"));
98108
}
99109

100110
if (!token.IsLastInLine() && !followedBySpace)

0 commit comments

Comments
 (0)