Skip to content

Commit 5946a4a

Browse files
committed
Python: Teach py/unused-local-variable about nonlocal.
1 parent 7a57a3c commit 5946a4a

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

python/ql/src/Variables/UnusedLocalVariable.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ predicate unused_local(Name unused, LocalVariable v) {
2121
def.isUnused() and
2222
not exists(def.getARedef()) and
2323
def.isRelevant() and
24+
not v = any(Nonlocal n).getAVariable() and
2425
not exists(def.getNode().getParentNode().(FunctionDef).getDefinedFunction().getADecorator()) and
2526
not exists(def.getNode().getParentNode().(ClassDef).getDefinedClass().getADecorator())
2627
)
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)