Skip to content

Commit d5ef97f

Browse files
[flake8-return] Fix false positive when variable is read in finally clause (RET504) (#25441)
<!-- Thank you for contributing to Ruff/ty! 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? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? - Does this PR follow our AI policy (https://github.com/astral-sh/.github/blob/main/AI_POLICY.md)? --> ## Summary Fixes #17292 RET504 flagged assignments as unnecessary even when the variable was read in `finally`/`except`, breaking runtime behavior on fix. Checks that the binding has only one reference (the return itself) before flagging ## Test Plan - Fixture cases for finally, except, nested try, and the still-fires case. - ran ecosystem checks locally and verified expected results --------- Co-authored-by: Brent Westbrook <[email protected]>
1 parent 64bc5a9 commit d5ef97f

3 files changed

Lines changed: 331 additions & 7 deletions

File tree

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
# `unnecessary-assign` (`RET504`)
2+
3+
```toml
4+
lint.select = ["RET504"]
5+
```
6+
7+
RET504 is suppressed only when the assigned name is read in an enclosing `finally` suite, which
8+
runs after the `return`. Reads elsewhere (sibling branches, `except` handlers) don't run after the
9+
`return`, so they don't keep the assignment alive.
10+
11+
## Variable read in the enclosing `finally`
12+
13+
```py
14+
def f():
15+
out = ""
16+
try:
17+
out = foo()
18+
return out
19+
except Exception as e:
20+
out = str(e)
21+
finally:
22+
log(out)
23+
```
24+
25+
A closure captured in `finally` reads the name from another scope:
26+
27+
```py
28+
def f():
29+
try:
30+
x = foo()
31+
return x
32+
finally:
33+
def _cleanup():
34+
log(x)
35+
_cleanup()
36+
```
37+
38+
Outer `finally` reads the name across a nested `try`:
39+
40+
```py
41+
def f():
42+
x = ""
43+
try:
44+
try:
45+
x = foo()
46+
return x
47+
except:
48+
pass
49+
finally:
50+
log(x)
51+
```
52+
53+
The outer `finally` runs after a `return` in an inner `finally`:
54+
55+
```py
56+
def f():
57+
x = ""
58+
try:
59+
try:
60+
pass
61+
finally:
62+
x = foo()
63+
return x
64+
finally:
65+
log(x)
66+
```
67+
68+
The `finally` also runs after a `return` in the `else` clause:
69+
70+
```py
71+
def f():
72+
try:
73+
pass
74+
except Exception:
75+
pass
76+
else:
77+
x = compute()
78+
return x
79+
finally:
80+
log(x)
81+
```
82+
83+
And after a `return` in an `except` handler:
84+
85+
```py
86+
def f():
87+
try:
88+
pass
89+
except Exception:
90+
x = recover()
91+
return x
92+
finally:
93+
log(x)
94+
```
95+
96+
The assignment may also come from a `with` body inside the `try`:
97+
98+
```py
99+
def f():
100+
try:
101+
with open("f") as fh:
102+
x = fh.read()
103+
return x
104+
finally:
105+
log(x)
106+
```
107+
108+
## Augmented assignment in `finally` reads the name
109+
110+
```py
111+
def f():
112+
try:
113+
x = foo()
114+
return x
115+
finally:
116+
x += 1
117+
```
118+
119+
```py
120+
def f():
121+
try:
122+
x = foo()
123+
return x
124+
finally:
125+
if cond():
126+
x += 1
127+
```
128+
129+
## `del` of the name in `finally`
130+
131+
Removing the assignment would leave the name unbound, so `del x` would raise `UnboundLocalError`:
132+
133+
```py
134+
def f():
135+
try:
136+
x = foo()
137+
return x
138+
finally:
139+
del x
140+
```
141+
142+
```py
143+
def f():
144+
try:
145+
x = foo()
146+
return x
147+
finally:
148+
if cond():
149+
del x
150+
```
151+
152+
## A read after a `finally` rebind still suppresses
153+
154+
A rebind in `finally` makes the assignment redundant, but distinguishing a rebind that kills the
155+
value from a plain read needs control-flow analysis we don't do here. We conservatively treat the
156+
later read as observing the assignment.
157+
158+
```py
159+
def f():
160+
try:
161+
x = foo()
162+
return x
163+
finally:
164+
x = "done"
165+
log(x)
166+
```
167+
168+
```py
169+
def f():
170+
try:
171+
x = foo()
172+
return x
173+
finally:
174+
x: str = "done"
175+
log(x)
176+
```
177+
178+
```py
179+
def f():
180+
try:
181+
x = foo()
182+
return x
183+
finally:
184+
x, _ = ("done", 0)
185+
log(x)
186+
```
187+
188+
```py
189+
def f():
190+
try:
191+
x = foo()
192+
return x
193+
finally:
194+
if cond():
195+
x = "done"
196+
log(x)
197+
```
198+
199+
## A read in an `except` handler fires
200+
201+
An `except` handler is an alternative path: if it runs, the `try` assignment never completed, so
202+
removing the assignment doesn't change what the handler reads.
203+
204+
```py
205+
def f():
206+
result = None
207+
try:
208+
result = compute()
209+
return result # error: [unnecessary-assign]
210+
except Exception as e:
211+
log(result)
212+
```
213+
214+
## `finally` doesn't read the name
215+
216+
```py
217+
def f():
218+
try:
219+
x = foo()
220+
return x # error: [unnecessary-assign]
221+
finally:
222+
log("done")
223+
```
224+
225+
```py
226+
def f():
227+
try:
228+
x = foo()
229+
return x # error: [unnecessary-assign]
230+
finally:
231+
x = "done"
232+
```
233+
234+
## Assignment and return both inside `finally`
235+
236+
```py
237+
def f():
238+
try:
239+
pass
240+
finally:
241+
x = foo()
242+
return x # error: [unnecessary-assign]
243+
```
244+
245+
## `return` in an `except` handler with no later read
246+
247+
```py
248+
def f():
249+
try:
250+
entry = fetch()
251+
except AlreadyExists:
252+
entry = lookup()
253+
result = to_dict(entry)
254+
return result # error: [unnecessary-assign]
255+
```
256+
257+
## Same name assigned and returned in sibling branches
258+
259+
Each branch's assignment is independently redundant. A later branch reusing the name doesn't
260+
observe an earlier branch's value, so both fire.
261+
262+
```py
263+
def f(cond):
264+
if cond:
265+
x = compute()
266+
return x # error: [unnecessary-assign]
267+
else:
268+
x = other()
269+
return x # error: [unnecessary-assign]
270+
```
271+
272+
The same holds when the branches are `try` arms without a `finally`:
273+
274+
```py
275+
def f():
276+
try:
277+
x = compute()
278+
return x # error: [unnecessary-assign]
279+
except Exception:
280+
x = fallback()
281+
return x # error: [unnecessary-assign]
282+
```

‎crates/ruff_linter/src/rules/flake8_return/rules/function.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ pub(crate) fn unnecessary_assign(checker: &Checker, function_stmt: &Stmt) {
568568
let Some(function_scope) = checker.semantic().function_scope(function_def) else {
569569
return;
570570
};
571-
for (assign, return_, stmt) in &stack.assignment_return {
571+
for (assign, return_, stmt, enclosing_finally) in &stack.assignment_return {
572572
// Identify, e.g., `return x`.
573573
let Some(value) = return_.value.as_ref() else {
574574
continue;
@@ -617,6 +617,22 @@ pub(crate) fn unnecessary_assign(checker: &Checker, function_stmt: &Stmt) {
617617
else {
618618
continue;
619619
};
620+
// Ignore assignments whose name is read or deleted in an enclosing `finally`, which runs
621+
// after the `return`. A reference resolving to a later rebinding in the `finally` counts
622+
// too, so check every binding of the name.
623+
if !enclosing_finally.is_empty()
624+
&& function_scope
625+
.get_all(assigned_id)
626+
.flat_map(|binding_id| checker.semantic().binding(binding_id).references())
627+
.map(|reference_id| checker.semantic().reference(reference_id))
628+
.any(|reference| {
629+
enclosing_finally
630+
.iter()
631+
.any(|finally_range| finally_range.contains_range(reference.range()))
632+
})
633+
{
634+
continue;
635+
}
620636
// Check if there's any reference made to `assigned_binding` in another scope, e.g, nested
621637
// functions. If there is, ignore them.
622638
if assigned_binding

‎crates/ruff_linter/src/rules/flake8_return/visitor.rs‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_hash::FxHashSet;
44
use ruff_python_ast::visitor;
55
use ruff_python_ast::visitor::Visitor;
66
use ruff_python_semantic::SemanticModel;
7+
use ruff_text_size::{Ranged, TextRange};
78

89
#[derive(Default)]
910
pub(super) struct Stack<'data> {
@@ -30,11 +31,16 @@ pub(super) struct Stack<'data> {
3031
pub(super) annotations: FxHashSet<&'data str>,
3132
/// Whether the current function is a generator.
3233
pub(super) is_generator: bool,
33-
/// The `assignment`-to-`return` statement pairs in the current function.
34+
/// The `assignment`-to-`return` statement pairs in the current function, each paired with the
35+
/// ranges of any enclosing `finally` suites that run after the `return`.
3436
/// TODO(charlie): Remove the extra [`Stmt`] here, which is necessary to support statement
3537
/// removal for the `return` statement.
36-
pub(super) assignment_return:
37-
Vec<(&'data ast::StmtAssign, &'data ast::StmtReturn, &'data Stmt)>,
38+
pub(super) assignment_return: Vec<(
39+
&'data ast::StmtAssign,
40+
&'data ast::StmtReturn,
41+
&'data Stmt,
42+
Vec<TextRange>,
43+
)>,
3844
}
3945

4046
pub(super) struct ReturnVisitor<'semantic, 'data> {
@@ -57,6 +63,20 @@ impl<'semantic, 'data> ReturnVisitor<'semantic, 'data> {
5763
parents: Vec::new(),
5864
}
5965
}
66+
67+
/// Return the enclosing `finally` suites that run after this `return`.
68+
fn enclosing_finally(&self, stmt_return: &ast::StmtReturn) -> Vec<TextRange> {
69+
self.parents
70+
.iter()
71+
.filter_map(|parent| parent.as_try_stmt())
72+
.filter_map(|stmt_try| {
73+
let first = stmt_try.finalbody.first()?;
74+
let last = stmt_try.finalbody.last()?;
75+
Some(TextRange::new(first.start(), last.end()))
76+
})
77+
.filter(|finally_range| !finally_range.contains_range(stmt_return.range()))
78+
.collect()
79+
}
6080
}
6181

6282
impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
@@ -128,9 +148,13 @@ impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
128148
// return x
129149
// ```
130150
Stmt::Assign(stmt_assign) => {
131-
self.stack
132-
.assignment_return
133-
.push((stmt_assign, stmt_return, stmt));
151+
let enclosing_finally = self.enclosing_finally(stmt_return);
152+
self.stack.assignment_return.push((
153+
stmt_assign,
154+
stmt_return,
155+
stmt,
156+
enclosing_finally,
157+
));
134158
}
135159
// Example:
136160
// ```python
@@ -144,10 +168,12 @@ impl<'a> Visitor<'a> for ReturnVisitor<'_, 'a> {
144168
with.body.last().and_then(Stmt::as_assign_stmt)
145169
{
146170
if !has_conditional_body(with, self.semantic) {
171+
let enclosing_finally = self.enclosing_finally(stmt_return);
147172
self.stack.assignment_return.push((
148173
stmt_assign,
149174
stmt_return,
150175
stmt,
176+
enclosing_finally,
151177
));
152178
}
153179
}

0 commit comments

Comments
 (0)