'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 {
}
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
EqualsOrHashCode- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Kotlin, 242.22892-IJ |
Last modified: 11 September 2024