mirror of
https://github.com/github/codeql.git
synced 2026-05-02 20:25:13 +02:00
Merge pull request #2079 from taus-semmle/python-unused-local-nonlocal
Approved by RasmusWL
This commit is contained in:
@@ -15,20 +15,20 @@ import python
|
||||
import Definition
|
||||
|
||||
predicate unused_local(Name unused, LocalVariable v) {
|
||||
forex(Definition def |
|
||||
def.getNode() = unused |
|
||||
forex(Definition def | def.getNode() = unused |
|
||||
def.getVariable() = v and
|
||||
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())
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
from Name unused, LocalVariable v
|
||||
where unused_local(unused, v) and
|
||||
// If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
|
||||
forall(Name el | el = unused.getParentNode().(Tuple).getAnElt() | unused_local(el, _))
|
||||
where
|
||||
unused_local(unused, v) and
|
||||
// If unused is part of a tuple, count it as unused if all elements of that tuple are unused.
|
||||
forall(Name el | el = unused.getParentNode().(Tuple).getAnElt() | unused_local(el, _))
|
||||
select unused, "The value assigned to local variable '" + v.getId() + "' is never used."
|
||||
|
||||
@@ -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