Inspectopedia Help

Variable declaration could be moved inside 'when'

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 } }

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Kotlin, @snapshot@

Last modified: 13 July 2023