Skip to content

Commit

Permalink
Change unused parameters analyzer configuration to analyze generated …
Browse files Browse the repository at this point in the history
…code

This fixes false reports for unused parameters of event handlers that are referenced/invoked from xaml generated code files. Fixes dotnet#37483
  • Loading branch information
mavasani committed Aug 21, 2019
1 parent 5551fad commit 840b1d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 840b1d8

Please sign in to comment.