Rule javascript:S2189 false positive in loop exit condition

Language: javascript
Rule: S2189
Platform: Sonarqube 9.9.4

It is a false positive because the variable detected for control exis loop is wrong. Right one is “i”.

Messege from Sonarqube: ‘lineNumber’ is not modified in this loop.

function arraySort(tableName, column, lineNumber, columnNumber) {
	var aTable = tables[tableName];
	var arrayToSort;
	var array;
	var reverse = 0;
	if (aTable) {
		array = aTable[0];
		arrayToSort = new Array(lineNumber);

               for (i=0;i<lineNumber;i++) {
            //'lineNumber' is not modified in this loop.
                    arrayToSort[i] = new Array(2);
                    arrayToSort[i][0] = array[i][column];
                    arrayToSort[i][1] = i;
              }

	     reverse = 1 - aTable[1];
	     aTable[1] = reverse;
	} 
........
}

Hello @yago, welcome to the Sonar Community!

Thanks a lot for your feedback. I have mentioned your case in the issue Fix FP S2189 (`no-infinite-loop`) by reporting only on local vars · Issue #2410 · SonarSource/SonarJS · GitHub that we use to track this false positive.

Until it is fixed, you can workaround the problem by explicitly declaring the i variable with either the var or let keyword, depending on the specification level that you target.

for (let i=0;i<lineNumber;i++) {
    // ...
}

This should make the false positive disappear.

Cheers,

Eric.