diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/HeldLockAnalyzer.java b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/HeldLockAnalyzer.java index 0ac30e84b6a5..26b04ef9326e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/HeldLockAnalyzer.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/HeldLockAnalyzer.java @@ -83,7 +83,7 @@ public final class HeldLockAnalyzer { .onDescendantOf("java.util.Map") .namedAnyOf("forEach", "replaceAll", "computeIfAbsent", "computeIfPresent", "merge"), instanceMethod().onDescendantOf("java.util.List").named("replaceAll"), - instanceMethod().onDescendantOf("java.util.Iterable").named("forEach"), + instanceMethod().onDescendantOf("java.lang.Iterable").named("forEach"), instanceMethod().onDescendantOf("java.util.Iterator").named("forEachRemaining"), staticMethod() .onClass("com.google.common.collect.Iterables") diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/threadsafety/GuardedByCheckerTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/threadsafety/GuardedByCheckerTest.java index 28b6653936fa..e402075294fc 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/threadsafety/GuardedByCheckerTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/threadsafety/GuardedByCheckerTest.java @@ -1963,4 +1963,20 @@ public void methodReference_referencedMethodIsFlagged() { "}") .doTest(); } + + @Test + public void lambdaMethodInvokedImmediately_shouldNotBeFlagged() { + compilationHelper + .addSourceLines( + "Test.java", + "import java.util.List;", + "import javax.annotation.concurrent.GuardedBy;", + "class Test {", + " @GuardedBy(\"this\") private final Object o = new Object();", + " public synchronized void test(List xs) {", + " xs.forEach(x -> o.toString());", + " }", + "}") + .doTest(); + } }