Skip to content

Do not offer introduce parameter for incomplete calls - #84769

Open
akhera99 wants to merge 3 commits into
dotnet:mainfrom
akhera99:introduce_param_error
Open

Do not offer introduce parameter for incomplete calls#84769
akhera99 wants to merge 3 commits into
dotnet:mainfrom
akhera99:introduce_param_error

Conversation

@akhera99

@akhera99 akhera99 commented Aug 5, 2026

Copy link
Copy Markdown
Member

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

@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.

@akhera99
akhera99 marked this pull request as ready for review August 6, 2026 18:00
@akhera99
akhera99 requested a review from a team as a code owner August 6, 2026 18:00
Copilot AI review requested due to automatic review settings August 6, 2026 18:00
@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.

Copilot AI left a comment

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.

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 ComputeRefactoringsAsync when 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|]);
}
""");
@JoeRobich

Copy link
Copy Markdown
Member

/azp run roslyn-integration-CI

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI review requested due to automatic review settings August 7, 2026 20:51

Copilot AI left a comment

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.

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

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSharpIntroduceParameterCodeRefactoringProvider encountered an error and has been disabled

3 participants