diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java index fee7bfe0284..3fe13f366fc 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java @@ -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)); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryLambdaTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryLambdaTest.java index bde995a9171..69e16564851 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryLambdaTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryLambdaTest.java @@ -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 p) {}", + " public void test() {", + " foo(E.ELEM.pred());", + " }", + " private enum E {", + " ELEM;", + " Predicate pred() {", + " return o -> true;", + " }", + " }", + "}") + .addOutputLines( + "Test.java", + "import java.util.function.Predicate;", + "class Test {", + " private void foo(Predicate p) {}", + " public void test() {", + " foo(E.ELEM::pred);", + " }", + " private enum E {", + " ELEM;", + " boolean pred(Object o) {", + " return true;", + " }", + " }", + "}") + .doTest(); + } }