Back to Intellij Community

IntroduceWhenSubject

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

2025.3-rc-2471 B
Original Source

Reports a when expression that can be simplified by introducing a subject argument.

Example:

fun test(obj: Any): String {
      return when {
          obj is String -> "string"
          obj is Int -> "int"
          else -> "unknown"
      }
  }

The quick fix introduces a subject argument:

fun test(obj: Any): String {
      return when (obj) {
          is String -> "string"
          is Int -> "int"
          else -> "unknown"
      }
  }