improve alert message for js/useless-assignment-to-local

This commit is contained in:
Erik Krogh Kristensen
2020-03-30 19:57:43 +02:00
parent 0b4bfed726
commit 7938bc4ed0
3 changed files with 27 additions and 1 deletions

View File

@@ -29,6 +29,23 @@ predicate deadStoreOfLocal(VarDef vd, PurelyLocalVariable v) {
not exists(SsaExplicitDefinition ssa | ssa.defines(vd, v))
}
/**
* Holds if there exists another definition of the variable `v` that dominates `dead`.
*/
predicate hasDominatingDef(VarDef dead, PurelyLocalVariable v) {
exists(VarDef otherDef | not otherDef = dead and otherDef.getAVariable() = v |
dead.getBasicBlock().getASuccessor+() = otherDef.getBasicBlock()
or
exists(ReachableBasicBlock bb, int i, int j |
bb = otherDef.getBasicBlock() and bb = dead.getBasicBlock()
|
bb.defAt(i, v, dead) and
bb.defAt(j, v, otherDef) and
j > i
)
)
}
from VarDef dead, PurelyLocalVariable v, string msg
where
deadStoreOfLocal(dead, v) and
@@ -63,7 +80,7 @@ where
(
// To avoid confusion about the meaning of "definition" and "declaration" we avoid
// the term "definition" when the alert location is a variable declaration.
if dead instanceof VariableDeclarator
if dead instanceof VariableDeclarator and hasDominatingDef(dead, v)
then msg = "The initial value of " + v.getName() + " is unused, since it is always overwritten."
else msg = "This definition of " + v.getName() + " is useless, since its value is never read."
)