]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-unreachable-loop | |
3 | layout: doc | |
4 | rule_type: problem | |
5 | related_rules: | |
6 | - no-unreachable | |
7 | - no-constant-condition | |
8 | - no-unmodified-loop-condition | |
9 | - for-direction | |
10 | --- | |
11 | ||
6f036462 TL |
12 | |
13 | A loop that can never reach the second iteration is a possible error in the code. | |
14 | ||
15 | ```js | |
16 | for (let i = 0; i < arr.length; i++) { | |
17 | if (arr[i].name === myName) { | |
18 | doSomething(arr[i]); | |
19 | // break was supposed to be here | |
20 | } | |
21 | break; | |
22 | } | |
23 | ``` | |
24 | ||
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. | |
26 | ||
27 | ## Rule Details | |
28 | ||
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. | |
30 | ||
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. | |
32 | ||
33 | This rule checks `while`, `do-while`, `for`, `for-in` and `for-of` loops. You can optionally disable checks for each of these constructs. | |
34 | ||
35 | Examples of **incorrect** code for this rule: | |
36 | ||
8f9d1d4d DC |
37 | ::: incorrect |
38 | ||
6f036462 TL |
39 | ```js |
40 | /*eslint no-unreachable-loop: "error"*/ | |
41 | ||
42 | while (foo) { | |
43 | doSomething(foo); | |
44 | foo = foo.parent; | |
45 | break; | |
46 | } | |
47 | ||
48 | function verifyList(head) { | |
49 | let item = head; | |
50 | do { | |
51 | if (verify(item)) { | |
52 | return true; | |
53 | } else { | |
54 | return false; | |
55 | } | |
56 | } while (item); | |
57 | } | |
58 | ||
59 | function findSomething(arr) { | |
60 | for (var i = 0; i < arr.length; i++) { | |
61 | if (isSomething(arr[i])) { | |
62 | return arr[i]; | |
63 | } else { | |
64 | throw new Error("Doesn't exist."); | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | for (key in obj) { | |
70 | if (key.startsWith("_")) { | |
71 | break; | |
72 | } | |
73 | firstKey = key; | |
74 | firstValue = obj[key]; | |
75 | break; | |
76 | } | |
77 | ||
78 | for (foo of bar) { | |
79 | if (foo.id === id) { | |
80 | doSomething(foo); | |
81 | } | |
82 | break; | |
83 | } | |
84 | ``` | |
85 | ||
8f9d1d4d DC |
86 | ::: |
87 | ||
6f036462 TL |
88 | Examples of **correct** code for this rule: |
89 | ||
8f9d1d4d DC |
90 | ::: correct |
91 | ||
6f036462 TL |
92 | ```js |
93 | /*eslint no-unreachable-loop: "error"*/ | |
94 | ||
95 | while (foo) { | |
96 | doSomething(foo); | |
97 | foo = foo.parent; | |
98 | } | |
99 | ||
100 | function verifyList(head) { | |
101 | let item = head; | |
102 | do { | |
103 | if (verify(item)) { | |
104 | item = item.next; | |
105 | } else { | |
106 | return false; | |
107 | } | |
108 | } while (item); | |
109 | ||
110 | return true; | |
111 | } | |
112 | ||
113 | function findSomething(arr) { | |
114 | for (var i = 0; i < arr.length; i++) { | |
115 | if (isSomething(arr[i])) { | |
116 | return arr[i]; | |
117 | } | |
118 | } | |
119 | throw new Error("Doesn't exist."); | |
120 | } | |
121 | ||
122 | for (key in obj) { | |
123 | if (key.startsWith("_")) { | |
124 | continue; | |
125 | } | |
126 | firstKey = key; | |
127 | firstValue = obj[key]; | |
128 | break; | |
129 | } | |
130 | ||
131 | for (foo of bar) { | |
132 | if (foo.id === id) { | |
133 | doSomething(foo); | |
134 | break; | |
135 | } | |
136 | } | |
137 | ``` | |
138 | ||
8f9d1d4d DC |
139 | ::: |
140 | ||
6f036462 TL |
141 | Please note that this rule is not designed to check loop conditions, and will not warn in cases such as the following examples. |
142 | ||
143 | Examples of additional **correct** code for this rule: | |
144 | ||
8f9d1d4d DC |
145 | ::: correct |
146 | ||
6f036462 TL |
147 | ```js |
148 | /*eslint no-unreachable-loop: "error"*/ | |
149 | ||
150 | do { | |
151 | doSomething(); | |
152 | } while (false) | |
153 | ||
154 | for (let i = 0; i < 1; i++) { | |
155 | doSomething(i); | |
156 | } | |
157 | ||
158 | for (const a of [1]) { | |
159 | doSomething(a); | |
160 | } | |
161 | ``` | |
162 | ||
8f9d1d4d DC |
163 | ::: |
164 | ||
6f036462 TL |
165 | ## Options |
166 | ||
167 | This rule has an object option, with one option: | |
168 | ||
169 | * `"ignore"` - an optional array of loop types that will be ignored by this rule. | |
170 | ||
8f9d1d4d | 171 | ### ignore |
6f036462 TL |
172 | |
173 | You can specify up to 5 different elements in the `"ignore"` array: | |
174 | ||
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. | |
180 | ||
181 | Examples of **correct** code for this rule with the `"ignore"` option: | |
182 | ||
8f9d1d4d DC |
183 | ::: correct |
184 | ||
6f036462 TL |
185 | ```js |
186 | /*eslint no-unreachable-loop: ["error", { "ignore": ["ForInStatement", "ForOfStatement"] }]*/ | |
187 | ||
188 | for (var key in obj) { | |
189 | hasEnumerableProperties = true; | |
190 | break; | |
191 | } | |
192 | ||
193 | for (const a of b) break; | |
194 | ``` | |
195 | ||
8f9d1d4d DC |
196 | ::: |
197 | ||
6f036462 TL |
198 | ## Known Limitations |
199 | ||
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: | |
201 | ||
202 | ```js | |
203 | for (let i = 0; i < 10; i++) { | |
204 | doSomething(i); | |
205 | if (true) { | |
206 | break; | |
207 | } | |
208 | } | |
209 | ``` |