From 932604d0bebd3723bb3b51ef870e74a059d6130b Mon Sep 17 00:00:00 2001 From: TillW <14997061+x789@users.noreply.github.com> Date: Tue, 13 Dec 2022 11:45:04 +0100 Subject: [PATCH] Remove CA2109 (#5974) --- .../CSharpReviewVisibleEventHandlers.Fixer.cs | 17 -- .../Core/AnalyzerReleases.Unshipped.md | 6 + .../ReviewVisibleEventHandlers.Fixer.cs | 29 --- .../ReviewVisibleEventHandlers.cs | 66 ----- .../Microsoft.CodeAnalysis.NetAnalyzers.md | 12 - .../Microsoft.CodeAnalysis.NetAnalyzers.sarif | 21 -- .../ReviewVisibleEventHandlersTests.cs | 246 ------------------ .../BasicReviewVisibleEventHandlers.Fixer.vb | 17 -- 8 files changed, 6 insertions(+), 408 deletions(-) delete mode 100644 src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/QualityGuidelines/CSharpReviewVisibleEventHandlers.Fixer.cs delete mode 100644 src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.Fixer.cs delete mode 100644 src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.cs delete mode 100644 src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlersTests.cs delete mode 100644 src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/QualityGuidelines/BasicReviewVisibleEventHandlers.Fixer.vb diff --git a/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/QualityGuidelines/CSharpReviewVisibleEventHandlers.Fixer.cs b/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/QualityGuidelines/CSharpReviewVisibleEventHandlers.Fixer.cs deleted file mode 100644 index 8fdf29e8e0..0000000000 --- a/src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/QualityGuidelines/CSharpReviewVisibleEventHandlers.Fixer.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. - -using System.Composition; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeQuality.Analyzers.QualityGuidelines; - -namespace Microsoft.CodeQuality.CSharp.Analyzers.QualityGuidelines -{ - /// - /// CA2109: Review visible event handlers - /// - [ExportCodeFixProvider(LanguageNames.CSharp), Shared] - public sealed class CSharpReviewVisibleEventHandlersFixer : ReviewVisibleEventHandlersFixer - { - } -} \ No newline at end of file diff --git a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md index 27e48718d7..cc57137b64 100644 --- a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md @@ -10,3 +10,9 @@ CA1512 | Maintainability | Info | UseExceptionThrowHelpers, [Documentation](http CA1513 | Maintainability | Info | UseExceptionThrowHelpers, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1513) CA1856 | Performance | Error | ConstantExpectedAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1856) CA1857 | Performance | Warning | ConstantExpectedAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1857) + +### Removed Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +CA2109 | Security | Disabled | ReviewVisibleEventHandlersAnalyzer, [Documentation](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2109) diff --git a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.Fixer.cs b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.Fixer.cs deleted file mode 100644 index 62681cfdca..0000000000 --- a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.Fixer.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. - -using System.Collections.Immutable; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CodeFixes; - -namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines -{ - /// - /// CA2109: Review visible event handlers - /// - public abstract class ReviewVisibleEventHandlersFixer : CodeFixProvider - { - public sealed override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Empty; - - public sealed override FixAllProvider GetFixAllProvider() - { - // See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers - return WellKnownFixAllProviders.BatchFixer; - } - - public sealed override Task RegisterCodeFixesAsync(CodeFixContext context) - { - // Fixer not yet implemented. - return Task.CompletedTask; - - } - } -} \ No newline at end of file diff --git a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.cs b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.cs deleted file mode 100644 index b234fcbdde..0000000000 --- a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlers.cs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. - -using System.Collections.Immutable; -using Analyzer.Utilities; -using Analyzer.Utilities.Extensions; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; - -namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines -{ - using static MicrosoftCodeQualityAnalyzersResources; - - /// - /// CA2109: - /// - [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] - public sealed class ReviewVisibleEventHandlersAnalyzer : DiagnosticAnalyzer - { - internal const string RuleId = "CA2109"; - - internal static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( - RuleId, - CreateLocalizableResourceString(nameof(ReviewVisibleEventHandlersTitle)), - CreateLocalizableResourceString(nameof(ReviewVisibleEventHandlersMessageDefault)), - DiagnosticCategory.Security, - RuleLevel.Disabled, - description: CreateLocalizableResourceString(nameof(ReviewVisibleEventHandlersDescription)), - isPortedFxCopRule: true, - isDataflowRule: false); - - public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); - - public override void Initialize(AnalysisContext context) - { - context.EnableConcurrentExecution(); - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); - - context.RegisterCompilationStartAction(context => - { - var eventArgsType = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemEventArgs); - - context.RegisterSymbolAction(context => - { - var method = (IMethodSymbol)context.Symbol; - - if (!method.IsExternallyVisible()) - { - return; - } - - if (!method.HasEventHandlerSignature(eventArgsType)) - { - return; - } - - if (method.IsOverride || method.IsImplementationOfAnyInterfaceMember()) - { - return; - } - - context.ReportDiagnostic(method.CreateDiagnostic(Rule, method.Name)); - }, SymbolKind.Method); - }); - } - } -} \ No newline at end of file diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md index 098d2f15a1..f39d1a13e9 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.md @@ -1860,18 +1860,6 @@ A platform invoke member allows partially trusted callers, has a string paramete |CodeFix|True| --- -## [CA2109](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2109): Review visible event handlers - -A public or protected event-handling method was detected. Event-handling methods should not be exposed unless absolutely necessary. - -|Item|Value| -|-|-| -|Category|Security| -|Enabled|False| -|Severity|Warning| -|CodeFix|False| ---- - ## [CA2119](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2119): Seal methods that satisfy private interfaces An inheritable public type provides an overridable method implementation of an internal (Friend in Visual Basic) interface. To fix a violation of this rule, prevent the method from being overridden outside the assembly. diff --git a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif index 2e18b39eec..0a572bfd80 100644 --- a/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif +++ b/src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif @@ -3361,27 +3361,6 @@ ] } }, - "CA2109": { - "id": "CA2109", - "shortDescription": "Review visible event handlers", - "fullDescription": "A public or protected event-handling method was detected. Event-handling methods should not be exposed unless absolutely necessary.", - "defaultLevel": "warning", - "helpUri": "https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2109", - "properties": { - "category": "Security", - "isEnabledByDefault": false, - "typeName": "ReviewVisibleEventHandlersAnalyzer", - "languages": [ - "C#", - "Visual Basic" - ], - "tags": [ - "PortedFromFxCop", - "Telemetry", - "EnabledRuleInAggressiveMode" - ] - } - }, "CA2119": { "id": "CA2119", "shortDescription": "Seal methods that satisfy private interfaces", diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlersTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlersTests.cs deleted file mode 100644 index a0a8a7bc8d..0000000000 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ReviewVisibleEventHandlersTests.cs +++ /dev/null @@ -1,246 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. - -using System.Threading.Tasks; -using Xunit; -using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< - Microsoft.CodeQuality.Analyzers.QualityGuidelines.ReviewVisibleEventHandlersAnalyzer, - Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; -using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier< - Microsoft.CodeQuality.Analyzers.QualityGuidelines.ReviewVisibleEventHandlersAnalyzer, - Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; - -namespace Microsoft.CodeQuality.Analyzers.QualityGuidelines.UnitTests -{ - public class ReviewVisibleEventHandlersTests - { - [Fact] - public async Task CA2109_PublicEventHandler_DiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class Program -{ - public void Handler1(object sender, EventArgs args) {} - protected void Handler2(object sender, EventArgs args) {} -}", -#pragma warning disable RS0030 // Do not use banned APIs - VerifyCS.Diagnostic().WithLocation(6, 17).WithArguments("Handler1"), -#pragma warning restore RS0030 // Do not use banned APIs -#pragma warning disable RS0030 // Do not use banned APIs - VerifyCS.Diagnostic().WithLocation(7, 20).WithArguments("Handler2")); -#pragma warning restore RS0030 // Do not use banned APIs - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class Program - Public Sub Handler1(ByVal sender As Object, ByVal args As EventArgs) - End Sub - - Protected Sub Handler2(ByVal sender As Object, ByVal args As EventArgs) - End Sub -End Class", -#pragma warning disable RS0030 // Do not use banned APIs - VerifyVB.Diagnostic().WithLocation(5, 16).WithArguments("Handler1"), -#pragma warning restore RS0030 // Do not use banned APIs -#pragma warning disable RS0030 // Do not use banned APIs - VerifyVB.Diagnostic().WithLocation(8, 19).WithArguments("Handler2")); -#pragma warning restore RS0030 // Do not use banned APIs - } - - [Fact] - public async Task CA2109_PublicEventHandlerWithSecurityAttribute_DiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; -using System.Security.Permissions; - -public class Program -{ - [SecurityPermissionAttribute(SecurityAction.Demand, UnmanagedCode=true)] - public void [|Handler1|](object sender, EventArgs args) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System -Imports System.Security.Permissions - -Public Class Program - - Public Sub [|Handler1|](ByVal sender As Object, ByVal args As EventArgs) - End Sub -End Class"); - } - - [Fact] - public async Task CA2109_PublicEventHandlerUWP_DiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -namespace Windows.UI.Xaml -{ - public class RoutedEventArgs {} -} - -public class Program -{ - public void [|Handler1|](object sender, Windows.UI.Xaml.RoutedEventArgs args) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Namespace Windows.UI.Xaml - Public Class RoutedEventArgs - End Class -End Namespace - -Public Class Program - Public Sub [|Handler1|](ByVal sender As Object, ByVal args As Windows.UI.Xaml.RoutedEventArgs) - End Sub -End Class"); - } - - [Fact] - public async Task CA2109_PrivateInternalEventHandler_NoDiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class Program -{ - private void Handler1(object sender, EventArgs args) {} - internal void Handler2(object sender, EventArgs args) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class Program - Private Sub Handler1(ByVal sender As Object, ByVal args As EventArgs) - End Sub - - Friend Sub Handler2(ByVal sender As Object, ByVal args As EventArgs) - End Sub -End Class"); - } - - [Fact] - public async Task CA2109_PublicProtectedNotEventHandler_NoDiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class Program -{ - public void Handler1(object sender) {} - public void Handler2(object sender, object o) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class Program - Public Sub Handler1(ByVal sender As Object) - End Sub - - Public Sub Handler2(ByVal sender As Object, ByVal o As Object) - End Sub -End Class"); - } - - [Fact] - public async Task CA2109_PublicOverrideVirtualEventHandler_NoDiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public class A -{ - public virtual void [|Handler1|](object sender, EventArgs args) {} -} - -public class B : A -{ - public override void Handler1(object sender, EventArgs args) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Class A - Public Overridable Sub [|Handler1|](ByVal sender As Object, ByVal args As EventArgs) - End Sub -End Class - -Public Class B - Inherits A - - Public Overrides Sub Handler1(ByVal sender As Object, ByVal args As EventArgs) - End Sub -End Class -"); - } - - [Fact] - public async Task CA2109_PublicOverrideAbstractEventHandler_NoDiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public abstract class A -{ - public abstract void [|Handler1|](object sender, EventArgs args); -} - -public class B : A -{ - public override void Handler1(object sender, EventArgs args) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public MustInherit Class A - Public MustOverride Sub [|Handler1|](ByVal sender As Object, ByVal args As EventArgs) -End Class - -Public Class B - Inherits A - - Public Overrides Sub Handler1(ByVal sender As Object, ByVal args As EventArgs) - End Sub -End Class -"); - } - - [Fact] - public async Task CA2109_PublicInterfaceImplementationEventHandler_NoDiagnosticAsync() - { - await VerifyCS.VerifyAnalyzerAsync(@" -using System; - -public interface IA -{ - void [|Handler1|](object sender, EventArgs args); -} - -public class B : IA -{ - public void Handler1(object sender, EventArgs args) {} -}"); - - await VerifyVB.VerifyAnalyzerAsync(@" -Imports System - -Public Interface IA - Sub [|Handler1|](ByVal sender As Object, ByVal args As EventArgs) -End Interface - -Public Class B - Implements IA - - Public Sub Handler1(ByVal sender As Object, ByVal args As EventArgs) Implements IA.Handler1 - End Sub -End Class"); - } - } -} \ No newline at end of file diff --git a/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/QualityGuidelines/BasicReviewVisibleEventHandlers.Fixer.vb b/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/QualityGuidelines/BasicReviewVisibleEventHandlers.Fixer.vb deleted file mode 100644 index c309634f13..0000000000 --- a/src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/QualityGuidelines/BasicReviewVisibleEventHandlers.Fixer.vb +++ /dev/null @@ -1,17 +0,0 @@ -' Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. - -Imports System.Composition -Imports Microsoft.CodeAnalysis -Imports Microsoft.CodeAnalysis.CodeFixes -Imports Microsoft.CodeQuality.Analyzers.QualityGuidelines - -Namespace Microsoft.CodeQuality.VisualBasic.Analyzers.QualityGuidelines - ''' - ''' CA2109: Review visible event handlers - ''' - - Public NotInheritable Class BasicReviewVisibleEventHandlersFixer - Inherits ReviewVisibleEventHandlersFixer - - End Class -End Namespace