Skip to content

Commit 0e64c84

Browse files
authored
Merge pull request #1656 from asger-semmle/rephrase-useless-def
Approved by xiemaisi
2 parents cff8262 + 7a27ccd commit 0e64c84

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

javascript/ql/src/Declarations/DeadStoreOfLocal.ql

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import DeadStore
1616
/**
1717
* Holds if `vd` is a definition of variable `v` that is dead, that is,
1818
* the value it assigns to `v` is not read.
19+
*
20+
* Captured variables may be read by closures, so we restrict this to
21+
* purely local variables.
1922
*/
2023
predicate deadStoreOfLocal(VarDef vd, PurelyLocalVariable v) {
2124
v = vd.getAVariable() and
@@ -26,7 +29,7 @@ predicate deadStoreOfLocal(VarDef vd, PurelyLocalVariable v) {
2629
not exists(SsaExplicitDefinition ssa | ssa.defines(vd, v))
2730
}
2831

29-
from VarDef dead, PurelyLocalVariable v // captured variables may be read by closures, so don't flag them
32+
from VarDef dead, PurelyLocalVariable v, string msg
3033
where
3134
deadStoreOfLocal(dead, v) and
3235
// the variable should be accessed somewhere; otherwise it will be flagged by UnusedVariable
@@ -51,5 +54,13 @@ where
5154
// don't flag exported variables
5255
not any(ES2015Module m).exportsAs(v, _) and
5356
// don't flag 'exports' assignments in closure modules
54-
not any(Closure::ClosureModule mod).getExportsVariable() = v
55-
select dead, "This definition of " + v.getName() + " is useless, since its value is never read."
57+
not any(Closure::ClosureModule mod).getExportsVariable() = v and
58+
(
59+
// To avoid confusion about the meaning of "definition" and "declaration" we avoid
60+
// the term "definition" when the alert location is a variable declaration.
61+
if dead instanceof VariableDeclarator then
62+
msg = "The initial value of " + v.getName() + " is unused, since it is always overwritten."
63+
else
64+
msg = "This definition of " + v.getName() + " is useless, since its value is never read."
65+
)
66+
select dead, msg
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
| overload.ts:10:12:10:14 | baz | This definition of baz is useless, since its value is never read. |
2-
| tst2.js:26:9:26:14 | x = 23 | This definition of x is useless, since its value is never read. |
2+
| tst2.js:26:9:26:14 | x = 23 | The initial value of x is unused, since it is always overwritten. |
33
| tst2.js:28:9:28:14 | x = 42 | This definition of x is useless, since its value is never read. |
44
| tst3.js:2:1:2:36 | exports ... a: 23 } | This definition of exports is useless, since its value is never read. |
55
| tst3b.js:2:18:2:36 | exports = { a: 23 } | This definition of exports is useless, since its value is never read. |
66
| tst.js:6:2:6:7 | y = 23 | This definition of y is useless, since its value is never read. |
7-
| tst.js:13:6:13:11 | a = 23 | This definition of a is useless, since its value is never read. |
7+
| tst.js:13:6:13:11 | a = 23 | The initial value of a is unused, since it is always overwritten. |
88
| tst.js:13:14:13:19 | a = 42 | This definition of a is useless, since its value is never read. |
9-
| tst.js:45:6:45:11 | x = 23 | This definition of x is useless, since its value is never read. |
10-
| tst.js:51:6:51:11 | x = 23 | This definition of x is useless, since its value is never read. |
11-
| tst.js:132:7:132:13 | {x} = o | This definition of x is useless, since its value is never read. |
12-
| tst.js:162:6:162:14 | [x] = [0] | This definition of x is useless, since its value is never read. |
9+
| tst.js:45:6:45:11 | x = 23 | The initial value of x is unused, since it is always overwritten. |
10+
| tst.js:51:6:51:11 | x = 23 | The initial value of x is unused, since it is always overwritten. |
11+
| tst.js:132:7:132:13 | {x} = o | The initial value of x is unused, since it is always overwritten. |
12+
| tst.js:162:6:162:14 | [x] = [0] | The initial value of x is unused, since it is always overwritten. |

0 commit comments

Comments
 (0)