Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent SuggestedFixes#renameMethod from modifying return type declaration #4043

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,12 @@ public static SuggestedFix renameMethod(MethodTree tree, String replacement, Vis
int endPos =
tree.getBody() != null ? getStartPosition(tree.getBody()) : state.getEndPosition(tree);
List<ErrorProneToken> methodTokens = state.getOffsetTokens(basePos, endPos);

int returnTypeEndPos = state.getEndPosition(tree.getReturnType());
for (ErrorProneToken token : methodTokens) {
if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(tree.getName())) {
if (token.kind() == TokenKind.IDENTIFIER
&& token.pos() > returnTypeEndPos
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking the token positions here, I think we might be able to adjust basePos earlier and only tokenize from after the return type. Do you see any issues with that approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch thanks! Will push a commit.

&& token.name().equals(tree.getName())) {
return SuggestedFix.replace(token.pos(), token.endPos(), replacement);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,32 @@ public void methodReference() {
"}")
.doTest();
}

@Test
public void methodNameWithMatchingReturnType() {
refactoringHelper
.addInputLines(
"Test.java",
"class Test {",
" private Object Object() {",
" return null;",
" }",
"",
" void call() {",
" Object();",
" }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" private Object object() {",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other reviewers: before this fix Error Prone would suggest:

Suggested change
" private Object object() {",
" private object Object() {",

" return null;",
" }",
"",
" void call() {",
" object();",
" }",
"}")
.doTest();
}
}