Skip to content

Commit 7e5e5ae

Browse files
committed
JS: use guard nodes instead of synactic isConditional check
1 parent 336f605 commit 7e5e5ae

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

javascript/ql/src/Statements/UselessConditional.ql

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,21 @@ predicate whitelist(Expr e) {
115115
}
116116

117117
/**
118-
* Holds if `e` is part of a conditional node `cond` that evaluates
119-
* `e` and checks its value for truthiness.
118+
* Gets the `&&` expression that defines this guard node, if any.
120119
*/
121-
predicate isConditional(ASTNode cond, Expr e) {
122-
e = cond.(IfStmt).getCondition() or
123-
e = cond.(ConditionalExpr).getCondition() or
124-
e = cond.(LogAndExpr).getLeftOperand() or
125-
e = cond.(LogOrExpr).getLeftOperand()
120+
LogAndExpr getLogAndExpr(ConditionGuardNode guard) {
121+
result.getLeftOperand().stripParens() = guard.getTest()
126122
}
127123

128-
from ASTNode cond, DataFlow::AnalyzedNode op, boolean cv, ASTNode sel, string msg
129-
where isConditional(cond, op.asExpr()) and
124+
from ConditionGuardNode guard, DataFlow::AnalyzedNode op, boolean cv, ASTNode sel, string msg
125+
where guard.getTest() = op.asExpr() and
130126
cv = op.getTheBooleanValue()and
131127
not whitelist(op.asExpr()) and
132128

133129
// if `cond` is of the form `<non-trivial truthy expr> && <something>`,
134130
// we suggest replacing it with `<non-trivial truthy expr>, <something>`
135-
if cond instanceof LogAndExpr and cv = true and not op.asExpr().isPure() then
136-
(sel = cond and msg = "This logical 'and' expression can be replaced with a comma expression.")
131+
if exists(getLogAndExpr(guard)) and cv = true and not op.asExpr().isPure() then
132+
(sel = getLogAndExpr(guard) and msg = "This logical 'and' expression can be replaced with a comma expression.")
137133

138134
// otherwise we just report that `op` always evaluates to `cv`
139135
else (

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| UselessConditional.js:5:7:5:12 | !lines | This expression always evaluates to false. |
1+
| UselessConditional.js:5:8:5:12 | lines | Variable 'lines' always evaluates to true here. |
22
| UselessConditional.js:12:34:12:79 | (v = ne ... k] = v) | This logical 'and' expression can be replaced with a comma expression. |
33
| UselessConditional.js:17:9:17:9 | a | Variable 'a' always evaluates to false here. |
44
| UselessConditional.js:18:9:18:9 | b | Variable 'b' always evaluates to false here. |

0 commit comments

Comments
 (0)