Skip to content

Commit 4972ba8

Browse files
Update SA1008 to correctly handle positional patterns inside property patterns
#3556
1 parent 224763f commit 4972ba8

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,50 @@ await VerifyCSharpFixAsync(
149149
fixedCode,
150150
CancellationToken.None).ConfigureAwait(false);
151151
}
152+
153+
[Fact]
154+
[WorkItem(3556, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3556")]
155+
public async Task TestInPositionalPatternAfterTagAsync()
156+
{
157+
var testCode = @"
158+
internal class TestClass
159+
{
160+
private void TestMethod(object obj)
161+
{
162+
_ = obj is ClassWithTuple { Tag:{|#0:(|} true, false) };
163+
}
164+
165+
public class ClassWithTuple
166+
{
167+
public (bool Item1, bool Item2) Tag { get; set; }
168+
}
169+
}
170+
";
171+
var fixedCode = @"
172+
internal class TestClass
173+
{
174+
private void TestMethod(object obj)
175+
{
176+
_ = obj is ClassWithTuple { Tag: (true, false) };
177+
}
178+
179+
public class ClassWithTuple
180+
{
181+
public (bool Item1, bool Item2) Tag { get; set; }
182+
}
183+
}
184+
";
185+
DiagnosticResult[] expectedResults =
186+
{
187+
Diagnostic(DescriptorPreceded).WithLocation(0),
188+
Diagnostic(DescriptorNotFollowed).WithLocation(0),
189+
};
190+
191+
await VerifyCSharpFixAsync(
192+
testCode,
193+
expectedResults,
194+
fixedCode,
195+
CancellationToken.None).ConfigureAwait(false);
196+
}
152197
}
153198
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
191191
|| prevToken.IsKind(SyntaxKindEx.OrKeyword)
192192
|| prevToken.IsKind(SyntaxKindEx.AndKeyword)
193193
|| prevToken.IsKind(SyntaxKindEx.NotKeyword)
194-
|| prevToken.IsKind(SyntaxKind.CommaToken);
194+
|| prevToken.IsKind(SyntaxKind.CommaToken)
195+
|| prevToken.IsKind(SyntaxKind.ColonToken);
195196
break;
196197

197198
case SyntaxKindEx.ParenthesizedPattern:

0 commit comments

Comments
 (0)