Python: Teach py/unused-local-variable about nonlocal.

This commit is contained in:
Taus Brock-Nannestad
2019-10-03 17:56:29 +02:00
parent 7a57a3c743
commit 5946a4a066
4 changed files with 39 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ predicate unused_local(Name unused, LocalVariable v) {
def.isUnused() and
not exists(def.getARedef()) and
def.isRelevant() and
not v = any(Nonlocal n).getAVariable() and
not exists(def.getNode().getParentNode().(FunctionDef).getDefinedFunction().getADecorator()) and
not exists(def.getNode().getParentNode().(ClassDef).getDefinedClass().getADecorator())
)

View File

@@ -0,0 +1 @@
| variables_test.py:32:9:32:12 | test | The value assigned to local variable 'test' is never used. |

View File

@@ -0,0 +1 @@
Variables/UnusedLocalVariable.ql

View File

@@ -0,0 +1,36 @@
# FPs involving nonlocal
def nonlocal_fp():
test = False
def set_test():
nonlocal test
test = True
set_test()
if test:
print("Test is set.")
nonlocal_fp()
def nonlocal_fp2():
test = False
def set_test():
nonlocal test
test = True
set_test()
result = 5
if not test:
return
return result
def not_fp():
test = False
def nonlocal_test():
nonlocal test
def set_test():
test = True
nonlocal_test()
set_test()
if test:
print("Test is set.")