From e82c86b00c49433ccc847f050e1be199194638bc Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Fri, 15 Sep 2023 11:14:33 -0700 Subject: [PATCH] Skip UnnecessaryLambda findings for usages in enhanced for loops https://github.com/google/error-prone/issues/2518 PiperOrigin-RevId: 565728375 --- .../bugpatterns/UnnecessaryLambda.java | 14 +++++++++++- .../bugpatterns/UnnecessaryLambdaTest.java | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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 be9332b94798..1cad68ba1216 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryLambda.java @@ -43,6 +43,7 @@ import com.google.errorprone.matchers.Description; import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.BlockTree; +import com.sun.source.tree.EnhancedForLoopTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; import com.sun.source.tree.LambdaExpressionTree; @@ -101,15 +102,26 @@ public Description matchMethod(MethodTree tree, VisitorState state) { if (state.isAndroidCompatible()) { return NO_MATCH; } + boolean[] usedInEnhancedForLoop = {false}; new TreePathScanner() { @Override public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { + if (Objects.equals(getSymbol(node), sym)) { - replaceUseWithMethodReference(fix, node, name, state.withPath(getCurrentPath())); + Tree parent = getCurrentPath().getParentPath().getLeaf(); + if (parent instanceof EnhancedForLoopTree + && ((EnhancedForLoopTree) parent).getExpression().equals(node)) { + usedInEnhancedForLoop[0] = true; + } else { + replaceUseWithMethodReference(fix, node, name, state.withPath(getCurrentPath())); + } } return super.visitMethodInvocation(node, null); } }.scan(state.getPath().getCompilationUnit(), null); + if (usedInEnhancedForLoop[0]) { + return NO_MATCH; + } lambdaToMethod(state, lambda, fix, name, type); return describeMatch(tree, fix.build()); } 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 69e16564851b..f80bb0eb527a 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryLambdaTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryLambdaTest.java @@ -388,4 +388,26 @@ public void e() { "}") .doTest(); } + + @Test + public void iterable() { + testHelper + .addInputLines( + "Test.java", + "import java.util.stream.IntStream;", + "import java.util.ArrayList;", + "import java.util.stream.IntStream;", + "class Example {", + " void someLoopyCode() {", + " for (int i : someIterable()) {", + " // Do something.", + " }", + " }", + " private Iterable someIterable() {", + " return () -> IntStream.range(0, 42).boxed().iterator();", + " }", + "}") + .expectUnchanged() + .doTest(); + } }