Skip to content

Commit 6a7ff67

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Make ComputeIfAbsentAmbiguousReference.COMPUTE_IF_ABSENT more specific.
Amusing bug. Fixes external #4736. PiperOrigin-RevId: 711698771
1 parent ed0ff42 commit 6a7ff67

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/ComputeIfAbsentAmbiguousReference.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
public final class ComputeIfAbsentAmbiguousReference extends BugChecker
5151
implements MethodInvocationTreeMatcher {
5252
private static final Matcher<ExpressionTree> COMPUTE_IF_ABSENT =
53-
instanceMethod().onDescendantOf("java.util.Map").named("computeIfAbsent");
53+
instanceMethod()
54+
.onDescendantOf("java.util.Map")
55+
.named("computeIfAbsent")
56+
.withParameters("java.lang.Object", "java.util.function.Function");
5457

5558
@Override
5659
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {

core/src/test/java/com/google/errorprone/bugpatterns/ComputeIfAbsentAmbiguousReferenceTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,25 @@ private void doWork(Map<Integer, HashMap> map) {
126126
""")
127127
.doTest();
128128
}
129+
130+
@Test
131+
public void negativeCase_nonOverride() {
132+
compilationHelper
133+
.addSourceLines(
134+
"in/Test.java",
135+
"""
136+
import java.util.Map;
137+
138+
class Test {
139+
interface SubMap extends Map<Object, Object> {
140+
Object computeIfAbsent(Object key);
141+
}
142+
143+
private void test(SubMap map) {
144+
map.computeIfAbsent(4L);
145+
}
146+
}
147+
""")
148+
.doTest();
149+
}
129150
}

0 commit comments

Comments
 (0)