Skip to content

[perflint] Fix comment duplication in fixes (PERF401, PERF403)#23729

Merged
ntBre merged 1 commit intoastral-sh:mainfrom
anishgirianish:fix/perf401-perf403-comment-duplication
Mar 6, 2026
Merged

[perflint] Fix comment duplication in fixes (PERF401, PERF403)#23729
ntBre merged 1 commit intoastral-sh:mainfrom
anishgirianish:fix/perf401-perf403-comment-duplication

Conversation

@anishgirianish
Copy link
Copy Markdown
Contributor

@anishgirianish anishgirianish commented Mar 5, 2026

Summary

  • Fix comments inside if test and for target being duplicated when transforming a for-loop into a comprehension

Test Plan

  • Added test cases for both PERF401 and PERF403
  • Verified comments appear exactly once in preview snapshot fixes

Closes: #18787

@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 5, 2026 03:56
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 5, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
Copy link
Copy Markdown
Contributor

ntBre commented Mar 5, 2026

Thanks! I'll try to give this a look tomorrow. Is the related issue #18787?

@anishgirianish
Copy link
Copy Markdown
Contributor Author

anishgirianish commented Mar 6, 2026

Thanks! I'll try to give this a look tomorrow. Is the related issue #18787?

@ntBre Apologies I forgot to tag the issue. Yes it is. thank you so much.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Mar 6, 2026
Copy link
Copy Markdown
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I wrote a couple of comments, but I don't think I feel strongly enough about them to change anything, so I'll go ahead and land this as-is.

Comment on lines +320 to +328
def f():
# comment duplication in target (https://github.com/astral-sh/ruff/issues/18787)
original = list(range(10000))
filtered = []
for (
i # comment
) in original:
if i > 0:
filtered.append(i) No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you have to change the code to handle this case as well as the one above? I ask because these look very similar to me, and I'm wondering if we can drop one of the tests, but if they exercise different parts of the code, it's fine to keep both.

for_stmt.range,
&[to_append.range(), for_stmt.iter.range()],
);
let mut ranges_to_ignore = vec![
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit sad to allocate a Vec here, but I think it's okay. It would be a slightly larger refactor to make comment_strings_in_range take an iterator and too much duplication to do something like:

if let Some(test) = if_test {
    &[to_append.range(), for_stmt.iter.range(), for_stmt.target.range(), test.range()]
} else {
    &[to_append.range(), for_stmt.iter.range(), for_stmt.target.range()]
}

Although now that I write that out, I don't mind it as much as I expected.

@ntBre ntBre changed the title [perflint] Fix PERF401/PERF403 comment duplication in comprehension fixes [perflint] Fix comment duplication in comprehension fixes (PERF401, PERF403) Mar 6, 2026
@ntBre ntBre changed the title [perflint] Fix comment duplication in comprehension fixes (PERF401, PERF403) [perflint] Fix comment duplication in fixes (PERF401, PERF403) Mar 6, 2026
@ntBre ntBre merged commit 46507b8 into astral-sh:main Mar 6, 2026
45 checks passed
@anishgirianish
Copy link
Copy Markdown
Contributor Author

Thank you! I wrote a couple of comments, but I don't think I feel strongly enough about them to change anything, so I'll go ahead and land this as-is.

@ntBre thank you for the review and the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perflint] PERF401/PERF403 fix can duplicate comments

2 participants