@@ -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 */
2023predicate 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
3033where
3134 deadStoreOfLocal ( dead , v ) and
3235 // the variable should be accessed somewhere; otherwise it will be flagged by UnusedVariable
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
0 commit comments