2 title: no-unreachable-loop
6 - no-constant-condition
7 - no-unmodified-loop-condition
12 A loop that can never reach the second iteration is a possible error in the code.
15 for (let i = 0; i < arr.length; i++) {
16 if (arr[i].name === myName) {
18 // break was supposed to be here
24 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.
28 This rule aims to detect and disallow loops that can have at most one iteration, by performing static code path analysis on loop bodies.
30 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.
32 This rule checks `while`, `do-while`, `for`, `for-in` and `for-of` loops. You can optionally disable checks for each of these constructs.
34 Examples of **incorrect** code for this rule:
39 /*eslint no-unreachable-loop: "error"*/
47 function verifyList(head) {
58 function findSomething(arr) {
59 for (var i = 0; i < arr.length; i++) {
60 if (isSomething(arr[i])) {
63 throw new Error("Doesn't exist.");
69 if (key.startsWith("_")) {
73 firstValue = obj[key];
87 Examples of **correct** code for this rule:
92 /*eslint no-unreachable-loop: "error"*/
99 function verifyList(head) {
112 function findSomething(arr) {
113 for (var i = 0; i < arr.length; i++) {
114 if (isSomething(arr[i])) {
118 throw new Error("Doesn't exist.");
122 if (key.startsWith("_")) {
126 firstValue = obj[key];
140 Please note that this rule is not designed to check loop conditions, and will not warn in cases such as the following examples.
142 Examples of additional **correct** code for this rule:
147 /*eslint no-unreachable-loop: "error"*/
153 for (let i = 0; i < 1; i++) {
157 for (const a of [1]) {
166 This rule has an object option, with one option:
168 * `"ignore"` - an optional array of loop types that will be ignored by this rule.
172 You can specify up to 5 different elements in the `"ignore"` array:
174 * `"WhileStatement"` - to ignore all `while` loops.
175 * `"DoWhileStatement"` - to ignore all `do-while` loops.
176 * `"ForStatement"` - to ignore all `for` loops (does not apply to `for-in` and `for-of` loops).
177 * `"ForInStatement"` - to ignore all `for-in` loops.
178 * `"ForOfStatement"` - to ignore all `for-of` loops.
180 Examples of **correct** code for this rule with the `"ignore"` option:
185 /*eslint no-unreachable-loop: ["error", { "ignore": ["ForInStatement", "ForOfStatement"] }]*/
187 for (var key in obj) {
188 hasEnumerableProperties = true;
192 for (const a of b) break;
199 Static code path analysis, in general, does not evaluate conditions. Due to this fact, this rule might miss reporting cases such as the following:
202 for (let i = 0; i < 10; i++) {