ruff
e2a1d1a8 - [`ruff`] Catch more dummy variable uses (`RUF052`) (#19799)

Commit
20 days ago
[`ruff`] Catch more dummy variable uses (`RUF052`) (#19799) ## Summary Extends the `used-dummy-variable` rule ([RUF052](https://docs.astral.sh/ruff/rules/used-dummy-variable/)) 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: ```python def example(): my_list = [{"foo": 1}, {"foo": 2}] # These were not detected before: [_item["foo"] for _item in my_list] # Should warn: _item is used {_item["key"]: _item["val"] for _item in my_list} # Should warn: _item is used (_item["foo"] for _item in my_list) # Should warn: _item is used ``` ### Solution - Extended scope checking to include all generator scopes () with any (list/dict/set comprehensions and generator expressions) `ScopeKind::Generator``GeneratorKind` - Added support for bindings, which cover loop variables in both regular for loops and comprehensions `BindingKind::LoopVar` - Refactored the scope validation logic for better readability with a descriptive variable `is_allowed_scope` [ISSUE](https://github.com/astral-sh/ruff/issues/19732) ## Test Plan ```bash cargo test ``` --------- Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
Author
Parents
Loading