Skip to content

Python: Add tests for reachability when using nonlocal. #2074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Statements/UnreachableCode.ql
Original file line number Diff line number Diff line change
@@ -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")