Skip to content

Commit 5fef6e0

Browse files
graememorganError Prone Team
authored andcommitted
Yet another JUnitIncompatibleType crash fix.
Fixes external #4377. PiperOrigin-RevId: 638595224
1 parent c2df1b6 commit 5fef6e0

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/JUnitIncompatibleType.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static com.google.errorprone.matchers.Matchers.anyOf;
2424
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
2525
import static com.google.errorprone.util.ASTHelpers.getSymbol;
26+
import static com.google.errorprone.util.ASTHelpers.getUpperBound;
2627
import static com.google.errorprone.util.ASTHelpers.isSameType;
2728

2829
import com.google.errorprone.BugPattern;
@@ -79,10 +80,15 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
7980
var typeB = ignoringCasts(arguments.get(skip + 1), state);
8081
return checkCompatibility(tree, typeA, typeB, state);
8182
} else if (ASSERT_ARRAY_EQUALS.matches(tree, state)) {
83+
8284
int skip = argumentsToSkip(tree, state);
83-
var typeA = ((ArrayType) ignoringCasts(arguments.get(skip), state)).elemtype;
84-
var typeB = ((ArrayType) ignoringCasts(arguments.get(skip + 1), state)).elemtype;
85-
return checkCompatibility(tree, typeA, typeB, state);
85+
var expected = getUpperBound(ignoringCasts(arguments.get(skip), state), state.getTypes());
86+
var actual = getUpperBound(ignoringCasts(arguments.get(skip + 1), state), state.getTypes());
87+
if (!(expected instanceof ArrayType) || !(actual instanceof ArrayType)) {
88+
return NO_MATCH;
89+
}
90+
return checkCompatibility(
91+
tree, ((ArrayType) expected).elemtype, ((ArrayType) actual).elemtype, state);
8692
}
8793
return NO_MATCH;
8894
}

core/src/test/java/com/google/errorprone/bugpatterns/collectionincompatibletype/JUnitIncompatibleTypeTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,51 @@ public void fourArgumentOverload() {
144144
"}")
145145
.doTest();
146146
}
147+
148+
@Test
149+
public void arrayNullComparison() {
150+
compilationHelper
151+
.addSourceLines(
152+
"Test.java",
153+
"import static org.junit.Assert.assertArrayEquals;",
154+
"class Test {",
155+
" public void test() {",
156+
" assertArrayEquals(null, new Object[] {2});",
157+
" }",
158+
"}")
159+
.doTest();
160+
}
161+
162+
@Test
163+
public void typeVariables_incompatible() {
164+
compilationHelper
165+
.addSourceLines(
166+
"Test.java",
167+
"import static org.junit.Assert.assertArrayEquals;",
168+
"import java.util.Map;",
169+
"class Test {",
170+
" public <T extends String> void test(Map<Integer, T[]> xs) {",
171+
" T[] x = xs.get(1);",
172+
" // BUG: Diagnostic contains:",
173+
" assertArrayEquals(x, new Double[] {1d});",
174+
" }",
175+
"}")
176+
.doTest();
177+
}
178+
179+
@Test
180+
public void typeVariables_compatible() {
181+
compilationHelper
182+
.addSourceLines(
183+
"Test.java",
184+
"import static org.junit.Assert.assertArrayEquals;",
185+
"import java.util.Map;",
186+
"class Test {",
187+
" public <T extends Double> void test(Map<Integer, T[]> xs) {",
188+
" T[] x = xs.get(1);",
189+
" assertArrayEquals(x, new Double[] {1d});",
190+
" }",
191+
"}")
192+
.doTest();
193+
}
147194
}

0 commit comments

Comments
 (0)