dotnet/roslyn

Invalid IDE0059 when local.ExtensionMethod used as Delegate

Aperta

#42.337 aperta il 11 mar 2020

 (1 commento) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-IDEBugFeature - IDE0059help wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

Version Used: master as of the 10th march

Steps to Reproduce:

using System;
public static class Program {
    public static void Main() {
        var x = 42;
        Func<string> a = x.ToStringExt;
        Console.WriteLine(a());
    }
    
    public static string ToStringExt(this object x) => x.ToString();
}

Expected Behavior: No warning.

Actual Behavior: IDE0059 unnecessary assignment of a value to 'x' Also the suggested codefix 'remove redundant assignment' doesn't compile:

using System;
public static class Program {
    public static void Main() {
        int x;
        Func<string> a = () => x.ToString(); // error CS0165: Use of unassigned local variable 'x'
        Console.WriteLine(a());
    }
}

Guida contributor