Back to Intellij Community

IfThenToElvis

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

2025.3-rc-2378 B
Original Source

Reports if-then expressions that can be folded into elvis (?:) expressions.

Example:

fun maybeFoo(): String? = "foo"

  var foo = maybeFoo()
  val bar = if (foo == null) "hello" else foo

The quick fix converts the if-then expression into an elvis (?:) expression:

fun maybeFoo(): String? = "foo"

  var foo = maybeFoo()
  val bar = foo ?: "hello"