Inspectopedia Help

'equals()' and 'hashCode()' not paired

Reports classes that override equals() but do not override hashCode(), or vice versa. It also reports object declarations that override either equals() or hashCode().

This can lead to undesired behavior when a class is added to a Collection

Example:

class C1 { override fun equals(other: Any?) = true } class C2 { override fun hashCode() = 0 } object O1 { override fun equals(other: Any?) = true } object O2 { override fun hashCode() = 0 }

The quick-fix overrides equals() or hashCode() for classes and deletes these methods for objects:

class C1 { override fun equals(other: Any?) = true override fun hashCode(): Int { return javaClass.hashCode() } } class C2 { override fun hashCode() = 0 override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false return true } } object O1 { } object O2 { }

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Kotlin, @snapshot@

Last modified: 13 July 2023