Null Type Checking with Elvis Return

Putting this into IntelliJ yields a weak warning:

Equality check should be used instead of elvis for nullable boolean check

If we accept that refactoring, the code is changed to:

fun test(values: IntArray?): Int? {
  if (values?.isEmpty() != false) {
    return null
  }

  return values.min()
}

values is now properly smart casted and the error is no more:

image

As to why that works and the original does not: I guess the compiler just isn’t that smart here. You could try searching the issue tracker or create an issue there about this.

A note about style: both variants can be confusing to readers - there was also some discussion in an old thread about this. To make it clearer, you can replace the if block with

  if (values == null || values.isEmpty()) {
    return null
  }

which makes the intention immediately obvious and also produces the correct smart cast. To tidy this up a bit, you could also write an extension function isNullOrEmpty (see above mentioned thread for an example), so the code would become

  if (values.isNullOrEmpty()) {
    return null
  }

which couldn’t be clearer, really (I would probably do this).

And as a final remark: you can just use values?.minOrNull() (ref) if that’s all test does, no need to reinvent it. :slight_smile:

1 Like