diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java b/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java index 36be5652cd7..d56d0f4826b 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java @@ -82,7 +82,8 @@ private TypeCompatibilityReport compatibilityOfTypes( } // If they're the exact same type, they are definitely compatible. - if (state.getTypes().isSameType(upperBound(leftType), upperBound(rightType))) { + Types types = state.getTypes(); + if (types.isSameType(upperBound(leftType), upperBound(rightType))) { return TypeCompatibilityReport.compatible(); } @@ -103,7 +104,7 @@ private TypeCompatibilityReport compatibilityOfTypes( // class Bar extends Super // class Foo extends Super // Bar and Foo would least-upper-bound to Super, and we compare String and Integer to each-other - Type commonSupertype = state.getTypes().lub(rightType, leftType); + Type commonSupertype = types.lub(types.erasure(rightType), types.erasure(leftType)); // primitives, etc. can't have a common superclass. if (commonSupertype.getTag().equals(TypeTag.BOT) || commonSupertype.getTag().equals(TypeTag.ERROR)) { diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/EqualsIncompatibleTypeTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/EqualsIncompatibleTypeTest.java index 19b064393e3..5559593f4c8 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/EqualsIncompatibleTypeTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/EqualsIncompatibleTypeTest.java @@ -120,4 +120,35 @@ public void methodReference_comparableTypes_noFinding() { "}") .doTest(); } + + @Test + public void wildcards_whenIncompatible() { + compilationHelper + .addSourceLines( + "Test.java", + "", + "public class Test {", + " public void test(Class a, Class b) {", + " // BUG: Diagnostic contains:", + " a.equals(b);", + " }", + "}") + .doTest(); + } + + @Test + public void unconstrainedWildcard_compatibleWithAnything() { + compilationHelper + .addSourceLines( + "Test.java", + "import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;", + "", + "public class Test {", + " public void test(java.lang.reflect.Method m, Class c) {", + " TestProtoMessage.class.equals(m.getParameterTypes()[0]);", + " TestProtoMessage.class.equals(c);", + " }", + "}") + .doTest(); + } }