Empty If Body

ID

go.empty_if_body

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

CWE:1071, empty, reliability

Description

Reports an if, else if, or else branch whose body is present but empty ({ } with no statement). The condition is evaluated yet the branch does nothing.

Rationale

An empty branch almost always signals a typo, a guard whose body was forgotten, or leftover code from an incomplete refactoring. Because Go always requires braces, the empty body looks like a real branch but performs no action. A branch that contains only a comment is still reported — a comment is not an executable statement.

func classify(n int) string {
    if n > 0 {           // FLAW — empty then-branch, nothing happens
    }

    if n > 0 {
        return "pos"
    } else {             // FLAW — empty else-branch
    }

    if n > 0 {
        return "pos"     // OK — non-empty branch
    } else if n < 0 {
        return "neg"     // OK — non-empty branch
    } else {
        return "zero"    // OK — non-empty branch
    }
}

Remediation

Fill in the missing body, or remove the branch (or the whole if) if the condition was meant to be deleted. If an empty branch is intentional alongside a non-empty one, invert the condition to eliminate the empty branch.