This rule raises an issue when return, break, or continue statements are used to exit a finally
block.
Using return, break, or continue statements that exit a finally block can lead to confusing
control flow and unexpected behavior. When these statements exit a finally block, they can mask exceptions that were raised in the
corresponding try or except blocks, making debugging difficult.
Starting with Python 3.14, this pattern emits a SyntaxWarning as specified in PEP 765, indicating that this usage is discouraged and
may become a syntax error in future Python versions. The finally block is intended for cleanup code that should always execute,
regardless of how the try block exits. When control flow statements exit the finally block, they can interfere with this
guarantee.
Additionally, this pattern can make code harder to understand and maintain, as the actual exit point becomes unclear when reading the code.
Using control flow statements that exit finally blocks can lead to:
Move the return statement outside the finally block and use a variable to store the return value if needed.
def risky_function():
try:
do_something()
finally:
cleanup()
return "done" # Noncompliant
def safe_function():
result = None
try:
do_something()
result = "done"
finally:
cleanup()
return result