Skip to content

Commit e809b29

Browse files
committed
Merge pull request #2424 from sharwell/pattern-matching-is
Fix 'is pattern expression' parentheses handling
2 parents b2f55c2 + ac0eff3 commit e809b29

5 files changed

Lines changed: 71 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.Test/AttributeTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,15 @@ public void TestNoDiagnosticAttributeReason()
2222
var attribute = new NoDiagnosticAttribute(reason);
2323
Assert.Same(reason, attribute.Reason);
2424
}
25+
26+
[Fact]
27+
public void TestWorkItemAttribute()
28+
{
29+
int id = 1234;
30+
string issueUri = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2419";
31+
var attribute = new WorkItemAttribute(id, issueUri);
32+
Assert.Equal(id, attribute.Id);
33+
Assert.Same(issueUri, attribute.Location);
34+
}
2535
}
2636
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@
423423
<Compile Include="Verifiers\CodeFixVerifier.cs" />
424424
<Compile Include="Verifiers\DiagnosticVerifier.cs" />
425425
<Compile Include="Verifiers\DiagnosticVerifierTests.cs" />
426+
<Compile Include="WorkItemAttribute.cs" />
426427
</ItemGroup>
427428
<ItemGroup>
428429
<None Include="..\..\build\keys\TestingKey.snk">
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test
5+
{
6+
using System;
7+
8+
/// <summary>
9+
/// Used to tag test methods or types which are created for a given WorkItem
10+
/// </summary>
11+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
12+
public sealed class WorkItemAttribute : Attribute
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="WorkItemAttribute"/> class.
16+
/// </summary>
17+
/// <param name="id">The ID of the issue in the original tracker where the work item was first reported. This
18+
/// could be a GitHub issue or pull request number, or the number of a Microsoft-internal bug.</param>
19+
/// <param name="issueUri">The URI where the work item can be viewed. This is a link to work item
20+
/// <paramref name="id"/> in the original source.</param>
21+
public WorkItemAttribute(int id, string issueUri)
22+
{
23+
this.Id = id;
24+
this.Location = issueUri;
25+
}
26+
27+
public int Id
28+
{
29+
get;
30+
}
31+
32+
public string Location
33+
{
34+
get;
35+
}
36+
}
37+
}

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)