Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-return] Consider exception suppress for unnecessary assignment #9673

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_return/RET504.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,38 @@ 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
49 changes: 48 additions & 1 deletion crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,9 @@ pub(crate) fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Ex
}

if checker.enabled(Rule::UnnecessaryAssign) {
unnecessary_assign(checker, &stack);
if should_analyze_unnecessary_assign(body, checker.semantic()) {
unnecessary_assign(checker, &stack);
}
}
} else {
if checker.enabled(Rule::UnnecessaryReturnNone) {
Expand Down Expand Up @@ -864,3 +866,48 @@ fn remove_else(
)))
}
}

/// RET504
/// If the last statement is a `return` statement, and the second-to-last statement is a
/// `with` statement that suppresses an exception, then we should not analyze the `return`
/// statement for unnecessary assignments. Otherwise we will suggest removing the assignment
/// and the `with` statement, which would change the behavior of the code.
///
/// Example:
/// ```python
/// def foo(data):
/// with suppress(JSONDecoderError):
/// data = data.decode()
/// return data

fn should_analyze_unnecessary_assign(body: &[Stmt], semantic: &SemanticModel) -> bool {
if let Some(index) = body.len().checked_sub(2) {
if let Some(previous_stmt) = body.get(index) {
if contains_exception_suppress(previous_stmt, semantic) {
return false;
}
}
}
true
}

fn contains_exception_suppress(stmt: &Stmt, semantic: &SemanticModel) -> bool {
let ast::Stmt::With(ast::StmtWith { items, .. }) = stmt else {
return false;
};
items.iter().any(|item| {
let ast::WithItem {
context_expr: Expr::Call(ast::ExprCall { func, .. }),
..
} = item
else {
return false;
};
if let Some(call_path) = semantic.resolve_call_path(func) {
if call_path.as_slice() == ["contextlib", "suppress"] {
return true;
}
}
false
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,25 @@ RET504.py:365:12: RET504 [*] Unnecessary assignment to `D` before `return` state
364 |- D=0.4853881 + 3.6006116*P - 0.0117368*(P-1.3822)**2
365 |- return D
364 |+ return 0.4853881 + 3.6006116*P - 0.0117368*(P-1.3822)**2
366 365 |
367 366 |
368 367 | # contextlib suppress in with statement

RET504.py:400:12: RET504 [*] Unnecessary assignment to `y` before `return` statement
|
398 | x = 1
399 | y = y + 2
400 | return y # RET504
| ^ RET504
|
= help: Remove unnecessary assignment

ℹ Unsafe fix
396 396 | x = 2
397 397 | with contextlib.suppress(Exception):
398 398 | x = 1
399 |- y = y + 2
400 |- return y # RET504
399 |+ return y + 2