Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56707fc

Browse files
committed
JS: recognize more conditionals in useless-conditional
1 parent 786377d commit 56707fc

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

javascript/ql/src/Statements/UselessConditional.ql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,12 @@ predicate whitelist(Expr e) {
120120
*/
121121
predicate isConditional(ASTNode cond, Expr e) {
122122
e = cond.(IfStmt).getCondition() or
123+
e = cond.(WhileStmt).getExpr() or
124+
e = cond.(ForStmt).getTest() or
123125
e = cond.(ConditionalExpr).getCondition() or
124-
e = cond.(LogAndExpr).getLeftOperand() or
125-
e = cond.(LogOrExpr).getLeftOperand()
126+
e = cond.(LogicalBinaryExpr).getLeftOperand() or
127+
// Include `z` in `if (x && z)`.
128+
isConditional(_, cond) and e = cond.(LogicalBinaryExpr).getRightOperand()
126129
}
127130

128131
from ASTNode cond, DataFlow::AnalyzedNode op, boolean cv, ASTNode sel, string msg
@@ -132,7 +135,7 @@ where isConditional(cond, op.asExpr()) and
132135

133136
// if `cond` is of the form `<non-trivial truthy expr> && <something>`,
134137
// we suggest replacing it with `<non-trivial truthy expr>, <something>`
135-
if cond instanceof LogAndExpr and cv = true and not op.asExpr().isPure() then
138+
if cond.(LogAndExpr).getLeftOperand() = op.asExpr() and cv = true and not op.asExpr().isPure() then
136139
(sel = cond and msg = "This logical 'and' expression can be replaced with a comma expression.")
137140

138141
// otherwise we just report that `op` always evaluates to `cv`

javascript/ql/test/query-tests/Statements/UselessConditional/UselessConditional.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
| UselessConditional.js:76:13:76:13 | x | This use of variable 'x' always evaluates to true. |
1717
| UselessConditional.js:82:13:82:13 | x | This use of variable 'x' always evaluates to true. |
1818
| UselessConditional.js:89:10:89:16 | x, true | This expression always evaluates to true. |
19+
| UselessConditional.js:94:16:94:16 | x | This use of variable 'x' always evaluates to false. |
20+
| UselessConditional.js:100:13:100:24 | true && true | This expression always evaluates to true. |
21+
| UselessConditional.js:101:18:101:18 | x | This use of variable 'x' always evaluates to false. |
1922
| UselessConditionalGood.js:58:12:58:13 | x2 | This use of variable 'x2' always evaluates to false. |
2023
| UselessConditionalGood.js:69:12:69:13 | xy | This use of variable 'xy' always evaluates to false. |
2124
| UselessConditionalGood.js:85:12:85:13 | xy | This use of variable 'xy' always evaluates to false. |

javascript/ql/test/query-tests/Statements/UselessConditional/UselessConditional.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,17 @@ async function awaitFlow(){
8989
if ((x, true));
9090
});
9191

92+
(function (x, y) {
93+
if (!x) {
94+
while (x) { // NOT OK
95+
f();
96+
}
97+
while (true) { // OK
98+
break;
99+
}
100+
if (true && true) {} // NOT OK
101+
if (y && x) {} // NOT OK
102+
}
103+
});
104+
92105
// semmle-extractor-options: --experimental

0 commit comments

Comments
 (0)