You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A loop that can never reach the second iteration is a possible error in the code.
for(leti=0;i<arr.length;i++){if(arr[i].name===myName){doSomething(arr[i]);// break was supposed to be here}break;}
In rare cases where only one iteration (or at most one iteration) is intended behavior, the code should be refactored to use if conditionals instead of while, do-while and for loops. It's considered a best practice to avoid using loop constructs for such cases.
Rule Details
This rule aims to detect and disallow loops that can have at most one iteration, by performing static code path analysis on loop bodies.
In particular, this rule will disallow a loop with a body that exits the loop in all code paths. If all code paths in the loop's body will end with either a break, return or a throw statement, the second iteration of such loop is certainly unreachable, regardless of the loop's condition.
This rule checks while, do-while, for, for-in and for-of loops. You can optionally disable checks for each of these constructs.
https://eslint.org/docs/rules/no-unreachable-loop
A loop that can never reach the second iteration is a possible error in the code.
In rare cases where only one iteration (or at most one iteration) is intended behavior, the code should be refactored to use
if
conditionals instead ofwhile
,do-while
andfor
loops. It's considered a best practice to avoid using loop constructs for such cases.Rule Details
This rule aims to detect and disallow loops that can have at most one iteration, by performing static code path analysis on loop bodies.
In particular, this rule will disallow a loop with a body that exits the loop in all code paths. If all code paths in the loop's body will end with either a
break
,return
or athrow
statement, the second iteration of such loop is certainly unreachable, regardless of the loop's condition.This rule checks
while
,do-while
,for
,for-in
andfor-of
loops. You can optionally disable checks for each of these constructs.Examples of incorrect code for this rule:
Examples of correct code for this rule:
Please note that this rule is not designed to check loop conditions, and will not warn in cases such as the following examples.
Examples of additional correct code for this rule:
The text was updated successfully, but these errors were encountered: