Empty When Block

ID

kotlin.empty_when_block

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Kotlin

Tags

empty, reliability

Description

Reports when expressions that have no entries — neither branches nor an else clause.

A when expression with an empty body is dead code. The subject expression may be evaluated (potentially triggering side-effects or null-pointer exceptions on the receiver) but no branch is ever matched or executed. Such a construct almost always results from an incomplete implementation or a copy-paste error.

Rationale

// Bad — when has no branches
when (status) { }   // FLAW

// OK — when has branches
when (status) {
    Status.OK    -> handleOk()
    Status.ERROR -> handleError()
    else         -> handleUnknown()
}

Remediation

  • Add the required branches to the when expression.

  • If no branching is needed, remove the when construct entirely.