Back to Intellij Community

FoldInitializerAndIfToElvis

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

2025.3-rc-2494 B
Original Source

Reports an if expression that checks variable being null or not right after initializing it that can be converted into an elvis operator in the initializer.

Example:

fun test(foo: Int?, bar: Int): Int {
      var i = foo
      if (i == null) {
          return bar
      }
      return i
  }

The quick-fix converts the if expression with an initializer into an elvis expression:

fun test(foo: Int?, bar: Int): Int {
      var i = foo ?: return bar
      return i
  }