only flag unused array-destructs if it is the last variable

This commit is contained in:
Erik Krogh Kristensen
2020-09-02 11:19:50 +02:00
parent 29b3759655
commit a24db09418
3 changed files with 37 additions and 0 deletions

View File

@@ -45,11 +45,28 @@ predicate countingLoop(EnhancedForLoop efl) {
)
}
/**
* Holds if `iter` is the non-last variable of array-destructuring in a for-loop.
* E.g. `for(const [foo, iter, bar] of array) {..}`.
*/
predicate isNonLastDestructedArrayElement(PurelyLocalVariable iter) {
exists(ArrayPattern pattern | pattern = any(EnhancedForLoop loop).getLValue() |
iter = pattern.getAVariable() and
iter =
pattern
.getElement([0 .. pattern.getSize() - 2])
.(BindingPattern)
.getABindingVarRef()
.getVariable()
)
}
from EnhancedForLoop efl, PurelyLocalVariable iter
where
iter = efl.getAnIterationVariable() and
not exists(SsaExplicitDefinition ssa | ssa.defines(efl.getIteratorExpr(), iter)) and
exists(ReachableBasicBlock body | body.getANode() = efl.getBody() | body.getASuccessor+() = body) and
not countingLoop(efl) and
not isNonLastDestructedArrayElement(iter) and
not iter.getName().toLowerCase().regexpMatch("(_|dummy|unused).*")
select efl.getIterator(), "For loop variable " + iter.getName() + " is not used in the loop body."