A record holds an array, which its generated equals and hashCode compare by identity

tx0/java:record-array-componentrecommendedwarningbug

What it finds

The equals and hashCode a record gets compare its components with their own equals and hashCode, which for an array means identity: two records holding arrays with the same elements are not equal, and land in different buckets of a hash map. Overriding both methods with Arrays.equals and Arrays.hashCode, or holding a List, compares the contents.

Reported are array components of records that override neither equals nor hashCode.

From the rule's source, record_array_component.rs.

Example from the rule's tests
record Packet(String name, byte[] payload) {}tx0 reports: the array payload is compared by identity record Point(int x, int y) {} record Safe(int[] values) {  @Override  public boolean equals(Object o) {    return o instanceof Safe s && java.util.Arrays.equals(values, s.values);  }   @Override  public int hashCode() {    return java.util.Arrays.hashCode(values);  }}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:record-array-component" = "warning"

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:record-array-component] reason="..."

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