KT.USE_DATA_CLASS

Use data class

Classes that simply hold data should be refactored into a data class. Data classes are specialized to hold data and generate hashCode, equals and toString implementations as well. Read more about data class: https://kotlinlang.org/docs/data-classes.html

Noncompliant Code

Copy
class DataClassCandidate(val i: Int) {
    val i2: Int = 0
}

Compliant Code

Copy
data class DataClass(val i: Int, val i2: Int)

// classes with delegating interfaces are compliant
interface I
class B() : I
class A(val b: B) : I by b

Options

  • allowVars (default: False)

    allows to relax this rule in order to exclude classes that contains one (or more) vars

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