diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP028_0.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP028_0.py index 08f382c956fd2..35c1639a9ca34 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP028_0.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP028_0.py @@ -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 diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs index 8e0b9448812c0..2bc3ab6d65779 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs @@ -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; } diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_0.py.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_0.py.snap index 8d5334bbd0da6..d5fc7a0540eb9 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_0.py.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_0.py.snap @@ -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 diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_1.py.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_1.py.snap index 2bacb5d540775..fe66cf1dbd872 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_1.py.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP028_1.py.snap @@ -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 |