dotnet/roslyn

IDE0051 should factor in new dependency attributes in .NET 5

Offen

#48.206 geöffnet am 30.09.2020

 (9 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)C# (4.257 Forks)batch import
Area-IDEConcept-Continuous ImprovementFeature - IDE0051help wanted

Repository-Metriken

Stars
 (20.414 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

Version Used: 3.9.0-1.20480.1+b3c3f4875957e51526000898673291d11708d35b

Steps to Reproduce:

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

class Program
{
    static void Main() => new Program().Bar();

    [DynamicDependency("Foo")]
    private void Bar() => typeof(Program).GetMethod("Foo", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this, null);

    private void Foo() => Console.WriteLine("Hello");
}

Expected Behavior: .NET 5 adds new attributes that enable dependencies not obvious in the code, e.g. those via reflection, to be used for dead code elimination tools like https://github.com/mono/linker to better understand such dependencies. IDE0051 is such a tool and should ideally respect these attributes, such that in this example, IDE0051 doesn't fire.

Actual Behavior: IDE0051 fires and suggests deleting the "unused" method: image

cc: @eerhardt, @nickcraver

Contributor Guide