Skip to content

Commit 24a60d4

Browse files
committed
Update SA1119 for pattern matching
1 parent ba57efd commit 24a60d4

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,90 @@ public void TestMethod()
328328

329329
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
330330
}
331+
332+
[Fact]
333+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
334+
public async Task TestReturnSwitchExpressionWithUnnecessaryParenthesisAsync()
335+
{
336+
const string testCode = @"
337+
public class TestClass
338+
{
339+
public object TestMethod(int n, object a, object b)
340+
{
341+
return {|#0:{|#1:(|}n switch { 1 => a, _ => b }{|#2:)|}|};
342+
}
343+
}
344+
";
345+
346+
const string fixedCode = @"
347+
public class TestClass
348+
{
349+
public object TestMethod(int n, object a, object b)
350+
{
351+
return n switch { 1 => a, _ => b };
352+
}
353+
}
354+
";
355+
356+
DiagnosticResult[] expected =
357+
{
358+
Diagnostic(DiagnosticId).WithLocation(0),
359+
Diagnostic(ParenthesesDiagnosticId).WithLocation(1),
360+
Diagnostic(ParenthesesDiagnosticId).WithLocation(2),
361+
};
362+
363+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
364+
}
365+
366+
[Fact]
367+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
368+
public async Task TestPropertyPatternWithUnnecessaryParenthesisAsync()
369+
{
370+
const string testCode = @"
371+
public class TestClass
372+
{
373+
public bool TestMethod(string value)
374+
{
375+
return {|#0:{|#1:(|}value is { Length: 1 }{|#2:)|}|};
376+
}
377+
}
378+
";
379+
380+
const string fixedCode = @"
381+
public class TestClass
382+
{
383+
public bool TestMethod(string value)
384+
{
385+
return value is { Length: 1 };
386+
}
387+
}
388+
";
389+
390+
DiagnosticResult[] expected =
391+
{
392+
Diagnostic(DiagnosticId).WithLocation(0),
393+
Diagnostic(ParenthesesDiagnosticId).WithLocation(1),
394+
Diagnostic(ParenthesesDiagnosticId).WithLocation(2),
395+
};
396+
397+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
398+
}
399+
400+
[Fact]
401+
[WorkItem(3003, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3003")]
402+
public async Task TestNegatedPropertyPatternIsNotReportedAsync()
403+
{
404+
const string testCode = @"
405+
public class TestClass
406+
{
407+
public bool TestMethod(string value)
408+
{
409+
return !(value is { Length: 0 });
410+
}
411+
}
412+
";
413+
414+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
415+
}
331416
}
332417
}

documentation/SA1119.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ string y = this.Method().ToString();
4444
return x.Value;
4545
```
4646

47+
Starting with C# 8.0, parenthesis may be required around switch expressions or pattern-matching expressions when they
48+
participate in other operations (for example, casts, `await`, conditional/element/member access, or logical negation).
49+
SA1119 will not report parenthesis that are necessary for correctness, but it will still flag parenthesis that are
50+
purely redundant, such as `return (value switch { ... });` or `var flag = (input is { Length: 1 });`.
51+
4752
## How to fix violations
4853

4954
To fix a violation of this rule, remove the unnecessary parenthesis.

0 commit comments

Comments
 (0)