π Disallow else after a statement that exits.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§ This rule is automatically fixable by the --fix CLI option.
When an if branch always exits, its else branch is unnecessary. Moving the else body after the if makes the control flow flatter.
This rule reports branches that always exit with return, throw, break, continue, or terminal direct global process.exit(). It uses code path analysis, so it also recognizes more complex control flow such as nested if statements where both branches exit, exhaustive switch statements, try/catch/finally, and infinite loops.
The autofix is conservative and skips cases where removing the else could affect scoped declarations, comments, or automatic semicolon insertion.
// β
if (foo) {
return;
} else {
bar();
}
// β
if (foo) {
return;
}
bar();// β
if (foo) {
throw new Error();
} else if (bar) {
baz();
}
// β
if (foo) {
throw new Error();
}
if (bar) {
baz();
}- ESLint
no-else-returnonly checkselseafterreturn. This rule is a broader alternative that also checksthrow,break,continue, and terminal direct globalprocess.exit(), and always reportselse if. unicorn/prefer-early-returnis complementary. It reports whole-function conditional wrappers withoutelse; this rule reports an existingelseafter an exiting branch.