From f85916edb042a0f56db0f4e0ee200650f99125f1 Mon Sep 17 00:00:00 2001 From: Gijs de Jong Date: Tue, 1 Aug 2023 15:04:46 +0200 Subject: [PATCH 1/2] Prevent `SuggestedFixes#renameMethod` from modifying return type declaration --- .../errorprone/fixes/SuggestedFixes.java | 6 +++- .../bugpatterns/MemberNameTest.java | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java index 6b2e28d3562..df6e43ce2c3 100644 --- a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java +++ b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java @@ -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 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 + && token.name().equals(tree.getName())) { return SuggestedFix.replace(token.pos(), token.endPos(), replacement); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java index 6e58cfdb57a..a1da16d7349 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java @@ -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() {", + " return null;", + " }", + "", + " void call() {", + " object();", + " }", + "}") + .doTest(); + } } From cbfbc5401eb659a7aef08317250fc20425cf09e7 Mon Sep 17 00:00:00 2001 From: Gijs de Jong Date: Wed, 9 Aug 2023 19:44:26 +0200 Subject: [PATCH 2/2] suggestions --- .../java/com/google/errorprone/fixes/SuggestedFixes.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java index df6e43ce2c3..f448941037b 100644 --- a/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java +++ b/check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java @@ -745,17 +745,14 @@ public Void visitMemberSelect(MemberSelectTree tree, Void unused) { /** Be warned, only changes method name at the declaration. */ public static SuggestedFix renameMethod(MethodTree tree, String replacement, VisitorState state) { - // Search tokens from beginning of method tree to beginning of method body. - int basePos = getStartPosition(tree); + // Search tokens from end of return type tree to beginning of method body. + int basePos = state.getEndPosition(tree.getReturnType()); int endPos = tree.getBody() != null ? getStartPosition(tree.getBody()) : state.getEndPosition(tree); List methodTokens = state.getOffsetTokens(basePos, endPos); - int returnTypeEndPos = state.getEndPosition(tree.getReturnType()); for (ErrorProneToken token : methodTokens) { - if (token.kind() == TokenKind.IDENTIFIER - && token.pos() > returnTypeEndPos - && token.name().equals(tree.getName())) { + if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(tree.getName())) { return SuggestedFix.replace(token.pos(), token.endPos(), replacement); } }