Equals compares a field of this object with a differently named field of the other
What it finds
return width == other.height && height == other.height; in equals compares a field of this object with a different field of the other one, so two equal objects can be reported unequal and unequal ones equal. It is almost always a copy and paste slip.
Reported, inside equals(Object), is a comparison with ==, !=, equals, Objects.equals, Arrays.equals or compare between an instance field of this class, read plainly or through this, and a field of another name read from another object, when the class declares both fields. A comparison that also appears the other way round, as in the equals of an unordered pair, is left alone.
From the rule's source, equals_crossed_fields.rs.
import java.util.Objects; class Box { int width; int height; String label; String title; @Override public boolean equals(Object o) { if (!(o instanceof Box)) { return false; } Box other = (Box) o; return width == other.heighttx0 reports: width of this object is compared with height of the other && height == other.height && Objects.equals(label, other.title)tx0 reports: label of this object is compared with title && title.equals(other.title); }} class Pair { Object first; Object second; @Override public boolean equals(Object o) { Pair p = (Pair) o; return first == p.first && second == p.second || first == p.second && second == p.first; }} 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:
[rules]
"tx0/java:equals-swapped-fields" = "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:equals-swapped-fields] reason="..."tx0 explain tx0/java:equals-swapped-fields shows the same in the terminal. Found a false positive? Report it.