KT.EQUALS_WITH_HASH_CODE_EXIST

Method hashCode() is not overridden

When a class overrides the equals() method it should also override the hashCode() method. All hash-based collections depend on objects meeting the equals-contract. Two equal objects must produce the same hashcode. When inheriting equals or hashcode, override the inherited and call the super method for clarification.

Noncompliant Code

Copy
class Foo {

    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }
}

Compliant Code

Copy
class Foo {

    override fun equals(other: Any?): Boolean {
        return super.equals(other)
    }

    override fun hashCode(): Int {
        return super.hashCode()
    }
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html