From ba7ce2e7d2ab9a16c83c7b77ddef9ed56f4c6796 Mon Sep 17 00:00:00 2001 From: ghm Date: Thu, 8 Dec 2022 14:35:51 -0800 Subject: [PATCH] Handle six-arg Arrays.equals in EqualsWrongThing PiperOrigin-RevId: 493998416 --- .../errorprone/bugpatterns/EqualsWrongThing.java | 8 +++++--- .../bugpatterns/EqualsWrongThingTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/EqualsWrongThing.java b/core/src/main/java/com/google/errorprone/bugpatterns/EqualsWrongThing.java index 16ddbad8058..ba63a295af7 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/EqualsWrongThing.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/EqualsWrongThing.java @@ -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)) { @@ -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); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/EqualsWrongThingTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/EqualsWrongThingTest.java index 5f40e14a943..7b5c02a850d 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/EqualsWrongThingTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/EqualsWrongThingTest.java @@ -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(); + } }