dotnet/roslyn

Incorrect IDE0007: Use 'var' instead of explicit type when the type implements multiple instantiations of a generic interface which influence overload resolution

Ouverte

#59 337 ouverte le 7 févr. 2022

 (0 commentaire) (0 réaction) (0 personne assignée)C# (4 257 forks)batch import
Area-IDEBugFeature - IDE0007help wanted

Métriques du dépôt

Stars
 (20 414 étoiles)
Métriques de merge PR
 (Merge moyen 6j 17h) (256 PRs mergées en 30 j)

Description

Version Used: Visual Studio 2022 (17.0.5) C# Tools 4.0.1-1.21568.1+6ab6601178d9fba8c680b56934cd1742e0816bff

Steps to Reproduce:

  1. Set csharp_style_var_when_type_is_apparent = true:suggestion in your .editorconfig
  2. Enter the following code:
public interface ITyped<T>
{
}
public interface IUntyped
    : ITyped<string>
    , ITyped<int>
{
}
public static class Extensions
{
    public static void Run()
    {
        IUntyped o = null;
        o.Invokee(out string value); // HERE
        _ = value;
    }
    public static void Invokee(
        this IUntyped o)
    {
        _ = o;
    }

    public static void Invokee<T>(
        this ITyped<T> o,
        out T value)
    {
        _ = o;
        value = default;
    }
}

Expected Behavior:

No codefix offered on the line marked // HERE.

Actual Behavior:

A diagnostic+codefix “IDE0007: use 'var' instead of explicit type” is offered on the line marked // HERE. Accepting replaces string with `var, breaking compilation. Compilation is broken because, without specifying the type of the parameter, the two conflicting generic interfaces are excluded during method resolution—and if they were not ignored but considered possible, there would be an ambiguity error (but that doesn’t seem to be how C# works).

Guide contributeur