Skip to content

Commit

Permalink
Handle six-arg Arrays.equals in EqualsWrongThing
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 493998416
  • Loading branch information
graememorgan authored and Error Prone Team committed Dec 8, 2022
1 parent ac51650 commit ba7ce2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ public Void visitBinary(BinaryTree node, Void unused) {

@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
var args = node.getArguments();
if (COMPARISON_METHOD.matches(node, state)) {
getDubiousComparison(
classSymbol, node, node.getArguments().get(0), node.getArguments().get(1))
// args.size() / 2 handles the six-arg overload of Arrays.equals (the 0th and 3rd args are
// the arrays).
getDubiousComparison(classSymbol, node, args.get(0), args.get(args.size() / 2))
.ifPresent(suspiciousComparisons::add);
}
if (instanceEqualsInvocation().matches(node, state)) {
Expand All @@ -102,7 +104,7 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
// Special-case super, for odd cases like `super.equals(this)`.
if (!(receiver instanceof IdentifierTree
&& ((IdentifierTree) receiver).getName().contentEquals("super"))) {
getDubiousComparison(classSymbol, node, receiver, node.getArguments().get(0))
getDubiousComparison(classSymbol, node, receiver, args.get(0))
.ifPresent(suspiciousComparisons::add);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,18 @@ public void positiveEquals() {
"}")
.doTest();
}

@Test
public void negativeArraysEquals() {
helper
.addSourceLines(
"Test.java",
"import java.util.Arrays;",
"class Test {",
" boolean test(int[] a, int[] b) {",
" return Arrays.equals(a, 0, 1, b, 2, 3);",
" }",
"}")
.doTest();
}
}

0 comments on commit ba7ce2e

Please sign in to comment.