equals, hashCode or toString throws, though callers such as collections and loggers expect them to return
What it finds
Collections call equals and hashCode, and loggers, debuggers and string templates call toString, at moments the class does not choose. An exception there surfaces far from its cause, or hides the message that was being logged. These functions are expected to return a value for every object.
From the rule's source, throw_in_object_contract.rs.
class Key(val id: Int) { override fun hashCode(): Int = throw UnsupportedOperationException("no")tx0 reports: hashCode throws, which its callers do not expect override fun toString(): String { if (id < 0) throw IllegalStateException("negative")tx0 reports: toString throws return "Key($id)" } override fun equals(other: Any?): Boolean { val parsed = try { parse() } catch (e: Exception) { throw IllegalStateException(e) } return other is Key && other.id == id } fun check() { throw IllegalStateException("fine here") }} 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:
[rules]
"tx0/kotlin:throw-in-equals-hashcode-tostring" = "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/kotlin:throw-in-equals-hashcode-tostring] reason="..."tx0 explain tx0/kotlin:throw-in-equals-hashcode-tostring shows the same in the terminal. Found a false positive? Report it.