Back to Intellij Community

CascadeIf

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

2025.3-rc-2527 B
Original Source

Reports if statements with three or more branches that can be replaced with the when expression with a subject.

Example:

fun translateNumber(n: Int): String {
    return if (n == 1) {
      "one"
    } else if (n == 2) {
      "two"
    } else {
      "???"
    }
  }

The quick-fix converts the if expression to when:

fun translateNumber(n: Int): String {
    return when (n) {
      1 -> {
        "one"
      }
      2 -> {
        "two"
      }
      else -> {
        "???"
      }
    }
  }