Skip to content

Commit 28f3b68

Browse files
authored
Merge pull request #380 from asger-semmle/generalize-useless-conditional
Approved by esben-semmle
2 parents dc3c5a6 + 1568d5d commit 28f3b68

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

change-notes/1.19/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
| User-controlled bypass of security check | Fewer results | This rule no longer flags conditions that guard early returns. The precision of this rule has been revised to "medium". Results are no longer shown on LGTM by default. |
4040
| Whitespace contradicts operator precedence | Fewer false-positive results | This rule no longer flags operators with asymmetric whitespace. |
4141
| Unused import | Fewer false-positive results | This rule no longer flags imports used by the `transform-react-jsx` Babel plugin. |
42+
| Useless conditional | More true-positive results | This rule now flags useless conditions in more cases. |
4243

4344
## Changes to QL libraries
4445

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: 3 additions & 2 deletions
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. |
@@ -15,7 +15,8 @@
1515
| UselessConditional.js:65:5:65:5 | x | Variable 'x' always evaluates to true here. |
1616
| UselessConditional.js:76:13:76:13 | x | Variable 'x' always evaluates to true here. |
1717
| UselessConditional.js:82:13:82:13 | x | Variable 'x' always evaluates to true here. |
18-
| UselessConditional.js:89:10:89:16 | x, true | This expression always evaluates to true. |
18+
| UselessConditional.js:94:16:94:16 | x | Variable 'x' always evaluates to false here. |
19+
| UselessConditional.js:101:18:101:18 | x | Variable 'x' always evaluates to false here. |
1920
| UselessConditionalGood.js:58:12:58:13 | x2 | Variable 'x2' always evaluates to false here. |
2021
| UselessConditionalGood.js:69:12:69:13 | xy | Variable 'xy' always evaluates to false here. |
2122
| UselessConditionalGood.js:85:12:85:13 | xy | Variable 'xy' always evaluates to false here. |

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) {} // OK
101+
if (y && x) {} // NOT OK
102+
}
103+
});
104+
92105
// semmle-extractor-options: --experimental

0 commit comments

Comments
 (0)