Skip to content

IDE0340 "Use unbound generic type" breaks the build when nameof goes through a member typed by a type parameterΒ #86572

Open

Description

@Manish-1kumar

Version Used: .NET SDK 10.0.401 (compiler and CodeStyle 5.9.0-1.26423.113); the analyzer is unchanged on main.

Steps to Reproduce:

public class Holder<T>
{
    public T Value = default!;
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine(nameof(Holder<string>.Value.Length));
    }
}
  1. Build with dotnet_diagnostic.IDE0340.severity = warning: warning IDE0340: Use unbound generic type.
  2. Apply the fix (dotnet format style --diagnostics IDE0340). The line becomes nameof(Holder<>.Value.Length).
  3. Build again.

Expected Behavior: IDE0340 isn't offered, since Value has type T once the type is unbound and T has no Length.

Actual Behavior: after the fix the build fails with error CS1061: 'T' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'T' could be found.

The same happens with properties (Holder<string>.Prop.Length) and nested generics (Holder<int>.Inner<string>.Y.Length). nameof(List<int>.Count) and chains through constrained type parameters convert correctly.

Cause: CSharpUseUnboundGenericTypeInNameOfDiagnosticAnalyzer only checks the syntax: it reports for any TypeArgumentListSyntax with non-omitted arguments inside nameof, without checking that the unbound form still binds. The C# 14 proposal notes that members of an unbound type have no type information beyond object and the constraints, so the error after the fix is expected language behavior. The tests in UseUnboundGenericTypeInNameOfTests only cover List<>, Dictionary<,> and Outer<>.Inner<>.

Possible fix: bind the unbound form speculatively and only report when it binds without errors (or bail when a member access in the chain has a type that uses a type parameter of a type being unbound), plus tests for both cases. I'd be glad to send a PR if that sounds right.

Activity

  1. CyrusNajmabadi commented on Nov 22, 2026

    @CyrusNajmabadi
    Contributor

    This is very odd. Why not do nameof(string.Length).

  2. Manish-1kumar commented on Nov 22, 2026

    @Manish-1kumar
    Author

    Fair point, the repro is minimized, and for that exact line nameof(string.Length) is simpler. The chained form is used when the name should stay tied to a specific path, e.g. nameof(Envelope<Order>.Payload.Total) in a validation message or log key: renaming Payload or changing its type then breaks the build instead of leaving a stale string.

    Either way, the issue is narrower than the pattern itself: IDE0340 (and Fix All) turns code that compiles into a CS1061 error. Not offering the diagnostic when the unbound form doesn't bind would be a small change, and I'm happy to send it. If you don't think it's worth it, closing is fine too.

  3. CyrusNajmabadi commented on Nov 22, 2026

    @CyrusNajmabadi
    Contributor

    If you don't think it's worth it, closing is fine too.

    depends on the fix. can you make one. if ti's very narrow in terms of what it does, and it solves this, i'm ok with it :)

  4. Manish-1kumar commented on Nov 22, 2026

    @Manish-1kumar
    Author

    Will do. I'll keep it to the analyzer: only report IDE0340 when the rewritten nameof still binds, with tests for the failing chains and for the cases that should still be offered (List<>.Count, chains through constrained type parameters).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions