Skip to content

Commit

Permalink
UnnecessaryLambda: keep the receiver of the method we're replacing _i…
Browse files Browse the repository at this point in the history
…f one exists_.

Not sure this is correct in 100% of cases, but it fixes the new test without breaking the old tests. :)

PiperOrigin-RevId: 482201874
  • Loading branch information
graememorgan authored and Error Prone Team committed Oct 19, 2022
1 parent 6d82c80 commit 0087e31
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,13 @@ private static void replaceUseWithMethodReference(
(receiver != null ? "." : "") + newName);
} else {
Symbol sym = getSymbol(node);
fix.replace(
node,
String.format(
"%s::%s", isStatic(sym) ? sym.owner.enclClass().getSimpleName() : "this", newName));
String receiverCode;
if (node instanceof MethodInvocationTree && getReceiver(node) != null) {
receiverCode = state.getSourceForNode(getReceiver(node));
} else {
receiverCode = isStatic(sym) ? sym.owner.enclClass().getSimpleName().toString() : "this";
}
fix.replace(node, String.format("%s::%s", receiverCode, newName));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,40 @@ public void producesIgnored() {
.expectUnchanged()
.doTest();
}

@Test
public void e() {
testHelper
.addInputLines(
"Test.java",
"import java.util.function.Predicate;",
"class Test {",
" private void foo(Predicate<Object> p) {}",
" public void test() {",
" foo(E.ELEM.pred());",
" }",
" private enum E {",
" ELEM;",
" Predicate<Object> pred() {",
" return o -> true;",
" }",
" }",
"}")
.addOutputLines(
"Test.java",
"import java.util.function.Predicate;",
"class Test {",
" private void foo(Predicate<Object> p) {}",
" public void test() {",
" foo(E.ELEM::pred);",
" }",
" private enum E {",
" ELEM;",
" boolean pred(Object o) {",
" return true;",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 0087e31

Please sign in to comment.