Skip to content

Commit 0e004e5

Browse files
cushonError Prone Team
authored andcommitted
Fix a crash in DuplicateAssertion
Don't skip the first string argument on `assertEquals(String, String)`, it isn't a message. PiperOrigin-RevId: 853249324
1 parent 8cfa4e5 commit 0e004e5

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ private ImmutableSetMultimap<Assertion, Integer> extractAssertionLines(
107107

108108
/** Returns the number of arguments to skip so we ignore {@code message} arguments. */
109109
private static int argumentsToSkip(MethodInvocationTree tree, VisitorState state) {
110-
return ASTHelpers.isSameType(
111-
getSymbol(tree).getParameters().getFirst().type, state.getSymtab().stringType, state)
112-
? 1
113-
: 0;
110+
var parameters = getSymbol(tree).getParameters();
111+
if (!ASTHelpers.isSameType(parameters.getFirst().type, state.getSymtab().stringType, state)) {
112+
return 0;
113+
}
114+
if (parameters.size() == 2) {
115+
return 0;
116+
}
117+
return 1;
114118
}
115119

116120
private record Assertion(String line, ConstantExpression assertee) {}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,25 @@ public void test(Iterator<Object> o) {
125125
""")
126126
.doTest();
127127
}
128+
129+
@Test
130+
public void twoStringParameters() {
131+
helper
132+
.addSourceLines(
133+
"Test.java",
134+
"""
135+
import junit.framework.Assert;
136+
import java.nio.file.Files;
137+
import java.nio.file.Path;
138+
import java.io.IOException;
139+
140+
class Test {
141+
public final void checkContents(String relativePath, String expectedContents) throws IOException {
142+
String actualContents = Files.readString(Path.of(relativePath));
143+
Assert.assertEquals(expectedContents, actualContents);
144+
}
145+
}
146+
""")
147+
.doTest();
148+
}
128149
}

0 commit comments

Comments
 (0)