Fix duplicate parameter names in introduce parameter refactoring#81404
Merged
CyrusNajmabadi merged 6 commits intomainfrom Nov 25, 2025
Merged
Fix duplicate parameter names in introduce parameter refactoring#81404CyrusNajmabadi merged 6 commits intomainfrom
CyrusNajmabadi merged 6 commits intomainfrom
Conversation
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix issue with ambiguous parameter names in overloads
Fix duplicate parameter names in introduce parameter refactoring
Nov 21, 2025
| else | ||
| { | ||
| baseName = _semanticFacts.GenerateNameForExpression(semanticModel, _expression, capitalize: false, cancellationToken); | ||
| } |
| _containerMethod, | ||
| _containerMethod, | ||
| baseName, | ||
| existingParameterNames, |
Contributor
There was a problem hiding this comment.
@copilot do you need to pass existingParameterNames along? i think GenerateUniqueName figures it out from the locations you pass in. Also, use GenerateUniqueLocalName. I don't think you need the container if you have the _expression as that's the location we can use to determine the names of things in scope.
Contributor
Author
There was a problem hiding this comment.
Done in cace554. Changed to use GenerateUniqueLocalName with _expression as the location and container: null, following the pattern used in IntroduceVariable. This automatically determines conflicts without needing to pass existing parameter names.
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
akhera99
approved these changes
Nov 25, 2025
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.
Fix "Introduce parameter" duplicate parameter names
This PR fixes an issue where the "Introduce parameter" refactoring could generate duplicate or ambiguous parameter names when the generated name conflicts with an existing parameter.
Problem:
When using the "Introduce parameter" refactoring on an expression like
v + 1in a method that already has a parameter namedv, the refactoring would generate a duplicate parameterv, leading to compiler errors:Solution:
Modified
GetNewParameterNameAsyncinIntroduceParameterDocumentRewriter.csto useISemanticFactsService.GenerateUniqueLocalNameto ensure the generated parameter name doesn't conflict with existing parameters. The method now:GenerateUniqueLocalNamewhich automatically determines conflicts from the expression locationTesting:
Added comprehensive tests covering all three code action options:
Status:
IntroduceParameterDocumentRewriter.csGetNewParameterNameAsyncmethod that generates parameter namesGetNewParameterNameAsyncto useISemanticFactsService.GenerateUniqueLocalNameFixes #81400
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.