ruff/crates/ruff_linter/resources/test/fixtures/perflint
Lior Weissman 5c2d4d8d8f
[`perflint`] Handle tuples in dictionary comprehensions (`PERF403`) (#19934)
This pull request fixes the bug described in issue
[#19153](https://github.com/astral-sh/ruff/issues/19153).

The issue occurred when `PERF403` incorrectly flagged cases involving
tuple unpacking in a for loop. For example:

```python
def f():
    v = {}
    for (o, p), x in [("op", "x")]:
        v[x] = o, p
```

This code was wrongly suggested to be rewritten into a dictionary
comprehension, which changes the semantics.

Changes in this PR:

Updated the `PERF403` rule to correctly handle tuple unpacking in loop
targets.

Added regression tests to ensure this case (and similar ones) are no
longer flagged incorrectly.

Why:
This ensures that `PERF403` only triggers when a dictionary
comprehension is semantically equivalent to the original loop,
preventing false positives.

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2025-08-28 21:37:40 +00:00
..
PERF101.py [`perflint`] Fix `PERF101` autofix creating a syntax error and mark autofix as unsafe if there are comments in the `list` call expr (#18803) 2025-06-23 11:51:46 +00:00
PERF102.py Add padding in `PERF102` fixes (#7554) 2023-09-20 19:33:54 -04:00
PERF203.py Rename `ruff` crate to `ruff_linter` (#7529) 2023-09-20 08:38:27 +02:00
PERF401.py [`perflint`] Ignore rule if target is `global` or `nonlocal` (`PERF401`) (#19539) 2025-07-28 17:03:22 -04:00
PERF402.py Ignore list-copy recommendations for async `for` loops (#11250) 2024-05-02 11:48:52 -07:00
PERF403.py [`perflint`] Handle tuples in dictionary comprehensions (`PERF403`) (#19934) 2025-08-28 21:37:40 +00:00