SonarJS
SonarJS copied to clipboard
Improve S2189 (no-infinite-loop) by reporting only on local vars
We use ESLint's no-unmodified-loop-condition
for this rule. It seems to be a bit noisy, because we are not able to detect cross-procedural modifications of the loop variables. We could significantly lower the number of FPs by not reporting on variables which are not defined locally in the function.
Another minor improvement - we should only report once per symbol, maybe with secondary location for other occurrences of the symbol within the loop condition
There are multiple FP patterns for the no-unmodified-loop
rule we are using, I think we should consider rewriting the rule completely. Some of the FP patterns are documented below
// control flow statements like break or return
let attempts = 3;
while(expected) {
if (--attempts < 0) {
break;
}
}
// variable is part of more complex condition
function foo(cursor, skipWhitespace) {
var token = this.getToken(cursor),
next = token,
doc = this.editor.document;
do {
if (next.end > cursor.ch) {
cursor.ch = next.end;
} else if (next.end < doc.getLine(cursor.line).length) {
cursor.ch = next.end + 1;
} else if (doc.getLine(cursor.line + 1)) {
cursor.ch = 0;
cursor.line++;
} else {
next = null;
break;
}
next = this.getToken(cursor);
} while (skipWhitespace && !/\S/.test(next.string)); // FP on skipWhitespace
}
When the rule is improved we can consider enabling it for TypeScript.
Here is an additional false positive that is most probably triggered by this issue:
https://community.sonarsource.com/t/rule-javascript-s2189-false-positive-in-loop-exit-condition/109778