[perflint] Extend PERF102 to comprehensions and generators#23473
[perflint] Extend PERF102 to comprehensions and generators#23473ntBre merged 7 commits intoastral-sh:mainfrom
perflint] Extend PERF102 to comprehensions and generators#23473Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PERF102 | 39 | 39 | 0 | 0 | 0 |
ntBre
left a comment
There was a problem hiding this comment.
Thanks! This looks reasonable to me overall, I mostly just had a couple of nits and then a larger suggestion about where to put this check.
We should also make this a preview change since it's a significant expansion to a stable rule.
crates/ruff_linter/src/rules/perflint/rules/incorrect_dict_iterator.rs
Outdated
Show resolved
Hide resolved
Summary -- While reviewing #23473, I noticed that the `while` loops in our deferred checks for `for` loops and lambdas would only be used if we pushed additional deferred scopes to the `Checker` while running lint rule code. This should actually be impossible given that the rules only receive a `&Checker` (as shown by the immutable re-borrow), and none of our tests fail when removing the loops. I guess it doesn't hurt to have the loops either since they only run once, but it did serve to confuse me today. I assume, but didn't fully confirm, that this used to be interleaved with the visiting phase, at which point additional deferred scopes could have still been added in the middle of this. Test Plan -- Existing tests
|
I really like the ecosystem hits here, didn't realize how common it was to use That said, when giving advice to use |
Let's not change too much about PERF102 in this PR 😄 I don't know the full history of recommending |
ntBre
left a comment
There was a problem hiding this comment.
Based on the ecosystem check, we still need to preview gate this change.
perflint] Extend PERF102 to comprehensions and generators
|
@ntBre Addressed all feedback with latest commits:
Would like to request you for your review whenever you get a chance. Thank you |
ntBre
left a comment
There was a problem hiding this comment.
Thank you! This makes sense to me.
I'll just apply my one suggestion adding a debug assertion to make it slightly more like the other deferred checks.
Thank you very much for the latest push. I really appreciate it. |
* main: [ty] Apply narrowing to walrus values (#23687) [`perflint`] Extend `PERF102` to comprehensions and generators (#23473) [ty] Fix GitHub-annotations mdtest output format (#23694) [ty] Reduce the number of potentially-flaky projects (#23698) [`pydocstyle`] Fix numpy section ordering (`D420`) (#23685) [ty] Move method-related types to a submodule (#23691) [ty] Avoid the mandatory "ecosystem-analyzer workflow run cancelled" notification every time you make a PR (#23695) [ty] Move `Type::subtyping_is_always_reflexive` to `types::relation` (#23692) Update conformance suite commit hash (#23693) [ty] Add mdtest suite for `typing.Concatenate` (#23554) [ty] filter out pre-loop bindings from loop headers (#23536)
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
Closes #6638