Merge pull request #2074 from taus-semmle/python-unreachable-nonlocal

Approved by RasmusWL
This commit is contained in:
semmle-qlci
2019-10-07 15:45:24 +01:00
committed by GitHub
3 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1 @@
Statements/UnreachableCode.ql

View File

@@ -0,0 +1,51 @@
def nonlocal_fp():
test = False
def set_test():
nonlocal test
test = True
set_test()
if test:
raise Exception("Foo")
# A couple of false negatives, roughly in order of complexity
# test is nonlocal, but not mutated
def nonlocal_fn1():
test = False
def set_test():
nonlocal test
set_test()
if test:
raise Exception("Foo")
# test is nonlocal and mutated, but does not change truthiness
def nonlocal_fn2():
test = False
def set_test():
nonlocal test
test = 0
set_test()
if test:
raise Exception("Foo")
# test is nonlocal and changes truthiness, but the function is never called
def nonlocal_fn3():
test = False
def set_test():
nonlocal test
test = True
if test:
raise Exception("Foo")
# test is nonlocal and changes truthiness, but only if the given argument is true
def nonlocal_fn4(x):
test = False
def set_test():
nonlocal test
test = True
if x:
set_test()
if test:
raise Exception("Foo")