Repository navigation
Don't offer IDE0340 when the unbound nameof wouldn't bind - #86579
Manish-1kumar wants to merge 2 commits into
Conversation
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
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
|
The two failures are in |
| var speculationAnalyzer = new SpeculationAnalyzer( | ||
| invocation, ConvertToUnboundGenericNameof(invocation), semanticModel, cancellationToken); | ||
| if (speculationAnalyzer.SpeculativeSemanticModel is not { } speculativeSemanticModel || | ||
| speculationAnalyzer.ReplacedExpression is not InvocationExpressionSyntax { ArgumentList.Arguments: [var argument] }) |
There was a problem hiding this comment.
when do either of these not succeed?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
basically, in what circumstances do we hit this path?
| return speculativeSemanticModel.GetSymbolInfo(argument.Expression, cancellationToken).GetAnySymbol() != null; | ||
| } | ||
|
|
||
| public static TSyntax ConvertToUnboundGenericNameof<TSyntax>(TSyntax syntax) |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Moved it to UseUnboundGenericTypeInNameOfHelpers, used by the analyzer and both code fixes.
…culation fallback
Fixes #86572
nameof(Holder<string>.Value.Length)was offered asnameof(Holder<>.Value.Length), which doesn't compile whenValueis aT: members reached through an unbound generic type only have the members ofobjectand of the type parameters' constraints.The analyzer now speculatively binds the rewritten
nameofargument and only reports when it still binds.ConvertToUnboundGenericNameofmoved from the code fix to the analyzer so both can use it, the same wayConvertToImplicitlyTypedLambdais shared.Microsoft Reviewers: Open in CodeFlow