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

Skip to content

Commit bda37b6

Browse files
committed
refactor of benignContext predicate based on code review
1 parent cdde8ae commit bda37b6

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

javascript/ql/src/Statements/UseOfReturnlessFunction.ql

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,36 @@ predicate benignContext(Expr e) {
3737
or
3838
exists(ConditionalExpr cond | cond.getABranch() = e and benignContext(cond))
3939
or
40-
exists(BinaryExpr bin | (bin.getOperator() = "&&" or bin.getOperator() = "||") and bin.getAnOperand() = e and benignContext(bin))
40+
exists(LogicalBinaryExpr bin | bin.getAnOperand() = e and benignContext(bin))
4141
or
42-
exists(SeqExpr parent | parent.getAnOperand() = e and benignContext(parent))
42+
exists(SeqExpr seq, int i, int n | e = seq.getOperand(i) and n = seq.getNumOperands() |
43+
i < n - 1 or benignContext(seq)
44+
)
4345
or
44-
exists(ParExpr par | par.getExpression() = e and benignContext(par))
45-
or
46-
exists(TypeAssertion assert | assert.getExpression() = e and inVoidContext(assert))
46+
exists(Expr parent | parent.getUnderlyingValue() = e and benignContext(parent))
4747
or
48-
exists(UnaryExpr unOp | unOp.getOperator() = "void" and unOp.getOperand() = e)
48+
exists(VoidExpr voidExpr | voidExpr.getOperand() = e)
49+
or
50+
51+
// The call is only in a non-void context because it is in a lambda.
52+
exists(ArrowFunctionExpr arrow |
53+
arrow.getBody() = e
54+
)
4955
or
5056

5157
// It is ok (or to be flagged by another query?) to await a non-async function.
52-
exists(AwaitExpr await | await.getOperand() = e)
58+
exists(AwaitExpr await | await.getOperand() = e and benignContext(await))
5359
or
5460

55-
// Avoid double reporting. It will always evaluate to false.
61+
// Avoid double reporting with js/trivial-conditional
5662
exists(IfStmt ifStmt | ifStmt.getCondition() = e)
5763
or
58-
// Avoid double reporting. `e` will always evaluate to undefined.
64+
// Avoid double reporting with js/comparison-between-incompatible-types
5965
exists(Comparison binOp | binOp.getAnOperand() = e)
6066
or
61-
// Avoid double reporting of "The base expression of this property access is always undefined.".
67+
// Avoid double reporting with js/property-access-on-non-object
6268
exists(PropAccess ac | ac.getBase() = e)
6369
or
64-
// The call is only in a non-void context because it is in a lambda.
65-
exists(ArrowFunctionExpr arrow |
66-
arrow.getBody() = e
67-
)
68-
or
6970
// Avoid double-reporting with unused local.
7071
exists(VariableDeclarator v | v.getInit() = e and v.getBindingPattern().getVariable() instanceof UnusedLocal)
7172
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
| tst.js:20:17:20:33 | onlySideEffects() | the function $@ does not return anything, yet the return value is used. | tst.js:11:5:13:5 | functio ... )\\n } | onlySideEffects |
22
| tst.js:24:13:24:29 | onlySideEffects() | the function $@ does not return anything, yet the return value is used. | tst.js:11:5:13:5 | functio ... )\\n } | onlySideEffects |
3+
| tst.js:30:20:30:36 | onlySideEffects() | the function $@ does not return anything, yet the return value is used. | tst.js:11:5:13:5 | functio ... )\\n } | onlySideEffects |

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323

2424
var b = onlySideEffects();
2525
console.log(b);
26+
27+
var c = 42 + (onlySideEffects(), 42); // OK, value is thrown away.
28+
console.log(c);
29+
30+
var d = 42 + (42, onlySideEffects()); // NOT OK!
31+
console.log(d);
2632
})();

0 commit comments

Comments
 (0)