RenameVariable: skip type-reference identifiers in method signatures and casts#7780
Merged
Merged
Conversation
…and casts `isVariableName` only excluded the method-name identifier on `J.MethodDeclaration`, so a return-type identifier matching the variable name was renamed too — turning `Foo create()` into `foo create()` when renaming a field `Foo`. Also extend the audit to `J.InstanceOf`, `J.ArrayType`, `J.Annotation`, and the `J.ControlParentheses` inside `J.TypeCast`, each demonstrated by a failing test.
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.
Summary
RenameVariable$RenameVariableVisitor.isVariableNameonly excluded the method-name identifier in itsJ.MethodDeclarationbranch, so a return-type identifier matching the variable being renamed was treated as a variable reference and rewritten — turningFoo create()intofoo create()when renaming a field namedFoo. This caused downstream recipes likeRenamePrivateFieldsToCamelCase(RenamePrivateFieldsToCamelCase renames a same-named method return type, producing uncompilable code rewrite-static-analysis#902) to produce uncompilable code.Audited the other branches for similar gaps and added exclusions for the type-reference positions that demonstrably regressed:
J.InstanceOf.getClazz(),J.ArrayType.getElementType(),J.Annotation.getAnnotationType(), and theJ.ControlParenthesesinsideJ.TypeCast. Each new exclusion is covered by a test that fails without the corresponding fix; positions where I couldn't reproduce a failure (e.g.J.AnnotatedTypefor simple identifiers, since type-use annotations get folded intoJ.Identifier.annotations) were left alone.Test plan
./gradlew :rewrite-java:test./gradlew :rewrite-java-test:testRenameVariableTestcases each fail before their fix and pass after