Empty When Branch

ID

kotlin.empty_when_branch

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

empty, when

Description

Reports when entries whose body is empty. A branch of the form value → { } or is X → { } matches the subject but does nothing.

Rationale

when (status) {
    Status.OK    -> { }            // FLAW — empty body
    Status.WARN  -> { }            // FLAW — empty body
    Status.ERROR -> handleError()  // OK
}

An empty branch is dead code: if the intent was to fall through to a common handler the entry should be removed; if the intent was to perform an action the body must be filled in.

Remediation

  • Remove the empty entry to fall through to the implicit no-op behaviour.

  • Add the intended logic inside the body.

  • Group several values mapping to the same logic into one comma-separated entry.