Skip to content

Commit e36e16a

Browse files
authored
Merge pull request github#2079 from taus-semmle/python-unused-local-nonlocal
Approved by RasmusWL
2 parents 3f45d86 + 26da6a1 commit e36e16a

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

python/ql/src/Variables/UnusedLocalVariable.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ import python
1515
import Definition
1616

1717
predicate unused_local(Name unused, LocalVariable v) {
18-
forex(Definition def |
19-
def.getNode() = unused |
18+
forex(Definition def | def.getNode() = unused |
2019
def.getVariable() = v and
2120
def.isUnused() and
2221
not exists(def.getARedef()) and
2322
def.isRelevant() and
23+
not v = any(Nonlocal n).getAVariable() and
2424
not exists(def.getNode().getParentNode().(FunctionDef).getDefinedFunction().getADecorator()) and
2525
not exists(def.getNode().getParentNode().(ClassDef).getDefinedClass().getADecorator())
2626
)
2727
}
2828

29-
3029
from Name unused, LocalVariable v
31-
where unused_local(unused, v) and
32-
// If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
33-
forall(Name el | el = unused.getParentNode().(Tuple).getAnElt() | unused_local(el, _))
30+
where
31+
unused_local(unused, v) and
32+
// If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
33+
forall(Name el | el = unused.getParentNode().(Tuple).getAnElt() | unused_local(el, _))
3434
select unused, "The value assigned to local variable '" + v.getId() + "' is never used."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| variables_test.py:32:9:32:12 | test | The value assigned to local variable 'test' is never used. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Variables/UnusedLocalVariable.ql
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# FPs involving nonlocal
3+
4+
def nonlocal_fp():
5+
test = False
6+
def set_test():
7+
nonlocal test
8+
test = True
9+
set_test()
10+
if test:
11+
print("Test is set.")
12+
13+
nonlocal_fp()
14+
15+
def nonlocal_fp2():
16+
test = False
17+
18+
def set_test():
19+
nonlocal test
20+
test = True
21+
set_test()
22+
result = 5
23+
if not test:
24+
return
25+
return result
26+
27+
def not_fp():
28+
test = False
29+
def nonlocal_test():
30+
nonlocal test
31+
def set_test():
32+
test = True
33+
nonlocal_test()
34+
set_test()
35+
if test:
36+
print("Test is set.")

0 commit comments

Comments
 (0)