From 840b1d8bb4a9be5d9472ff6d72e012ea8ca0b903 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Wed, 21 Aug 2019 13:56:47 -0700 Subject: [PATCH] Change unused parameters analyzer configuration to analyze generated code This fixes false reports for unused parameters of event handlers that are referenced/invoked from xaml generated code files. Fixes #37483 --- .../RemoveUnusedParametersTests.cs | 40 +++++++++++++++++++ ...edParametersAndValuesDiagnosticAnalyzer.cs | 6 ++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedParametersTests.cs b/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedParametersTests.cs index 5f897900ee281..bace973032761 100644 --- a/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedParametersTests.cs +++ b/src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedParametersTests.cs @@ -1335,6 +1335,46 @@ private void myAction() { } }", options); } + [WorkItem(37483, "https://github.com/dotnet/roslyn/issues/37483")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)] + public async Task MethodUsedAsDelegateInGeneratedCode_NoDiagnostic() + { + await TestDiagnosticMissingAsync( +@"using System; + +public partial class C +{ + private void M(int [|x|]) + { + } +} + +public partial class C +{ + [System.CodeDom.Compiler.GeneratedCodeAttribute("""", """")] + public void M2(out Action a) + { + a = M; + } +} +"); + } + + [WorkItem(37483, "https://github.com/dotnet/roslyn/issues/37483")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)] + public async Task UnusedParameterInGeneratedCode_NoDiagnostic() + { + await TestDiagnosticMissingAsync( +@"public partial class C +{ + [System.CodeDom.Compiler.GeneratedCodeAttribute("""", """")] + private void M(int [|x|]) + { + } +} +"); + } + [WorkItem(36817, "https://github.com/dotnet/roslyn/issues/36817")] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)] public async Task ParameterWithoutName_NoDiagnostic() diff --git a/src/Features/Core/Portable/RemoveUnusedParametersAndValues/AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs b/src/Features/Core/Portable/RemoveUnusedParametersAndValues/AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs index 365abe9eac703..9bf1fc5951ac2 100644 --- a/src/Features/Core/Portable/RemoveUnusedParametersAndValues/AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs +++ b/src/Features/Core/Portable/RemoveUnusedParametersAndValues/AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.cs @@ -181,8 +181,12 @@ void AddEntryCore(UnusedValuePreference preference, bool isUnusedLocalAssignment public override DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SemanticSpanAnalysis; protected sealed override void InitializeWorker(AnalysisContext context) - => context.RegisterCompilationStartAction( + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze); + + context.RegisterCompilationStartAction( compilationContext => SymbolStartAnalyzer.CreateAndRegisterActions(compilationContext, this)); + } private bool TryGetOptions( SyntaxTree syntaxTree,