no-unreachable-loop
A loop that can never reach its second iteration is usually a mistake. If you only need one iteration, use anif statement instead.
Rule Type: Problem
Fixable: No
Fixable: No
Why This Rule Exists
Loops are meant to execute multiple times. If a loop always exits on the first iteration (viabreak, return, or throw), it should be refactored to use if statements instead.
Rule Details
This rule detects loops where all code paths exit the loop in the first iteration via:breakstatementreturnstatementthrowstatement
whileloopsdo-whileloopsforloopsfor-inloopsfor-ofloops
Examples
Incorrect Code
Correct Code
Common Refactorings
Replace Loop with If Statement
Use Array Methods
Fix the Break Statement
Replace Break with Continue
Options
ignore
Type: Array<string>
Specify loop types to ignore. Possible values:
"WhileStatement"- ignorewhileloops"DoWhileStatement"- ignoredo-whileloops"ForStatement"- ignoreforloops"ForInStatement"- ignorefor-inloops"ForOfStatement"- ignorefor-ofloops
Known Limitations
The rule may miss obvious cases:When Not to Use It
This rule helps catch bugs, but you might disable it if:- Your codebase uses loops for single-iteration patterns intentionally
- You’re using a framework that expects loop syntax for single operations
if statements will make code clearer.