Skip to content

Commit 595fe21

Browse files
author
Esben Sparre Andreasen
committed
JS: support noop parentheses in js/useless-assignment-to-local
The syntatic recognizer `isNullOrUndef` did not handle expressions that were wrapped in parentheses. This eliminates some results here: https://lgtm.com/projects/g/vuejs/vue/alerts?mode=tree&ruleFocus=7900088
1 parent d2f11dc commit 595fe21

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

change-notes/1.19/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
| **Query** | **Expected impact** | **Change** |
2121
|--------------------------------|----------------------------|----------------------------------------------|
22+
| Useless assignment to local variable | Fewer false-positive results | This rule now recognizes additional ways default values can be set. |
2223
| Regular expression injection | Fewer false-positive results | This rule now identifies calls to `String.prototype.search` with more precision. |
2324
| Unbound event handler receiver | Fewer false-positive results | This rule now recognizes additional ways class methods can be bound. |
2425
| Remote property injection | Fewer results | The precision of this rule has been revised to "medium". Results are no longer shown on LGTM by default. |

javascript/ql/src/Declarations/DeadStoreOfLocal.ql

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ predicate deadStoreOfLocal(VarDef vd, PurelyLocalVariable v) {
3333
* is itself an expression evaluating to `null` or `undefined`.
3434
*/
3535
predicate isNullOrUndef(Expr e) {
36-
// `null` or `undefined`
37-
e instanceof NullLiteral or
38-
e.(VarAccess).getName() = "undefined" or
39-
e instanceof VoidExpr or
40-
// recursive case to catch multi-assignments of the form `x = y = null`
41-
isNullOrUndef(e.(AssignExpr).getRhs())
36+
exists (Expr inner |
37+
inner = e.stripParens() |
38+
// `null` or `undefined`
39+
inner instanceof NullLiteral or
40+
inner.(VarAccess).getName() = "undefined" or
41+
inner instanceof VoidExpr or
42+
// recursive case to catch multi-assignments of the form `x = y = null`
43+
isNullOrUndef(inner.(AssignExpr).getRhs())
44+
)
4245
}
4346

4447
/**

javascript/ql/test/query-tests/Declarations/DeadStoreOfLocal/tst.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,15 @@ function v() {
141141
x = 23;
142142
x = 42;
143143
return x;
144-
}
144+
}
145+
146+
(function(){
147+
var x = (void 0);
148+
var y = ((void 0));
149+
var z1 = z2 = (void 0);
150+
x = 42;
151+
y = 42;
152+
z1 = 42;
153+
z2 = 42;
154+
return x + y + z1 + z2;
155+
});

0 commit comments

Comments
 (0)