Skip to content

[Perflint] PERF401/PERF403 fix can duplicate comments #18787

@MeGaGiGaGon

Description

@MeGaGiGaGon

Summary

With preview enabled, the fix for PERF401 can duplicate comments if they are inside an empty tuple inside the if test. playground

PS D:\python_projects> Get-Content issue.py
original = list(range(10000))
filtered = []
for i in original:
    if (
        # comment
    ):
        filtered.append(i)
PS D:\python_projects> uvx ruff check issue.py --select PERF --preview
issue.py:7:9: PERF401 Use a list comprehension to create a transformed list
  |
5 |         # comment
6 |     ):
7 |         filtered.append(i)
  |         ^^^^^^^^^^^^^^^^^^ PERF401
  |
  = help: Replace for loop with list comprehension

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
PS D:\python_projects> uvx ruff check issue.py --select PERF --preview --unsafe-fixes --fix --diff
--- issue.py
+++ issue.py
@@ -1,7 +1,5 @@
 original = list(range(10000))
-filtered = []
-for i in original:
-    if (
+# comment
+filtered = [i for i in original if (
         # comment
-    ):
-        filtered.append(i)
+    )]

Would fix 1 error.

This also applies to PERF403 playground

Version

ruff 0.12.0 (87f0feb 2025-06-17) + playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions