54 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
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.
2021-02-02 15:02:10 +01:00
=== Known Limitations
2021-01-27 13:42:22 +01:00
* False positives: when ``++yield++`` is used - https://github.com/SonarSource/SonarJS/issues/674[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;
}
}
----
include::../see.adoc[]