diff --git a/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py b/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py index 526c999441..a2e6c623f7 100644 --- a/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py +++ b/crates/ruff/resources/test/fixtures/pyflakes/F841_3.py @@ -147,3 +147,10 @@ def f() -> None: global CONSTANT CONSTANT = 1 CONSTANT = 2 + + +def f() -> None: + try: + print("hello") + except A as e : + print("oh no!") diff --git a/crates/ruff/src/rules/pyflakes/fixes.rs b/crates/ruff/src/rules/pyflakes/fixes.rs index 4d2bcdc986..59d8c14910 100644 --- a/crates/ruff/src/rules/pyflakes/fixes.rs +++ b/crates/ruff/src/rules/pyflakes/fixes.rs @@ -115,6 +115,7 @@ pub(crate) fn remove_exception_handler_assignment( // Lex forwards, to the `:` token. let following = SimpleTokenizer::starts_at(bound_exception.range.end(), locator.contents()) + .skip_trivia() .next() .context("expected the exception name to be followed by a colon")?; debug_assert!(matches!(following.kind, SimpleTokenKind::Colon)); diff --git a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap index be49b03119..3484053324 100644 --- a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap +++ b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F841_F841_3.py.snap @@ -584,4 +584,22 @@ F841_3.py:139:17: F841 Local variable `x` is assigned to but never used | = help: Remove assignment to unused variable `x` +F841_3.py:155:17: F841 [*] Local variable `e` is assigned to but never used + | +153 | try: +154 | print("hello") +155 | except A as e : + | ^ F841 +156 | print("oh no!") + | + = help: Remove assignment to unused variable `e` + +ℹ Fix +152 152 | def f() -> None: +153 153 | try: +154 154 | print("hello") +155 |- except A as e : + 155 |+ except A: +156 156 | print("oh no!") +