Skip to content

Commit 2c11844

Browse files
committed
Revert "Merge pull request #380 from asger-semmle/generalize-useless-conditional"
This reverts commit 28f3b68, reversing changes made to dc3c5a6.
1 parent 28f3b68 commit 2c11844

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

change-notes/1.19/analysis-javascript.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
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. |
4342

4443
## Changes to QL libraries
4544

javascript/ql/src/Statements/UselessConditional.ql

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

117117
/**
118-
* Gets the `&&` expression that defines this guard node, if any.
118+
* Holds if `e` is part of a conditional node `cond` that evaluates
119+
* `e` and checks its value for truthiness.
119120
*/
120-
LogAndExpr getLogAndExpr(ConditionGuardNode guard) {
121-
result.getLeftOperand().stripParens() = guard.getTest()
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()
122126
}
123127

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

129133
// if `cond` is of the form `<non-trivial truthy expr> && <something>`,
130134
// we suggest replacing it with `<non-trivial truthy expr>, <something>`
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.")
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.")
133137

134138
// otherwise we just report that `op` always evaluates to `cv`
135139
else (

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| UselessConditional.js:5:8:5:12 | lines | Variable 'lines' always evaluates to true here. |
1+
| UselessConditional.js:5:7:5:12 | !lines | This expression always evaluates to false. |
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,8 +15,7 @@
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: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. |
18+
| UselessConditional.js:89:10:89:16 | x, true | This expression always evaluates to true. |
2019
| UselessConditionalGood.js:58:12:58:13 | x2 | Variable 'x2' always evaluates to false here. |
2120
| UselessConditionalGood.js:69:12:69:13 | xy | Variable 'xy' always evaluates to false here. |
2221
| 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: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,4 @@ 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-
10592
// semmle-extractor-options: --experimental

0 commit comments

Comments
 (0)