Skip to content

Commit

Permalink
Avoid reading shadowed bindings of loop variables when considering if…
Browse files Browse the repository at this point in the history
… UP028 is safe

Closes #13266
  • Loading branch information
zanieb committed Sep 24, 2024
1 parent 03503f7 commit 35f8477
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
26 changes: 26 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP028_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,29 @@ def _serve_method(fn):
.markup(highlight=args.region)
):
yield h


def f():
for x in (1, 2, 3):
yield x
# Shadowing with another loop
for x in (1, 2, 3):
yield x


def f():
for x in (1, 2, 3):
yield x
# Shadowing with an `except`
try:
pass
except Exception as x:
pass


def f():
for x in (1, 2, 3):
yield x
# Shadowing with `with`
with contextlib.nullcontext() as x:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt_for: &ast::StmtFor)
checker
.semantic()
.current_scope()
.get_all(name.id.as_str())
.any(|binding_id| {
.get(name.id.as_str())
.map(|binding_id| {
let binding = checker.semantic().binding(binding_id);
binding.references.iter().any(|reference_id| {
checker.semantic().reference(*reference_id).range() != name.range()
})
})
.unwrap_or(false)
}) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,52 @@ UP028_0.py:79:5: UP028 [*] Replace `yield` over `for` loop with `yield from`
82 |- ):
83 |- yield h
82 |+ )
84 83 |
85 84 |
86 85 | def f():

UP028_0.py:95:5: UP028 [*] Replace `yield` over `for` loop with `yield from`
|
94 | def f():
95 | for x in (1, 2, 3):
| _____^
96 | | yield x
| |_______________^ UP028
97 | # Shadowing with an `except`
98 | try:
|
= help: Replace with `yield from`

Unsafe fix
92 92 |
93 93 |
94 94 | def f():
95 |- for x in (1, 2, 3):
96 |- yield x
95 |+ yield from (1, 2, 3)
97 96 | # Shadowing with an `except`
98 97 | try:
99 98 | pass

UP028_0.py:105:5: UP028 [*] Replace `yield` over `for` loop with `yield from`
|
104 | def f():
105 | for x in (1, 2, 3):
| _____^
106 | | yield x
| |_______________^ UP028
107 | # Shadowing with `with`
108 | with contextlib.nullcontext() as x:
|
= help: Replace with `yield from`

Unsafe fix
102 102 |
103 103 |
104 104 | def f():
105 |- for x in (1, 2, 3):
106 |- yield x
105 |+ yield from (1, 2, 3)
107 106 | # Shadowing with `with`
108 107 | with contextlib.nullcontext() as x:
109 108 | pass
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
---
UP028_1.py:78:5: UP028 [*] Replace `yield` over `for` loop with `yield from`
|
77 | def f(x):
78 | for x in y:
| _____^
79 | | yield x
| |_______________^ UP028
80 | del x
|
= help: Replace with `yield from`

Unsafe fix
75 75 |
76 76 |
77 77 | def f(x):
78 |- for x in y:
79 |- yield x
78 |+ yield from y
80 79 | del x
81 80 |
82 81 |

0 comments on commit 35f8477

Please sign in to comment.