KT.ELSE_CASE_INSTEAD_OF_EXHAUSTIVE_WHEN

When expression should not contain an else case when its an enum class, sealed class or of type boolean.

This rule reports when expressions that contain an else case even though they have an exhaustive set of cases. This occurs when the subject of the when expression is either an enum class, sealed class or of type boolean. Using else cases for these expressions can lead to unintended behavior when adding new enum types, sealed subtypes or changing the nullability of a boolean, since this will be implicitly handled by the else case.

Noncompliant Code

Copy
enum class Color {
    RED,
    GREEN,
    BLUE
}
when(c) {
    Color.RED -> {}
    Color.GREEN -> {}
    else -> {}
}

Compliant Code

Copy
enum class Color {
    RED,
    GREEN,
    BLUE
}
when(c) {
    Color.RED -> {}
    Color.GREEN -> {}
    Color.BLUE -> {}
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html