An infinite loop is one that will never end while the program is running, i.e., you have to kill the program to get out of the loop. Whether it is
by meeting the loop’s end condition or via a break
, every loop should have an end condition.
Known Limitations
- False positives: when
yield
is used - Issue #674.
- False positives: when an exception is raised by a function invoked within the loop.
- False negatives: when a loop condition is based on an element of an array or object.
Noncompliant Code Example
for (;;) { // Noncompliant; end condition omitted
// ...
}
var j = 0;
while (true) { // Noncompliant; constant end condition
j++;
}
var k;
var b = true;
while (b) { // Noncompliant; constant end condition
k++;
}
Compliant Solution
while (true) { // break will potentially allow leaving the loop
if (someCondition) {
break;
}
}
var k;
var b = true;
while (b) {
k++;
b = k < 10;
}
outer:
while(true) {
while(true) {
break outer;
}
}