Skip to content

Commit e08936b

Browse files
graememorganError Prone Team
authored andcommitted
Fix a false positive when comparing protos.
PiperOrigin-RevId: 384457967
1 parent 960c32a commit e08936b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ private TypeCompatibilityReport compatibilityOfTypes(
8282
}
8383

8484
// If they're the exact same type, they are definitely compatible.
85-
if (state.getTypes().isSameType(upperBound(leftType), upperBound(rightType))) {
85+
Types types = state.getTypes();
86+
if (types.isSameType(upperBound(leftType), upperBound(rightType))) {
8687
return TypeCompatibilityReport.compatible();
8788
}
8889

@@ -103,7 +104,7 @@ private TypeCompatibilityReport compatibilityOfTypes(
103104
// class Bar extends Super<String>
104105
// class Foo extends Super<Integer>
105106
// Bar and Foo would least-upper-bound to Super, and we compare String and Integer to each-other
106-
Type commonSupertype = state.getTypes().lub(rightType, leftType);
107+
Type commonSupertype = types.lub(types.erasure(rightType), types.erasure(leftType));
107108
// primitives, etc. can't have a common superclass.
108109
if (commonSupertype.getTag().equals(TypeTag.BOT)
109110
|| commonSupertype.getTag().equals(TypeTag.ERROR)) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,35 @@ public void methodReference_comparableTypes_noFinding() {
120120
"}")
121121
.doTest();
122122
}
123+
124+
@Test
125+
public void wildcards_whenIncompatible() {
126+
compilationHelper
127+
.addSourceLines(
128+
"Test.java",
129+
"",
130+
"public class Test {",
131+
" public void test(Class<? extends Integer> a, Class<? extends String> b) {",
132+
" // BUG: Diagnostic contains:",
133+
" a.equals(b);",
134+
" }",
135+
"}")
136+
.doTest();
137+
}
138+
139+
@Test
140+
public void unconstrainedWildcard_compatibleWithAnything() {
141+
compilationHelper
142+
.addSourceLines(
143+
"Test.java",
144+
"import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;",
145+
"",
146+
"public class Test {",
147+
" public void test(java.lang.reflect.Method m, Class<?> c) {",
148+
" TestProtoMessage.class.equals(m.getParameterTypes()[0]);",
149+
" TestProtoMessage.class.equals(c);",
150+
" }",
151+
"}")
152+
.doTest();
153+
}
123154
}

0 commit comments

Comments
 (0)