Equals Incompatible Types

ID

kotlin.equals_incompatible_types

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

reliability, suspicious-comparison

Description

Reports a.equals(b) calls where the static types of a and b belong to unrelated value-type families (for example String and Int, or Double and Boolean). The call cannot succeed: the JDK equals implementation of any boxed value type starts with if (other !is ThisType) return false.

Rationale

val s: String = "hello"
val n: Int = 1

s.equals(n)        // FLAW — always false
"x".equals(42)     // FLAW — always false

Such a call is almost always the result of a typo or a copy-paste error. Worse, the compiler accepts it silently because equals takes Any?, so the bug surfaces at runtime as a feature that "never matches" — a class of bug that is notoriously hard to reproduce from logs alone.

Remediation

If the comparison is intentional, fix the types so they actually belong to the same family — for example by parsing the string into an Int first:

val s: String = "hello"
val n: Int = 1

n.equals(s.toIntOrNull())  // OK — both Int? after conversion

If you wrote the wrong identifier, replace it with the right one.