Arrays are compared with equals, which compares references

tx0/java:array-equalsdefaultwarningbug

What it finds

a.equals(b) on an array compares references, not elements, since arrays inherit equals from Object: two arrays with the same elements are not equal. Arrays.equals(a, b) compares elements. Objects.equals(a, b) with two arrays is the same mistake.

Only receivers whose declared type is an array are reported. A variable of a type parameter can hold an array too, but is usually not meant to.

From the rule's source, array_equals.rs.

Example from the rule's tests
import java.util.Arrays;import java.util.List;import java.util.Objects; class Keys {  private final byte[] digest;   Keys(byte[] digest) {    this.digest = digest;  }   boolean same(byte[] other, int[] a, int b[], List<Integer> list, Object o) {    boolean r = digest.equals(other);tx0 reports: digest is an array    r = this.digest.equals(other);tx0 reports: this.digest is an array    r = b.equals(a);tx0 reports: b is an array    r = Objects.equals(a, b);tx0 reports: a is an array    r = Objects.equals(a, o);    r = Arrays.equals(digest, other);    r = list.equals(o);    return r;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:array-equals" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:array-equals] reason="..."

tx0 explain tx0/java:array-equals shows the same in the terminal. Found a false positive? Report it.