Superfluous Else

ID

go.superfluous_else

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Go

Tags

code-style, reliability

Description

Reports an else branch that follows an if whose then-branch always terminates — it ends in return, break, continue, goto, panic(…​) or os.Exit(…​). When the then-branch can never fall through, the else is unnecessary: its body can be unindented to follow the if.

Rationale

When the if branch always leaves the enclosing function, loop or block, control never reaches the code after it unless the condition was false — so the else only adds nesting. The idiomatic Go style is the early return: handle the special case in the if, then continue with the normal path at the outer level. Flattening the else reduces indentation and makes the happy path easier to read.

if x > 0 {
    return "positive"
} else {            // FLAW — the if branch returns; drop the else
    return "non-positive"
}

if x > 0 {
    return "positive"
}
return "non-positive" // OK — no else; the early return stands alone

Remediation

Remove the else and unindent its body so it follows the if directly.

References