Skip to content

Commit 8b31376

Browse files
authored
Merge pull request #19743 from Napalys/js/quality/loop_shift
JS: Promote `js/loop-iteration-skipped-due-to-shifting` to the Code Quality suite
2 parents 8c2bda3 + bca536c commit 8b31376

File tree

7 files changed

+56
-2
lines changed

7 files changed

+56
-2
lines changed

javascript/ql/integration-tests/query-suite/javascript-code-quality-extended.qls.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ ql/javascript/ql/src/Statements/DanglingElse.ql
8888
ql/javascript/ql/src/Statements/IgnoreArrayResult.ql
8989
ql/javascript/ql/src/Statements/InconsistentLoopOrientation.ql
9090
ql/javascript/ql/src/Statements/LabelInCase.ql
91+
ql/javascript/ql/src/Statements/LoopIterationSkippedDueToShifting.ql
9192
ql/javascript/ql/src/Statements/MisleadingIndentationAfterControlStmt.ql
9293
ql/javascript/ql/src/Statements/ReturnAssignsLocal.ql
9394
ql/javascript/ql/src/Statements/SuspiciousUnusedLoopIterationVariable.ql

javascript/ql/integration-tests/query-suite/javascript-code-quality.qls.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ ql/javascript/ql/src/Statements/DanglingElse.ql
8888
ql/javascript/ql/src/Statements/IgnoreArrayResult.ql
8989
ql/javascript/ql/src/Statements/InconsistentLoopOrientation.ql
9090
ql/javascript/ql/src/Statements/LabelInCase.ql
91+
ql/javascript/ql/src/Statements/LoopIterationSkippedDueToShifting.ql
9192
ql/javascript/ql/src/Statements/MisleadingIndentationAfterControlStmt.ql
9293
ql/javascript/ql/src/Statements/ReturnAssignsLocal.ql
9394
ql/javascript/ql/src/Statements/SuspiciousUnusedLoopIterationVariable.ql

javascript/ql/src/Statements/LoopIterationSkippedDueToShifting.ql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* @kind problem
66
* @problem.severity warning
77
* @id js/loop-iteration-skipped-due-to-shifting
8-
* @tags correctness
8+
* @tags quality
9+
* reliability
10+
* correctness
911
* @precision high
1012
*/
1113

@@ -146,7 +148,12 @@ class ArrayIterationLoop extends ForStmt {
146148
or
147149
this.hasPathThrough(splice, cfg.getAPredecessor()) and
148150
this.getLoopEntry().dominates(cfg.getBasicBlock()) and
149-
not this.hasIndexingManipulation(cfg)
151+
not this.hasIndexingManipulation(cfg) and
152+
// Don't continue through a branch that tests the splice call's return value
153+
not exists(ConditionGuardNode guard | cfg = guard |
154+
guard.getTest() = splice.asExpr() and
155+
guard.getOutcome() = false
156+
)
150157
}
151158
}
152159

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed false positives in the `js/loop-iteration-skipped-due-to-shifting` query when the return value of `splice` is used to decide whether to adjust the loop counter.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `js/loop-iteration-skipped-due-to-shifting` query now has the `reliability` tag.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| tst.js:4:27:4:44 | parts.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
22
| tst.js:13:29:13:46 | parts.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
33
| tst.js:24:9:24:26 | parts.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
4+
| tst.js:128:11:128:33 | pending ... e(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |
5+
| tst.js:153:11:153:26 | toc.splice(i, 1) | Removing an array item without adjusting the loop index 'i' causes the subsequent array item to be skipped. |

javascript/ql/test/query-tests/Statements/LoopIterationSkippedDueToShifting/tst.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,38 @@ function inspectNextElement(string) {
121121
}
122122
return parts.join('/');
123123
}
124+
125+
function withTryCatch(pendingCSS) {
126+
for (let i = 0; i < pendingCSS.length; ++i) {
127+
try {
128+
pendingCSS.splice(i, 1); // $ SPURIOUS:Alert
129+
i -= 1;
130+
} catch (ex) {}
131+
}
132+
}
133+
134+
function andOperand(toc) {
135+
for (let i = 0; i < toc.length; i++) {
136+
toc[i].ignoreSubHeading && toc.splice(i, 1) && i--;
137+
}
138+
}
139+
140+
function ifStatement(toc) {
141+
for (let i = 0; i < toc.length; i++) {
142+
if(toc[i].ignoreSubHeading){
143+
if(toc.splice(i, 1)){
144+
i--;
145+
}
146+
}
147+
}
148+
}
149+
150+
function ifStatement2(toc) {
151+
for (let i = 0; i < toc.length; i++) {
152+
if(toc[i].ignoreSubHeading){
153+
if(!toc.splice(i, 1)){ // $Alert
154+
i--;
155+
}
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)