CodeQL documentation

‘break’ or ‘return’ statement in finally

ID: py/exit-from-finally
Kind: problem
Security severity: 
Severity: warning
Precision: medium
Tags:
   - reliability
   - maintainability
   - external/cwe/cwe-584
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

When a break or return statement is used in a finally block this causes the try-finally block to exit immediately discarding the exception. This is unlikely to be the intention of the developer and makes the code more difficult to read.

Recommendation

Either move the break or return statement to immediately after the finally block or use an explicit except block to handle the exception.

These modifications are behavior changing so you must take care to ensure that the resulting behavior is correct.

References

  • © GitHub, Inc.
  • Terms
  • Privacy