Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion javascript/ql/lib/Expressions/ExprHasNoEffect.qll
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ predicate inVoidContext(Expr e) {
)
)
or
// propagate void context through parenthesized expressions
inVoidContext(e.getParent().(ParExpr))
or
exists(SeqExpr seq, int i, int n |
e = seq.getOperand(i) and
n = seq.getNumOperands()
Expand Down Expand Up @@ -129,6 +132,19 @@ predicate noSideEffects(Expr e) {
)
}

/**
* Holds if `e` is a compound expression that may contain sub-expressions with side effects.
* We should not flag these directly as useless since we want to flag only the innermost
* expressions that actually have no effect.
*/
predicate isCompoundExpression(Expr e) {
e instanceof LogicalBinaryExpr
or
e instanceof SeqExpr
or
e instanceof ParExpr
}

/**
* Holds if the expression `e` should be reported as having no effect.
*/
Expand All @@ -145,6 +161,7 @@ predicate hasNoEffect(Expr e) {
not isDeclaration(e) and
// exclude DOM properties, which sometimes have magical auto-update properties
not isDomProperty(e.(PropAccess).getPropertyName()) and
not isCompoundExpression(e) and
// exclude xUnit.js annotations
not e instanceof XUnitAnnotation and
// exclude common patterns that are most likely intentional
Expand All @@ -157,7 +174,17 @@ predicate hasNoEffect(Expr e) {
not exists(fe.getName())
) and
// exclude block-level flow type annotations. For example: `(name: empty)`.
not e.(ParExpr).getExpression().getLastToken().getNextToken().getValue() = ":" and
not exists(ParExpr parent |
e.getParent() = parent and
e.getLastToken().getNextToken().getValue() = ":"
) and
// exclude expressions that are part of a conditional expression
not exists(ConditionalExpr cond | e = cond.getABranch() |
e instanceof NullLiteral or
e.(GlobalVarAccess).getName() = "undefined" or
e.(NumberLiteral).getIntValue() = 0 or
e instanceof VoidExpr
) and
// exclude the first statement of a try block
not e = any(TryStmt stmt).getBody().getStmt(0).(ExprStmt).getExpr() and
// exclude expressions that are alone in a file, and file doesn't contain a function.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `js/useless-expression` query now correctly flags only the innermost expressions with no effect, avoiding duplicate alerts on compound expressions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| dom.js:2:33:2:50 | a.clientTop === !0 | This expression has no effect. |
| try.js:22:9:22:26 | x.ordinaryProperty | This expression has no effect. |
| tst2.js:2:4:2:4 | 0 | This expression has no effect. |
| tst.js:3:1:3:2 | 23 | This expression has no effect. |
Expand All @@ -11,4 +12,4 @@
| tst.js:50:3:50:36 | new Err ... age(e)) | This expression has no effect. |
| tst.js:61:2:61:20 | o.trivialNonGetter1 | This expression has no effect. |
| tst.js:77:24:77:24 | o | This expression has no effect. |
| uselessfn.js:1:1:1:26 | (functi ... .");\\n}) | This expression has no effect. |
| uselessfn.js:1:2:1:26 | functio ... d.");\\n} | This expression has no effect. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function f(){
a.clientTop && a.clientTop, a.clientTop === !0; //$Alert
a && a.clientTop;
a.clientTop, a.clientTop;
if(a) return a.clientTop && a.clientTop, a.clientTop === !0;
if(b) return b && (b.clientTop, b.clientTop && b.clientTop), null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ function g() {

consume(testSomeCondition() ? o :
doSomethingDangerous());

("release" === isRelease() ? warning() : null);
"release" === isRelease() ? warning() : null;
"release" === isRelease() ? warning() : 0;
"release" === isRelease() ? warning() : undefined;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(function f() { // $ Alert
console.log("I'm never called.");
})
})