2 title: no-unreachable-loop
7 - no-constant-condition
8 - no-unmodified-loop-condition
13 A loop that can never reach the second iteration is a possible error in the code.
16 for (let i = 0; i < arr.length; i++) {
17 if (arr[i].name === myName) {
19 // break was supposed to be here
25 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.
29 This rule aims to detect and disallow loops that can have at most one iteration, by performing static code path analysis on loop bodies.
31 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.
33 This rule checks `while`, `do-while`, `for`, `for-in` and `for-of` loops. You can optionally disable checks for each of these constructs.
35 Examples of **incorrect** code for this rule:
40 /*eslint no-unreachable-loop: "error"*/
48 function verifyList(head) {
59 function findSomething(arr) {
60 for (var i = 0; i < arr.length; i++) {
61 if (isSomething(arr[i])) {
64 throw new Error("Doesn't exist.");
70 if (key.startsWith("_")) {
74 firstValue = obj[key];
88 Examples of **correct** code for this rule:
93 /*eslint no-unreachable-loop: "error"*/
100 function verifyList(head) {
113 function findSomething(arr) {
114 for (var i = 0; i < arr.length; i++) {
115 if (isSomething(arr[i])) {
119 throw new Error("Doesn't exist.");
123 if (key.startsWith("_")) {
127 firstValue = obj[key];
141 Please note that this rule is not designed to check loop conditions, and will not warn in cases such as the following examples.
143 Examples of additional **correct** code for this rule:
148 /*eslint no-unreachable-loop: "error"*/
154 for (let i = 0; i < 1; i++) {
158 for (const a of [1]) {
167 This rule has an object option, with one option:
169 * `"ignore"` - an optional array of loop types that will be ignored by this rule.
173 You can specify up to 5 different elements in the `"ignore"` array:
175 * `"WhileStatement"` - to ignore all `while` loops.
176 * `"DoWhileStatement"` - to ignore all `do-while` loops.
177 * `"ForStatement"` - to ignore all `for` loops (does not apply to `for-in` and `for-of` loops).
178 * `"ForInStatement"` - to ignore all `for-in` loops.
179 * `"ForOfStatement"` - to ignore all `for-of` loops.
181 Examples of **correct** code for this rule with the `"ignore"` option:
186 /*eslint no-unreachable-loop: ["error", { "ignore": ["ForInStatement", "ForOfStatement"] }]*/
188 for (var key in obj) {
189 hasEnumerableProperties = true;
193 for (const a of b) break;
200 Static code path analysis, in general, does not evaluate conditions. Due to this fact, this rule might miss reporting cases such as the following:
203 for (let i = 0; i < 10; i++) {