no-useless-else
UnreleasedConfiguration
rslint.config.ts
Rule Details
Disallow an else after an if branch that always exits. This includes branches that end in return, throw, break, continue, or a call to the global process.exit(). The rule also recognizes nested conditionals, switches, loops, and try statements when execution cannot continue after the first branch.
Examples of incorrect code:
Examples of correct code:
The fix moves the else body after the if. The rule reports without a fix when moving the body could change declaration scope, comment placement, multiline literal values, or automatic semicolon insertion.
Options
This rule has no options. Enable it with "unicorn/no-useless-else": "error".
Differences from upstream
- A call to
process.exit()is treated as an ordinary call when theprocessglobal is configured as"off"or a surroundingwithstatement can supply that name. Unicorn may report anelsein these cases. - If a
finallyblock overrides a pendingbreakorcontinue, the rule follows the destination that execution actually takes. This can keep anelsethat Unicorn reports, or report one when thefinallyblock exits. - The rule keeps an
elseif acatchcould let execution continue after a JSX factory call, a destructured catch binding, or a read ofthisin a derived constructor. Reads ofthisare treated conservatively even after a call tosuper(). - In a
tryblock, more complex assignments and optional chains can keep anelsethat Unicorn reports. For example, the rule can prove thatvalue = 1is safe whenvalueis a local parameter, but it may keep theelseaftervalue = other. - When an
elsebody contains a lint directive such aseslint-disable-next-line, the rule reports it without an autofix. Keep the directive next to its intended statement if you move the body manually.