From e7b0a76f6df7460bab01447b7f02a79f0bd282e8 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 10 Aug 2023 23:51:54 -0400 Subject: [PATCH] Respect dummy-variable-rgx for unused bound exceptions --- .../resources/test/fixtures/pyflakes/F841_0.py | 6 ++++++ .../ruff/src/checkers/ast/analyze/bindings.rs | 8 +++++++- .../rules/pyflakes/rules/unused_variable.rs | 11 +++++++---- ...flakes__tests__f841_dummy_variable_rgx.snap | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pyflakes/F841_0.py b/crates/ruff/resources/test/fixtures/pyflakes/F841_0.py index 94bc0b6ca507d..bde71536966f4 100644 --- a/crates/ruff/resources/test/fixtures/pyflakes/F841_0.py +++ b/crates/ruff/resources/test/fixtures/pyflakes/F841_0.py @@ -145,3 +145,9 @@ def do_thing(self): obj = Foo() obj.do_thing() + +def f(): + try: + pass + except Exception as _: + pass diff --git a/crates/ruff/src/checkers/ast/analyze/bindings.rs b/crates/ruff/src/checkers/ast/analyze/bindings.rs index 8cce5812eba53..91931f3ded711 100644 --- a/crates/ruff/src/checkers/ast/analyze/bindings.rs +++ b/crates/ruff/src/checkers/ast/analyze/bindings.rs @@ -18,7 +18,13 @@ pub(crate) fn bindings(checker: &mut Checker) { for binding in checker.semantic.bindings.iter() { if checker.enabled(Rule::UnusedVariable) { - if binding.kind.is_bound_exception() && !binding.is_used() { + if binding.kind.is_bound_exception() + && !binding.is_used() + && !checker + .settings + .dummy_variable_rgx + .is_match(binding.name(checker.locator)) + { let mut diagnostic = Diagnostic::new( pyflakes::rules::UnusedVariable { name: binding.name(checker.locator).to_string(), diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs index dd3ff4c7c72f1..4e723160be15d 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs @@ -312,10 +312,13 @@ pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mu && !binding.is_global() && !binding.is_used() && !checker.settings.dummy_variable_rgx.is_match(name) - && name != "__tracebackhide__" - && name != "__traceback_info__" - && name != "__traceback_supplement__" - && name != "__debuggerskip__" + && !matches!( + name, + "__tracebackhide__" + | "__traceback_info__" + | "__traceback_supplement__" + | "__debuggerskip__" + ) { return Some((name.to_string(), binding.range, binding.source)); } diff --git a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__f841_dummy_variable_rgx.snap b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__f841_dummy_variable_rgx.snap index ff53df87e19d1..5197e53959bcb 100644 --- a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__f841_dummy_variable_rgx.snap +++ b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__f841_dummy_variable_rgx.snap @@ -256,4 +256,22 @@ F841_0.py:127:21: F841 Local variable `value` is assigned to but never used | = help: Remove assignment to unused variable `value` +F841_0.py:152:25: F841 [*] Local variable `_` is assigned to but never used + | +150 | try: +151 | pass +152 | except Exception as _: + | ^ F841 +153 | pass + | + = help: Remove assignment to unused variable `_` + +ℹ Fix +149 149 | def f(): +150 150 | try: +151 151 | pass +152 |- except Exception as _: + 152 |+ except Exception: +153 153 | pass +