Back to Intellij Community

MoveVariableDeclarationIntoWhen

plugins/kotlin/code-insight/descriptions/resources-en/inspectionDescriptions/MoveVariableDeclarationIntoWhen.html

2025.3-rc-2391 B
Original Source

Reports variable declarations that can be moved inside a when expression.

Example:

fun someCalc(x: Int) = x * 42

fun foo(x: Int): Int {
  val a = someCalc(x)
  return when (a) {
    1 -> a
    2 -> 2 * a
    else -> 24
  }
}

After the quick-fix is applied:

fun foo(x: Int): Int {
  return when (val a = someCalc(x)) {
    1 -> a
    2 -> 2 * a
    else -> 24
  }
}