Skip to content

Commit

Permalink
Fix a false positive when comparing protos.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 384457967
  • Loading branch information
graememorgan authored and Error Prone Team committed Jul 13, 2021
1 parent 960c32a commit e08936b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -103,7 +104,7 @@ private TypeCompatibilityReport compatibilityOfTypes(
// class Bar extends Super<String>
// class Foo extends Super<Integer>
// 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Integer> a, Class<? extends String> 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();
}
}

0 comments on commit e08936b

Please sign in to comment.