Python: Teach py/unreachable-statement about contextlib.suppress.

This commit is contained in:
Taus Brock-Nannestad
2019-10-03 15:12:41 +02:00
parent 37291c5642
commit 99b99ef2b6
5 changed files with 49 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,31 @@
from contextlib import suppress
def raises_exception():
raise Exception("This will be suppressed")
def foo():
test = False
with suppress(Exception):
raises_exception()
test = True
if test:
return
print("An exception was raised") # FP: not reached
foo()
def bar(x):
test = False
try:
if x:
raise Exception("Bar")
test = True
except Exception:
pass
if test:
print("Test was set")
return
print("Test was not set")
bar(True)
bar(False)