else branch has an empty body
ID |
scala.correctness.empty_else_block |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Scala |
Description
Reports an else branch whose body is an empty block, so the branch exists but does nothing.
Rationale
An empty else is indistinguishable from an unfinished one: a reader cannot tell whether the case was deliberately ignored or whether the handling was never written or was deleted during a refactor. It also adds a branch that coverage tooling and tests must account for while contributing no behaviour.
The following code illustrates the pattern detected by this rule:
class EmptyElseBlock {
def guard(n: Int): Unit = {
// FLAGGED: else branch has an empty body
if (n > 0) {
println("positive")
} else {}
}
}