KT.NO_NAME_SHADOWING

Shadowing variable declarations

Disallows shadowing variable declarations. Shadowing makes it impossible to access a variable with the same name in the scope.

Noncompliant Code

Copy
fun test(i: Int, j: Int, k: Int) {
  val i = 1
  val (j, _) = 1 to 2
  listOf(1).map { k -> println(k) }
    listOf(1).forEach {
      listOf(2).forEach {
      }
    }
}

Compliant Code

Copy
fun test(i: Int, j: Int, k: Int) {
  val x = 1
  val (y, _) = 1 to 2
  listOf(1).map { z -> println(z) }
  listOf(1).forEach {
  listOf(2).forEach { x ->
  }
  }
}

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