mirror of
https://github.com/github/codeql.git
synced 2026-04-29 18:55:14 +02:00
Python: Teach py/unused-local-variable about nonlocal.
This commit is contained in:
@@ -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())
|
||||
)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
| variables_test.py:32:9:32:12 | test | The value assigned to local variable 'test' is never used. |
|
||||
@@ -0,0 +1 @@
|
||||
Variables/UnusedLocalVariable.ql
|
||||
@@ -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.")
|
||||
Reference in New Issue
Block a user