You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[perflint] Extend PERF102 to comprehensions and generators (#23473)
## Summary
Extends PERF102 to catch .items() misuse in comprehensions and
generators, not just for loops.
Extracted the core detection into a shared helper
(check_dict_items_usage) so both the for loop and comprehension paths
can reuse it. The comprehension check runs between Step 2 and Step 3 in
visit_expr since is_unused() needs the generator scope to still be
active.
now flagged
_ = [k for k, _ in d.items()] # use .keys()
_ = {v for _, v in d.items()} # use .values()
_ = (v for _, v in d.items()) # use .values()
still fine
_ = [(k, v) for k, v in d.items()] # both used
_ = [k for k, v in d.items() if v] # v used in condition
## Test Plan
- Added error and no-error cases for list/set/dict comps, generators,
and nested generators
- Tests, clippy, and prek all pass
Closes#6638
---------
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Copy file name to clipboardExpand all lines: crates/ruff_linter/src/rules/perflint/snapshots/ruff_linter__rules__perflint__tests__PERF102_PERF102.py.snap
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -226,4 +226,6 @@ help: Replace `.items()` with `.keys()`
0 commit comments