Skip to content

Commit ff5a98b

Browse files
authored
Merge pull request #2074 from taus-semmle/python-unreachable-nonlocal
Approved by RasmusWL
2 parents e36e16a + 384013e commit ff5a98b

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

python/ql/test/query-tests/Statements/unreachable_nonlocal/UnreachableCode.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Statements/UnreachableCode.ql
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
def nonlocal_fp():
3+
test = False
4+
def set_test():
5+
nonlocal test
6+
test = True
7+
set_test()
8+
if test:
9+
raise Exception("Foo")
10+
11+
12+
# A couple of false negatives, roughly in order of complexity
13+
14+
# test is nonlocal, but not mutated
15+
def nonlocal_fn1():
16+
test = False
17+
def set_test():
18+
nonlocal test
19+
set_test()
20+
if test:
21+
raise Exception("Foo")
22+
23+
# test is nonlocal and mutated, but does not change truthiness
24+
def nonlocal_fn2():
25+
test = False
26+
def set_test():
27+
nonlocal test
28+
test = 0
29+
set_test()
30+
if test:
31+
raise Exception("Foo")
32+
33+
# test is nonlocal and changes truthiness, but the function is never called
34+
def nonlocal_fn3():
35+
test = False
36+
def set_test():
37+
nonlocal test
38+
test = True
39+
if test:
40+
raise Exception("Foo")
41+
42+
# test is nonlocal and changes truthiness, but only if the given argument is true
43+
def nonlocal_fn4(x):
44+
test = False
45+
def set_test():
46+
nonlocal test
47+
test = True
48+
if x:
49+
set_test()
50+
if test:
51+
raise Exception("Foo")

0 commit comments

Comments
 (0)