ruff/crates/ruff_linter/resources/test/fixtures/control-flow-graph/try.py
Auguste Lalande 450d4e0e0c
[pylint] Fix unreachable infinite loop (PLW0101) (#15278)
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

Fix infinite loop issue reported here #15248.
The issue was caused by the break inside the if block, which caused the
flow to exit in an unforeseen way. This caused other issues, eventually
leading to an infinite loop.

Resolves #15248. Resolves #15336.

## Test Plan

Added failing code to fixture.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: dylwil3 <dylwil3@gmail.com>
2025-01-08 09:45:04 -06:00

130 lines
2.2 KiB
Python

def func():
try:
print("try")
except Exception:
print("Exception")
except OtherException as e:
print("OtherException")
else:
print("else")
finally:
print("finally")
def func():
try:
print("try")
except:
print("Exception")
def func():
try:
print("try")
except:
print("Exception")
except OtherException as e:
print("OtherException")
def func():
try:
print("try")
except Exception:
print("Exception")
except OtherException as e:
print("OtherException")
def func():
try:
print("try")
except Exception:
print("Exception")
except OtherException as e:
print("OtherException")
else:
print("else")
def func():
try:
print("try")
finally:
print("finally")
def func():
try:
return 0
except:
return 1
finally:
return 2
def func():
try:
raise Exception()
except:
print("reached")
def func():
try:
assert False
print("unreachable")
except:
print("reached")
def func():
try:
raise Exception()
finally:
print('reached')
return 2
def func():
try:
assert False
print("unreachable")
finally:
print("reached")
# Test case from ibis caused overflow
def func():
try:
if catalog is not None:
try:
x = 0
except PySparkParseException:
x = 1
try:
x = 2
except PySparkParseException:
x = 3
x = 8
finally:
if catalog is not None:
try:
x = 4
except PySparkParseException:
x = 5
try:
x = 6
except PySparkParseException:
x = 7
def func():
try:
assert False
except ex:
raise ex
finally:
raise Exception("other")
# previously caused infinite loop
# found by fuzzer
def func():
for i in():
try:
try:
while r:
if t:break
finally:()
return
except:l