A class overrides equals without hashCode
What it finds
Equal objects must have equal hash codes, or hash-based collections lose them: a HashSet holds two equal elements, and a HashMap does not find a key that is in it. A class that overrides equals keeps the identity hash code of Any, which differs between equal instances. Objects are left alone, as there is only one instance of each.
From the rule's source, equals_hashcode.rs.
class Point(val x: Int, val y: Int) { override fun equals(other: Any?): Boolean = other is Point && other.x == x && other.y == ytx0 reports: equals is overridden without hashCode} class Id(val value: Int) { override fun hashCode() = value} object Single { override fun equals(other: Any?) = other === this} class Both(val value: Int) { override fun equals(other: Any?) = other is Both && other.value == value override fun hashCode() = value} 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/kotlin:equals-without-hashcode" = "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/kotlin:equals-without-hashcode] reason="..."tx0 explain tx0/kotlin:equals-without-hashcode shows the same in the terminal. Found a false positive? Report it.