A class defines compareTo while neither it nor a class above it defines equals

tx0/java:compare-to-without-equalsstrictwarningreliability/equalitydepth projectbug

What it finds

A class whose compareTo returns 0 for two distinct objects while equals is the identity of Object behaves differently in sorted and hashed collections: a TreeSet treats the two as one element, a HashSet keeps both. The contract of Comparable strongly recommends that natural ordering agrees with equals.

Reported at compareTo of a class that implements Comparable, across files, when no class above it up to Object defines equals; all of them must be classes of the project.

From the rule's source, compare_to_without_equals.rs.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:compare-to-without-equals" = "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:compare-to-without-equals] reason="..."

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