Skip trivia when searching for named exception (#6218)

Closes https://github.com/astral-sh/ruff/issues/6213.
This commit is contained in:
Charlie Marsh 2023-07-31 23:42:30 -04:00 committed by GitHub
parent 38b5726948
commit ff9ebbaa5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View file

@ -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!")

View file

@ -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));

View file

@ -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!")