Modify rule S2189: Adapt to LaYC (#2656)
This commit is contained in:
parent
4cc3355a7b
commit
20d3bbbc43
@ -1,59 +1,67 @@
|
||||
== Why is this an issue?
|
||||
|
||||
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.
|
||||
A loop is a control structure that allows a block of code to be executed repeatedly until a certain condition is met. The basic idea behind a loop is to automate repetitive tasks, such as iterating over a collection of data or performing a calculation multiple times with different inputs.
|
||||
|
||||
An infinite loop is a loop that runs indefinitely without ever terminating. In other words, the loop condition is always true, and the loop never exits. This can happen when the loop condition is not defined or when the loop condition is never met.
|
||||
|
||||
There are some known limitations:
|
||||
Infinite loops can cause a program to hang or crash, as the program will continue to execute the loop indefinitely.
|
||||
|
||||
This rule will raise an issue on `for`, `while` and ``++do...while++`` loops where no clear exit condition has been found.
|
||||
|
||||
There are some known limitations for this rule:
|
||||
|
||||
* 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
|
||||
|
||||
[source,javascript]
|
||||
[source,javascript,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
for (;;) { // Noncompliant; end condition omitted
|
||||
for (;;) { // Noncompliant: end condition omitted
|
||||
// ...
|
||||
}
|
||||
|
||||
var j = 0;
|
||||
while (true) { // Noncompliant; constant end condition
|
||||
let j = 0;
|
||||
while (true) { // Noncompliant: constant end condition
|
||||
j++;
|
||||
}
|
||||
|
||||
var k;
|
||||
var b = true;
|
||||
while (b) { // Noncompliant; constant end condition
|
||||
let k;
|
||||
let b = true;
|
||||
while (b) { // Noncompliant: constant end condition
|
||||
k++;
|
||||
}
|
||||
----
|
||||
|
||||
=== Compliant solution
|
||||
Ensure the loop condition is defined or use a ``++break++`` statement.
|
||||
|
||||
[source,javascript]
|
||||
[source,javascript,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
while (true) { // break will potentially allow leaving the loop
|
||||
if (someCondition) {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
// ...
|
||||
}
|
||||
|
||||
let j = 0;
|
||||
while (true) {
|
||||
j++;
|
||||
if (j < 5) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var k;
|
||||
var b = true;
|
||||
let k;
|
||||
let b = true;
|
||||
while (b) {
|
||||
k++;
|
||||
b = k < 10;
|
||||
}
|
||||
|
||||
outer:
|
||||
while(true) {
|
||||
while(true) {
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration[MDN - Loops and iteration]
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for[MDN - `for`]
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while[MDN - `while`]
|
||||
* link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while++[MDN - ``++do...while++``]
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break[MDN - `break`]
|
||||
* https://en.wikipedia.org/wiki/Infinite_loop[Wikipedia - Infinite loop]
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user