Skip to content

Don't offer IDE0340 when the unbound nameof wouldn't bind - #86579

Open
Manish-1kumar wants to merge 2 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/ide0340-unbound-member-chain
Open

Manish-1kumar wants to merge 2 commits into
dotnet:mainfrom
Manish-1kumar:users/Manish-1kumar/ide0340-unbound-member-chain

Conversation

@Manish-1kumar

@Manish-1kumar Manish-1kumar commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #86572

nameof(Holder<string>.Value.Length) was offered as nameof(Holder<>.Value.Length), which doesn't compile when Value is a T: members reached through an unbound generic type only have the members of object and of the type parameters' constraints.

The analyzer now speculatively binds the rewritten nameof argument and only reports when it still binds. ConvertToUnboundGenericNameof moved from the code fix to the analyzer so both can use it, the same way ConvertToImplicitlyTypedLambda is shared.

Microsoft Reviewers: Open in CodeFlow

Members reached through an unbound generic type only have the members of
object and of the type parameters' constraints, so converting
nameof(Holder<string>.Value.Length) to nameof(Holder<>.Value.Length) breaks
the build when Value is a T. Only report when the rewritten nameof binds.

Fixes dotnet#86572
@Manish-1kumar
Manish-1kumar requested a review from a team as a code owner November 22, 2026 17:53
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode labels Nov 22, 2026
@Manish-1kumar

Copy link
Copy Markdown
Author

The two failures are in LanguageServerDaemonTests (Daemon_SharesGreenSyntaxTreesAcrossServers and Daemon_StartupFailure_DaemonSurvivesAndContinuesAccepting), which this PR doesn't touch, and the same legs pass on #86580. Could someone re-run them?

var speculationAnalyzer = new SpeculationAnalyzer(
invocation, ConvertToUnboundGenericNameof(invocation), semanticModel, cancellationToken);
if (speculationAnalyzer.SpeculativeSemanticModel is not { } speculativeSemanticModel ||
speculationAnalyzer.ReplacedExpression is not InvocationExpressionSyntax { ArgumentList.Arguments: [var argument] })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when do either of these not succeed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither should fail for a real nameof: there's always a statement, initializer, attribute or arrow clause to speculate on, and the argument check was only for error code like nameof(List<int>, x). I removed the fallback and check for a single argument up front instead.

if (speculationAnalyzer.SpeculativeSemanticModel is not { } speculativeSemanticModel ||
speculationAnalyzer.ReplacedExpression is not InvocationExpressionSyntax { ArgumentList.Arguments: [var argument] })
{
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically, in what circumstances do we hit this path?

return speculativeSemanticModel.GetSymbolInfo(argument.Expression, cancellationToken).GetAnySymbol() != null;
}

public static TSyntax ConvertToUnboundGenericNameof<TSyntax>(TSyntax syntax)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we already have something that does this? what does the codefixprovider do to make the final pice of syntax?

return current;
});
editor.ReplaceNode(
nameofInvocation, CSharpUseUnboundGenericTypeInNameOfDiagnosticAnalyzer.ConvertToUnboundGenericNameof(nameofInvocation));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh. i see what you did. in this case, we put this in a helpers type. that would be my preference here vs the provider calling into the analyzer.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to UseUnboundGenericTypeInNameOfHelpers, used by the analyzer and both code fixes.

This branch has not been deployed

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

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. VSCode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants