-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathUnnecessaryDelete.ql
40 lines (37 loc) · 1.2 KB
/
UnnecessaryDelete.ql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @name Unnecessary delete statement in function
* @description Using a 'delete' statement to delete a local variable is
* unnecessary, because the variable is deleted automatically when
* the function exits.
* @kind problem
* @tags maintainability
* useless-code
* @problem.severity warning
* @sub-severity low
* @precision high
* @id py/unnecessary-delete
*/
import python
predicate isInsideLoop(AstNode node) {
node.getParentNode() instanceof While
or
node.getParentNode() instanceof For
or
exists(AstNode prev | isInsideLoop(prev) | node = prev.getAChildNode())
}
from Delete del, Expr e, Function f
where
f.getLastStatement() = del and
e = del.getATarget() and
f.containsInScope(e) and
not e instanceof Subscript and
not e instanceof Attribute and
not isInsideLoop(del) and
// False positive: calling `sys.exc_info` within a function results in a
// reference cycle, and an explicit call to `del` helps break this cycle.
not exists(FunctionValue ex |
ex = Value::named("sys.exc_info") and
ex.getACall().getScope() = f
)
select del, "Unnecessary deletion of local variable $@ in function $@.", e, e.toString(), f,
f.getName()