mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:54:48 +00:00
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`
This commit is contained in:
parent
563374503f
commit
cc151c35a8
4 changed files with 38 additions and 5 deletions
|
@ -145,3 +145,9 @@ def f() -> None:
|
||||||
obj = Foo()
|
obj = Foo()
|
||||||
obj.do_thing()
|
obj.do_thing()
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
except Exception as _:
|
||||||
|
pass
|
||||||
|
|
|
@ -18,7 +18,13 @@ pub(crate) fn bindings(checker: &mut Checker) {
|
||||||
|
|
||||||
for binding in checker.semantic.bindings.iter() {
|
for binding in checker.semantic.bindings.iter() {
|
||||||
if checker.enabled(Rule::UnusedVariable) {
|
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(
|
let mut diagnostic = Diagnostic::new(
|
||||||
pyflakes::rules::UnusedVariable {
|
pyflakes::rules::UnusedVariable {
|
||||||
name: binding.name(checker.locator).to_string(),
|
name: binding.name(checker.locator).to_string(),
|
||||||
|
|
|
@ -312,10 +312,13 @@ pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mu
|
||||||
&& !binding.is_global()
|
&& !binding.is_global()
|
||||||
&& !binding.is_used()
|
&& !binding.is_used()
|
||||||
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
&& !checker.settings.dummy_variable_rgx.is_match(name)
|
||||||
&& name != "__tracebackhide__"
|
&& !matches!(
|
||||||
&& name != "__traceback_info__"
|
name,
|
||||||
&& name != "__traceback_supplement__"
|
"__tracebackhide__"
|
||||||
&& name != "__debuggerskip__"
|
| "__traceback_info__"
|
||||||
|
| "__traceback_supplement__"
|
||||||
|
| "__debuggerskip__"
|
||||||
|
)
|
||||||
{
|
{
|
||||||
return Some((name.to_string(), binding.range, binding.source));
|
return Some((name.to_string(), binding.range, binding.source));
|
||||||
}
|
}
|
||||||
|
|
|
@ -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`
|
= 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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue