Do not offer introduce parameter for incomplete calls - #84769
Open
akhera99 wants to merge 3 commits into
Open
Conversation
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Introduce Parameter refactoring to avoid offering the refactoring when the selected expression is inside an invocation that is missing required arguments, and hardens call-site rewriting to tolerate malformed argument lists without throwing.
Changes:
- Add an early bail-out in
ComputeRefactoringsAsyncwhen the containing invocation is missing required arguments. - Clamp out-of-range insertion indices when adding arguments during call-site rewrites, appending a named argument in malformed cases.
- Add a regression test covering the missing-required-argument scenario.
Show a summary per file
| File | Description |
|---|---|
| src/Features/CSharpTest/IntroduceParameter/IntroduceParameterTests.cs | Adds a regression test ensuring the refactoring is not offered for an invocation missing required arguments. |
| src/Features/Core/Portable/IntroduceParameter/IntroduceParameterDocumentRewriter.cs | Makes argument insertion resilient when insertionIndex exceeds the current argument count by appending a named argument. |
| src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs | Adds detection of missing required arguments for the containing invocation and skips offering the refactoring in that case. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 2
Comment on lines
+173
to
+194
| foreach (var parameter in method.Parameters) | ||
| { | ||
| if (parameter.IsOptional || parameter.IsParams) | ||
| continue; | ||
|
|
||
| var supplied = false; | ||
| foreach (var argument in arguments) | ||
| { | ||
| var argumentParameter = semanticFacts.FindParameterForArgument( | ||
| semanticModel, argument, allowUncertainCandidates: true, allowParams: true, cancellationToken); | ||
| if (argumentParameter?.Ordinal == parameter.Ordinal) | ||
| { | ||
| supplied = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!supplied) | ||
| return true; | ||
| } | ||
|
|
||
| return false; |
Comment on lines
+1089
to
+1096
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/82049")] | ||
| public Task TestMissingWithMissingRequiredArgument() | ||
| => TestMissingAsync(""" | ||
| void M(int a, int b) | ||
| { | ||
| M([|1|]); | ||
| } | ||
| """); |
Member
|
/azp run roslyn-integration-CI |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Contributor
There was a problem hiding this comment.
Copilot's findings
Suppressed comments (2)
src/Features/CSharpTest/IntroduceParameter/IntroduceParameterTests.cs:1091
- Test name has a duplicated/awkward phrasing ("MissingWithMissing"). Renaming will make it easier to understand and keep naming consistent with other tests in this file.
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/82049")]
public Task TestMissingWithMissingRequiredArgument()
=> TestMissingInRegularAndScriptAsync("""
src/Features/Core/Portable/IntroduceParameter/AbstractIntroduceParameterCodeRefactoringProvider.cs:180
- HasMissingRequiredArguments uses FindParameterForArgument with allowUncertainCandidates:true, which can return a parameter from a different candidate symbol than the IMethodSymbol chosen via GetAnySymbol(). That can incorrectly mark parameters as "supplied" (masking missing required args) when overload resolution is ambiguous/broken. Only treat an argument as supplying a parameter if that parameter belongs to the selected method symbol.
foreach (var argument in arguments)
{
var argumentParameter = semanticFacts.FindParameterForArgument(
semanticModel, argument, allowUncertainCandidates: true, allowParams: true, cancellationToken);
if (argumentParameter is not null)
suppliedParameters[argumentParameter.Ordinal] = true;
- Files reviewed: 3/3 changed files
- Comments generated: 0 new
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #82049
Prevents introduce parameter from being offered when the selected invocation is missing required argument and add handling for malformed call sites, if discovered later.
Microsoft Reviewers: Open in CodeFlow