From 9166e4f5f466be9610468b76e1cb281ff7acd31a Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 16 Jun 2017 18:54:32 -0500 Subject: [PATCH 1/2] Add WorkItemAttribute --- .../StyleCop.Analyzers.Test/AttributeTests.cs | 10 +++++ .../StyleCop.Analyzers.Test.csproj | 1 + .../WorkItemAttribute.cs | 37 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 StyleCop.Analyzers/StyleCop.Analyzers.Test/WorkItemAttribute.cs diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/AttributeTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/AttributeTests.cs index a39c156d6..fb387eac6 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/AttributeTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/AttributeTests.cs @@ -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); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj b/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj index 8711eb196..8fb8cce44 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj @@ -410,6 +410,7 @@ + diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/WorkItemAttribute.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/WorkItemAttribute.cs new file mode 100644 index 000000000..afd1e5dfe --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/WorkItemAttribute.cs @@ -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; + + /// + /// Used to tag test methods or types which are created for a given WorkItem + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] + public sealed class WorkItemAttribute : Attribute + { + /// + /// Initializes a new instance of the class. + /// + /// 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. + /// The URI where the work item can be viewed. This is a link to work item + /// in the original source. + public WorkItemAttribute(int id, string issueUri) + { + this.Id = id; + this.Location = issueUri; + } + + public int Id + { + get; + } + + public string Location + { + get; + } + } +} From ac0eff31749709edc124221c687ad54428a11247 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 16 Jun 2017 18:55:09 -0500 Subject: [PATCH 2/2] Avoid reporting necessary parenthesis in 'is pattern expression' Fixes #2372 --- .../SA1119CSharp7UnitTests.cs | 23 +++++++++++++++++-- ...atementMustNotUseUnnecessaryParenthesis.cs | 2 ++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1119CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1119CSharp7UnitTests.cs index 781522c2e..2b060d401 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1119CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/MaintainabilityRules/SA1119CSharp7UnitTests.cs @@ -16,7 +16,7 @@ public class SA1119CSharp7UnitTests : SA1119UnitTests /// /// A representing the asynchronous unit test. /// - [Fact] + [Fact(Skip = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2398")] public async Task TestPatternMatchingAsync() { var testCode = @"public class Foo @@ -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; } @@ -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() { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs index 5ae5c96a9..07cf9ab2f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs @@ -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; /// /// A C# statement contains parenthesis which are unnecessary and should be removed. @@ -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)