Skip to content

Commit 94671b7

Browse files
committed
Update SA1413 for pattern matching
1 parent 33de76f commit 94671b7

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1413CSharp8UnitTests.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.MaintainabilityRules
75
{
86
using System.Threading;
@@ -54,6 +52,53 @@ public void TestMethod(int input)
5452
}
5553
}
5654
}
55+
";
56+
57+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
58+
}
59+
60+
[Fact]
61+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
62+
public async Task TestPropertyPatternAsync()
63+
{
64+
var testCode = @"
65+
public class TestClass
66+
{
67+
public bool Test1(SomeType value) => value is SomeType { X: 1, Y: 2 };
68+
public bool Test2(SomeType value) => value is SomeType { X: 1, Y: 2, };
69+
public bool Test3(SomeType value) => value is SomeType
70+
{
71+
X: 1,
72+
[|Y: 2|]
73+
};
74+
}
75+
76+
public class SomeType
77+
{
78+
public int X { get; set; }
79+
80+
public int Y { get; set; }
81+
}
82+
";
83+
84+
var fixedCode = @"
85+
public class TestClass
86+
{
87+
public bool Test1(SomeType value) => value is SomeType { X: 1, Y: 2 };
88+
public bool Test2(SomeType value) => value is SomeType { X: 1, Y: 2, };
89+
public bool Test3(SomeType value) => value is SomeType
90+
{
91+
X: 1,
92+
Y: 2,
93+
};
94+
}
95+
96+
public class SomeType
97+
{
98+
public int X { get; set; }
99+
100+
public int Y { get; set; }
101+
}
57102
";
58103

59104
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1413UseTrailingCommasInMultiLineInitializers.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ internal class SA1413UseTrailingCommasInMultiLineInitializers : DiagnosticAnalyz
6262
private static readonly Action<SyntaxNodeAnalysisContext> HandleAnonymousObjectInitializerAction = HandleAnonymousObjectInitializer;
6363
private static readonly Action<SyntaxNodeAnalysisContext> HandleEnumDeclarationAction = HandleEnumDeclaration;
6464
private static readonly Action<SyntaxNodeAnalysisContext> HandleSwitchExpressionAction = HandleSwitchExpression;
65+
private static readonly Action<SyntaxNodeAnalysisContext> HandlePropertyPatternClauseAction = HandlePropertyPatternClause;
6566

6667
private static readonly ImmutableArray<SyntaxKind> ObjectInitializerKinds =
6768
ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression, SyntaxKind.ArrayInitializerExpression, SyntaxKind.CollectionInitializerExpression, SyntaxKindEx.WithInitializerExpression);
@@ -80,6 +81,7 @@ public override void Initialize(AnalysisContext context)
8081
context.RegisterSyntaxNodeAction(HandleAnonymousObjectInitializerAction, SyntaxKind.AnonymousObjectCreationExpression);
8182
context.RegisterSyntaxNodeAction(HandleEnumDeclarationAction, SyntaxKind.EnumDeclaration);
8283
context.RegisterSyntaxNodeAction(HandleSwitchExpressionAction, SyntaxKindEx.SwitchExpression);
84+
context.RegisterSyntaxNodeAction(HandlePropertyPatternClauseAction, SyntaxKindEx.PropertyPatternClause);
8385
}
8486

8587
private static void HandleEnumDeclaration(SyntaxNodeAnalysisContext context)
@@ -138,5 +140,19 @@ private static void HandleSwitchExpression(SyntaxNodeAnalysisContext context)
138140
context.ReportDiagnostic(Diagnostic.Create(Descriptor, switchExpression.Arms.Last().SyntaxNode.GetLocation()));
139141
}
140142
}
143+
144+
private static void HandlePropertyPatternClause(SyntaxNodeAnalysisContext context)
145+
{
146+
var propertyPatternClause = (PropertyPatternClauseSyntaxWrapper)context.Node;
147+
if (propertyPatternClause.SyntaxNode == null || !propertyPatternClause.SyntaxNode.SpansMultipleLines())
148+
{
149+
return;
150+
}
151+
152+
if (propertyPatternClause.Subpatterns.SeparatorCount < propertyPatternClause.Subpatterns.Count)
153+
{
154+
context.ReportDiagnostic(Diagnostic.Create(Descriptor, propertyPatternClause.Subpatterns.Last().SyntaxNode.GetLocation()));
155+
}
156+
}
141157
}
142158
}

documentation/SA1413.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ var x = new Barnacle
5555
};
5656
```
5757

58-
This diagnostic is also reported for other forms of comma-separated list, such as enum members.
58+
This diagnostic is also reported for other forms of comma-separated list, such as enum members. Starting with C# 8.0, it
59+
also applies to switch expression arm lists and the subpatterns inside multi-line property patterns—add a trailing comma
60+
to the last arm or subpattern when they span multiple lines.
5961

6062
## How to fix violations
6163

0 commit comments

Comments
 (0)