Skip to content

Commit 1ae44e2

Browse files
committed
Update SA1500 for pattern matching
1 parent 24a60d4 commit 1ae44e2

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/LayoutRules/SA1500CSharp8UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp8.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp7.LayoutRules;
10+
using StyleCop.Analyzers.Test.Helpers;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.LayoutRules.SA1500BracesForMultiLineStatementsMustNotShareLine,
14+
StyleCop.Analyzers.LayoutRules.SA1500CodeFixProvider>;
715

816
public partial class SA1500CSharp8UnitTests : SA1500CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
20+
public async Task TestSwitchExpressionBracesMustOwnLineAsync()
21+
{
22+
var testCode = @"
23+
public class TestClass
24+
{
25+
public int Test(Item value)
26+
{
27+
return value switch {|#0:{|}
28+
{ Prop: 1 } => 1,
29+
_ => 0 {|#1:}|};
30+
}
31+
}
32+
33+
public class Item
34+
{
35+
public int Prop { get; set; }
36+
}
37+
";
38+
39+
var fixedCode = @"
40+
public class TestClass
41+
{
42+
public int Test(Item value)
43+
{
44+
return value switch
45+
{
46+
{ Prop: 1 } => 1,
47+
_ => 0
48+
};
49+
}
50+
}
51+
52+
public class Item
53+
{
54+
public int Prop { get; set; }
55+
}
56+
";
57+
58+
DiagnosticResult[] expectedDiagnostics =
59+
{
60+
Diagnostic().WithLocation(0),
61+
Diagnostic().WithLocation(1),
62+
};
63+
64+
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
65+
}
66+
67+
[Fact]
68+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
69+
public async Task TestPropertyPatternBracesMustOwnLineAsync()
70+
{
71+
var testCode = @"
72+
public class TestClass
73+
{
74+
public bool Test(object value)
75+
{
76+
return value is TestType {|#0:{|} A: 1,
77+
B: 2 {|#1:}|};
78+
}
79+
}
80+
81+
public class TestType
82+
{
83+
public int A { get; set; }
84+
85+
public int B { get; set; }
86+
}
87+
";
88+
89+
var fixedCode = @"
90+
public class TestClass
91+
{
92+
public bool Test(object value)
93+
{
94+
return value is TestType
95+
{
96+
A: 1,
97+
B: 2
98+
};
99+
}
100+
}
101+
102+
public class TestType
103+
{
104+
public int A { get; set; }
105+
106+
public int B { get; set; }
107+
}
108+
";
109+
110+
DiagnosticResult[] expectedDiagnostics =
111+
{
112+
Diagnostic().WithLocation(0),
113+
Diagnostic().WithLocation(1),
114+
};
115+
116+
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
117+
}
10118
}
11119
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ internal class SA1500BracesForMultiLineStatementsMustNotShareLine : DiagnosticAn
7979
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> SwitchStatementAction = HandleSwitchStatement;
8080
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> InitializerExpressionAction = HandleInitializerExpression;
8181
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> AnonymousObjectCreationExpressionAction = HandleAnonymousObjectCreationExpression;
82+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> SwitchExpressionAction = HandleSwitchExpression;
83+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> PropertyPatternClauseAction = HandlePropertyPatternClause;
8284

8385
/// <inheritdoc/>
8486
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -99,6 +101,8 @@ public override void Initialize(AnalysisContext context)
99101
context.RegisterSyntaxNodeAction(SwitchStatementAction, SyntaxKind.SwitchStatement);
100102
context.RegisterSyntaxNodeAction(InitializerExpressionAction, SyntaxKinds.InitializerExpression);
101103
context.RegisterSyntaxNodeAction(AnonymousObjectCreationExpressionAction, SyntaxKind.AnonymousObjectCreationExpression);
104+
context.RegisterSyntaxNodeAction(SwitchExpressionAction, SyntaxKindEx.SwitchExpression);
105+
context.RegisterSyntaxNodeAction(PropertyPatternClauseAction, SyntaxKindEx.PropertyPatternClause);
102106
});
103107
}
104108

@@ -144,6 +148,18 @@ private static void HandleAnonymousObjectCreationExpression(SyntaxNodeAnalysisCo
144148
CheckBraces(context, settings, syntax.OpenBraceToken, syntax.CloseBraceToken);
145149
}
146150

151+
private static void HandleSwitchExpression(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
152+
{
153+
var syntax = (SwitchExpressionSyntaxWrapper)context.Node;
154+
CheckBraces(context, settings, syntax.OpenBraceToken, syntax.CloseBraceToken);
155+
}
156+
157+
private static void HandlePropertyPatternClause(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
158+
{
159+
var syntax = (PropertyPatternClauseSyntaxWrapper)context.Node;
160+
CheckBraces(context, settings, syntax.OpenBraceToken, syntax.CloseBraceToken);
161+
}
162+
147163
private static void CheckBraces(SyntaxNodeAnalysisContext context, StyleCopSettings settings, SyntaxToken openBraceToken, SyntaxToken closeBraceToken)
148164
{
149165
if (openBraceToken.IsKind(SyntaxKind.None) || closeBraceToken.IsKind(SyntaxKind.None))

documentation/SA1500.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public object Method()
5757
}
5858
```
5959

60+
Starting with C# 8.0, this rule also applies to braces introduced by switch expressions and property patterns. When the
61+
arms or pattern contents span multiple lines, the `{` and `}` for the switch expression or property pattern must appear
62+
on their own lines.
63+
6064
## How to fix violations
6165

6266
To fix a violation of this rule, ensure that both the opening and closing braces are placed on their own line, and do not share the line with any other code, other than comments.

0 commit comments

Comments
 (0)