avoid_redundant_else
v0.8.0 Warning Fix Control Flow
This rule flags an else branch whose matching if branch always exits — via return, throw, break, or continue.
Why use this rule
Section titled “Why use this rule”When the then-branch cannot fall through, the else adds nothing: control reaching the code after the if already implies the condition was false. What it does add is a level of indentation for everything that follows, which compounds in methods with several guards.
Removing it produces the guard-clause style: handle the exceptional cases early and let the main path stay flat.
String describe(int value) { if (value < 0) { return 'negative'; } else { // Indented for no reason — the branch above always returns return 'non-negative'; }}String describe(int value) { if (value < 0) { return 'negative'; } return 'non-negative';}Known limitations
Section titled “Known limitations”else if chains are never reported. They read as a single decision, and splitting them into sequential if statements usually reads worse than the chain.
The exit check is syntactic: a branch counts as exiting when its last statement is a return, throw, break, or continue. A branch that exits through a helper (_fail() returning Never) is not recognised.
The quick fix declines to hoist an else body that declares a variable, since the name could collide in the enclosing scope. Those cases report without an automatic fix.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_redundant_else: trueTo turn it off again:
rules: avoid_redundant_else: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_collapsible_if— Merge nested if statements with &&.prefer_early_return— Replace a body-wrapping if with an early-return guard.prefer_immediate_return— Return an expression directly instead of via a throwaway variable.no_equal_then_else— Both branches of a condition are identical.