Skip to content

Latest commit

Β 

History

History
57 lines (41 loc) Β· 1.94 KB

File metadata and controls

57 lines (41 loc) Β· 1.94 KB

no-useless-else

πŸ“ 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.

Examples

// ❌
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();
}

Related rules

  • ESLint no-else-return only checks else after return. This rule is a broader alternative that also checks throw, break, continue, and terminal direct global process.exit(), and always reports else if.
  • unicorn/prefer-early-return is complementary. It reports whole-function conditional wrappers without else; this rule reports an existing else after an exiting branch.