KT.AVOID_REFERENTIAL_EQUALITY

Referential equality checks

Kotlin supports two types of equality: structural equality and referential equality. While there are use cases for both, checking for referential equality for some types (such as String or List) is likely not intentional and may case unexpected results.

Noncompliant Code

Copy
val areEqual = "aString" === otherString
val areNotEqual = "aString" !== otherString

Compliant Code

Copy
val areEqual = "aString" == otherString
val areNotEqual = "aString" != otherString

Options

  • forbiddenTypePatterns (default: ['kotlin.string'])

    Specifies those types for which referential equality checks are considered a rule violation. The types are defined by a list of simple glob patterns (supporting * and ? wildcards) that match the fully qualified type name.

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