Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect dummy-variable-rgx for unused bound exceptions #6492

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/pyflakes/F841_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,9 @@ def do_thing(self):
obj = Foo()
obj.do_thing()


def f():
try:
pass
except Exception as _:
pass
8 changes: 7 additions & 1 deletion crates/ruff/src/checkers/ast/analyze/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
11 changes: 7 additions & 4 deletions crates/ruff/src/rules/pyflakes/rules/unused_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is expected -- it's a variant of the fixture where we replace the dummy variable regex with z.)


Loading