Simplify Boolean Return

ID

go.simplify_boolean_return

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

Go

Tags

code-smell, readability

Description

Reports the pattern if cond { return true } else { return false } (and its inverted form), which can always be written as the single statement return cond (or return !cond).

Rationale

Branching to return one of two boolean literals expresses, in three lines and an extra indentation level, exactly what a single boolean return states directly. Collapsing it to return cond is behaviour-preserving and noticeably clearer.

The redundant comparison forms x == true / x == false and the double negation !!x are reported by separate rules, so this rule stays focused on the if/else return-boolean statement form.

if cond { return true } else { return false }  // FLAW — return cond
if cond { return false } else { return true }  // FLAW — return !cond
if cond { return true } else { return true }   // OK — same value
if cond { return 1 } else { return 0 }         // OK — not boolean

Remediation

Replace the if/else with return cond when the if branch returns true, or return !cond when it returns false.