mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-24 05:50:21 +00:00
[flake8-return] Consider exception suppress for unnecessary assignment (#9673)
## Summary This review contains a fix for [RET504](https://docs.astral.sh/ruff/rules/unnecessary-assign/) (unnecessary-assign) The problem is that Ruff suggests combining a return statement inside contextlib.suppress. Even though it is an unsafe fix it might lead to an invalid code that is not equivalent to the original one. See: https://github.com/astral-sh/ruff/issues/5909 ## Test Plan ```bash cargo test ```
This commit is contained in:
parent
0045032905
commit
ad2cfa3dba
4 changed files with 147 additions and 17 deletions
|
|
@ -363,3 +363,46 @@ def foo():
|
|||
def mavko_debari(P_kbar):
|
||||
D=0.4853881 + 3.6006116*P - 0.0117368*(P-1.3822)**2
|
||||
return D
|
||||
|
||||
|
||||
# contextlib suppress in with statement
|
||||
import contextlib
|
||||
|
||||
|
||||
def foo():
|
||||
x = 2
|
||||
with contextlib.suppress(Exception):
|
||||
x = x + 1
|
||||
return x
|
||||
|
||||
|
||||
def foo(data):
|
||||
with open("in.txt") as file_out, contextlib.suppress(IOError):
|
||||
file_out.write(data)
|
||||
data = 10
|
||||
return data
|
||||
|
||||
|
||||
def foo(data):
|
||||
with open("in.txt") as file_out:
|
||||
file_out.write(data)
|
||||
with contextlib.suppress(IOError):
|
||||
data = 10
|
||||
return data
|
||||
|
||||
|
||||
def foo():
|
||||
y = 1
|
||||
x = 2
|
||||
with contextlib.suppress(Exception):
|
||||
x = 1
|
||||
y = y + 2
|
||||
return y # RET504
|
||||
|
||||
|
||||
def foo():
|
||||
y = 1
|
||||
if y > 0:
|
||||
with contextlib.suppress(Exception):
|
||||
y = 2
|
||||
return y
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue