2023-05-03 11:06:20 +02:00
== Why is this an issue?
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.
2020-12-21 15:38:52 +01:00
2021-02-02 15:02:10 +01:00
2023-05-03 11:06:20 +02:00
There are some known limitations:
2020-12-21 15:38:52 +01:00
2021-01-27 13:42:22 +01:00
* False positives: when ``++yield++`` is used - https://github.com/SonarSource/SonarJS/issues/674[Issue #674].
2020-12-21 15:38:52 +01:00
* 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.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-12-21 15:38:52 +01:00
----
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++;
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-12-21 15:38:52 +01:00
----
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;
}
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]