From 1e87e4cbf05ecdbdf5e7506bf4746b0e398a93d9 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 11 Aug 2023 00:02:02 -0400 Subject: [PATCH] Respect dummy-variable-rgx for unused bound exceptions (#6492) ## Summary This PR respects our unused variable regex when flagging bound exceptions, so that you no longer get a violation for, e.g.: ```python def f(): try: pass except Exception as _: pass ``` This is an odd pattern, but I think it's surprising that the regex _isn't_ respected here. Closes https://github.com/astral-sh/ruff/issues/6391 ## Test Plan `cargo test` --- .../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 94bc0b6ca507d3..bde71536966f44 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 8cce5812eba53e..91931f3ded7112 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 dd3ff4c7c72f19..4e723160be15da 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 ff53df87e19d13..5197e53959bcbd 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 +