Skip to content

First On Possibly Empty (ARCH8078)

Key kotlin.performance.first-on-possibly-empty · Severity warning · Category performance

Flags first() / last() / [0] on a filtered or fetched collection with no emptiness guard - NoSuchElementException on the empty case.

val u = users.filter { it.active }.first()
val u = users.firstOrNull { it.active } ?: return

Use firstOrNull / lastOrNull / getOrNull and handle null.