-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ruff] Catch more dummy variable uses (RUF052)
#19799
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
[ruff] Catch more dummy variable uses (RUF052)
#19799
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF052 | 185 | 185 | 0 | 0 | 0 |
| DOC201 | 4 | 2 | 2 | 0 | 0 |
| DOC501 | 2 | 1 | 1 | 0 | 0 |
|
Hmm...It seems that a significant number of changes have been detected in the ecosystem run. |
ntBre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the code changes look reasonable to me! If you don't mind, can you reorganize the snapshots a bit as I suggested down below? That will make it a little easier for me to review those. Otherwise I just had some minor simplification suggestions.
As a note for myself, we don't have to do any special preview-gating here because the rule itself is still in preview.
Have you had a chance to go through the ecosystem changes? It's not necessarily a bad thing for there to be this many of them, if they're all correct. I'll take a look at them at some point too, but I wanted to see if you've had the first pass yet.
|
@ntBre it seems like there were only a few nit comments. Would it make sense for you to address them to get this PR landed? |
| match binding.kind { | ||
| BindingKind::Annotation | ||
| | BindingKind::Argument | ||
| | BindingKind::NamedExprAssignment | ||
| | BindingKind::Assignment | ||
| | BindingKind::LoopVar | ||
| | BindingKind::WithItemVar | ||
| | BindingKind::BoundException | ||
| | BindingKind::UnboundException(_) => {} | ||
|
|
||
| BindingKind::TypeParam | ||
| | BindingKind::Global(_) | ||
| | BindingKind::Nonlocal(_, _) | ||
| | BindingKind::Builtin | ||
| | BindingKind::ClassDefinition(_) | ||
| | BindingKind::FunctionDefinition(_) | ||
| | BindingKind::Export(_) | ||
| | BindingKind::FutureImport | ||
| | BindingKind::Import(_) | ||
| | BindingKind::FromImport(_) | ||
| | BindingKind::SubmoduleImport(_) | ||
| | BindingKind::Deletion | ||
| | BindingKind::ConditionalDeletion(_) | ||
| | BindingKind::DunderClassCell => return, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and expanded this a bit more to all of the binding kinds that seemed like "local" variables instead of only adding LoopVar. And we now match exhaustively so we know we've considered all of them.
| RUF052 Local dummy variable `other` is accessed | ||
| --> RUF052_0.py:177:13 | ||
| | | ||
| 175 | return | ||
| 176 | _seen.add(self) | ||
| 177 | for other in self.connected: | ||
| | ^^^^^ | ||
| 178 | other.recurse(_seen=_seen) | ||
| | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was pretty confused by this at first, but the dummy-variable-rgx setting for this test is the empty string:
ruff/crates/ruff_linter/src/rules/ruff/mod.rs
Line 626 in e3f9975
| #[test_case(Rule::UsedDummyVariable, Path::new("RUF052_0.py"), r"", 2)] |
so I think it's correct. We just weren't checking usage of dummy loop variables before.
ruff] Extend RUF052 to handle dummy variables in comprehensions an…ruff] Catch more dummy variable uses (RUF052)
|
I checked most of the ecosystem hits (I checked all of the error messages in the comment, but only clicked through to most), and they look correct to me. I think the DOC changes must be unrelated since we only modify the RUF052 code in this PR, maybe some quirk of the base branch or an old PR. |
…d-typevar * origin/main: (24 commits) [ty] Remove brittle constraint set reveal tests (#21568) [`ruff`] Catch more dummy variable uses (`RUF052`) (#19799) [ty] Use the same snapshot handling as other tests (#21564) [ty] suppress autocomplete suggestions during variable binding (#21549) Set severity for non-rule diagnostics (#21559) [ty] Add `with_type` convenience to display code (#21563) [ty] Implement docstring rendering to markdown (#21550) [ty] Reduce indentation of `TypeInferenceBuilder::infer_attribute_load` (#21560) Bump 0.14.6 (#21558) [ty] Improve debug messages when imports fail (#21555) [ty] Add support for relative import completions [ty] Refactor detection of import statements for completions [ty] Use dedicated collector for completions [ty] Attach subdiagnostics to `unresolved-import` errors for relative imports as well as absolute imports (#21554) [ty] support PEP 613 type aliases (#21394) [ty] More low-hanging fruit for inlay hint goto-definition (#21548) [ty] implement `TypedDict` structural assignment (#21467) [ty] Add more random TypeDetails and tests (#21546) [ty] Add goto for `Unknown` when it appears in an inlay hint (#21545) [ty] Add type definitions for `Type::SpecialForm`s (#21544) ...
Summary
Extends the
used-dummy-variablerule (RUF052) to detect dummy variables that are used within list comprehensions, dict comprehensions, set comprehensions, and generator expressions, not just regular for loops and function assignments.Problem
Previously, RUF052 only flagged dummy variables (variables with leading underscores) that were used in function scopes via assignments or regular for loops. It missed cases where dummy variables were used within comprehensions:
Solution
ScopeKind::Generator``GeneratorKindBindingKind::LoopVaris_allowed_scopeISSUE
Test Plan
cargo test