mirror of
https://github.com/github/codeql.git
synced 2026-04-23 15:55:18 +02:00
improve alert message for js/useless-assignment-to-local
This commit is contained in:
@@ -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."
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user