Skip to content

Commit ac0eff3

Browse files
committed
Avoid reporting necessary parenthesis in 'is pattern expression'
Fixes #2372
1 parent 9166e4f commit ac0eff3

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1119CSharp7UnitTests.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class SA1119CSharp7UnitTests : SA1119UnitTests
1616
/// </summary>
1717
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
1818
/// <seealso cref="SA1408CSharp7UnitTests.TestPatternMatchingAsync"/>
19-
[Fact]
19+
[Fact(Skip = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2398")]
2020
public async Task TestPatternMatchingAsync()
2121
{
2222
var testCode = @"public class Foo
@@ -33,7 +33,7 @@ public void Bar()
3333
{
3434
public void Bar()
3535
{
36-
if ( new object() is bool b && b)
36+
if (new object() is bool b && b)
3737
{
3838
return;
3939
}
@@ -52,6 +52,25 @@ public void Bar()
5252
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
5353
}
5454

55+
[Fact]
56+
[WorkItem(2372, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2372")]
57+
public async Task TestNegatedPatternMatchingAsync()
58+
{
59+
var testCode = @"public class Foo
60+
{
61+
public void Bar()
62+
{
63+
object obj = null;
64+
if (!(obj is string anythng))
65+
{
66+
// ...
67+
}
68+
}
69+
}";
70+
71+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
72+
}
73+
5574
[Fact]
5675
public async Task TestTupleDeconstructionAsync()
5776
{

StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs

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

1415
/// <summary>
1516
/// A C# statement contains parenthesis which are unnecessary and should be removed.
@@ -106,6 +107,7 @@ private static void HandleParenthesizedExpression(SyntaxNodeAnalysisContext cont
106107
&& !node.Expression.IsKind(SyntaxKind.CastExpression)
107108
&& !node.Expression.IsKind(SyntaxKind.ConditionalExpression)
108109
&& !node.Expression.IsKind(SyntaxKind.IsExpression)
110+
&& !node.Expression.IsKind(SyntaxKindEx.IsPatternExpression)
109111
&& !node.Expression.IsKind(SyntaxKind.SimpleLambdaExpression)
110112
&& !node.Expression.IsKind(SyntaxKind.ParenthesizedLambdaExpression)
111113
&& !node.Expression.IsKind(SyntaxKind.ArrayCreationExpression)

0 commit comments

Comments
 (0)