Skip to content

Commit

Permalink
Merge pull request #2424 from sharwell/pattern-matching-is
Browse files Browse the repository at this point in the history
Fix 'is pattern expression' parentheses handling
  • Loading branch information
sharwell committed Jun 19, 2017
2 parents b2f55c2 + ac0eff3 commit e809b29
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SA1119CSharp7UnitTests : SA1119UnitTests
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
/// <seealso cref="SA1408CSharp7UnitTests.TestPatternMatchingAsync"/>
[Fact]
[Fact(Skip = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2398")]
public async Task TestPatternMatchingAsync()
{
var testCode = @"public class Foo
Expand All @@ -33,7 +33,7 @@ public void Bar()
{
public void Bar()
{
if ( new object() is bool b && b)
if (new object() is bool b && b)
{
return;
}
Expand All @@ -52,6 +52,25 @@ public void Bar()
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
}

[Fact]
[WorkItem(2372, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2372")]
public async Task TestNegatedPatternMatchingAsync()
{
var testCode = @"public class Foo
{
public void Bar()
{
object obj = null;
if (!(obj is string anythng))
{
// ...
}
}
}";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestTupleDeconstructionAsync()
{
Expand Down
10 changes: 10 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers.Test/AttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ public void TestNoDiagnosticAttributeReason()
var attribute = new NoDiagnosticAttribute(reason);
Assert.Same(reason, attribute.Reason);
}

[Fact]
public void TestWorkItemAttribute()
{
int id = 1234;
string issueUri = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2419";
var attribute = new WorkItemAttribute(id, issueUri);
Assert.Equal(id, attribute.Id);
Assert.Same(issueUri, attribute.Location);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@
<Compile Include="Verifiers\CodeFixVerifier.cs" />
<Compile Include="Verifiers\DiagnosticVerifier.cs" />
<Compile Include="Verifiers\DiagnosticVerifierTests.cs" />
<Compile Include="WorkItemAttribute.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\build\keys\TestingKey.snk">
Expand Down
37 changes: 37 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers.Test/WorkItemAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test
{
using System;

/// <summary>
/// Used to tag test methods or types which are created for a given WorkItem
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public sealed class WorkItemAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="WorkItemAttribute"/> class.
/// </summary>
/// <param name="id">The ID of the issue in the original tracker where the work item was first reported. This
/// could be a GitHub issue or pull request number, or the number of a Microsoft-internal bug.</param>
/// <param name="issueUri">The URI where the work item can be viewed. This is a link to work item
/// <paramref name="id"/> in the original source.</param>
public WorkItemAttribute(int id, string issueUri)
{
this.Id = id;
this.Location = issueUri;
}

public int Id
{
get;
}

public string Location
{
get;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Lightup;

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

0 comments on commit e809b29

Please sign in to comment.