KT.NULL_CHECK_ON_MUTABLE_PROPERTY

null-checks on mutable property

Reports null-checks on mutable properties, as these properties' value can be changed - and thus make the null-check invalid - after the execution of the if-statement.

Noncompliant Code

Copy
class A(private var a: Int?) {
  fun foo() {
      if (a != null) {
      println(2 + a!!)
      }
  }
}

Compliant Code

Copy
class A(private val a: Int?) {
  fun foo() {
      if (a != null) {
      println(2 + a)
      }
  }
}

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