KT.VAR_COULD_BE_VAL

Var declaration that could be val

Reports var declarations (both local variables and private class properties) that could be val, as they are not re-assigned. Val declarations are assign-once (read-only), which makes understanding the current state easier.

Noncompliant Code

Copy
fun example() {
    var i = 1 // violation: this variable is never re-assigned
    val j = i + 1
}

Compliant Code

Copy
fun example() {
    val i = 1
    val j = i + 1
}

Options

  • ignoreLateinitVar (default: False)

    Whether to ignore uninitialized lateinit 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