Skip to content

[pylint] Preserve trailing comments in useless-return fix (PLR1711) - #29180

Merged
ntBre merged 1 commit into
astral-sh:mainfrom
yxshee:useless-return-comments
Oct 7, 2026
Merged

ntBre merged 1 commit into
astral-sh:mainfrom
yxshee:useless-return-comments

Conversation

@yxshee

@yxshee yxshee commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #28861

The useless-return rule (PLR1711) removes the final return / return None
in a function. When that line had a trailing comment, the fix deleted the whole
line and the comment was lost — and the fix was still marked safe.

This keeps a trailing comment on its own line instead of deleting it, the same
way PIE790 and PYI013 already do. If removing the statement would still drop
a comment (for example a comment inside a parenthesized multi-line return), the
fix is now marked unsafe rather than silently deleting it. I also added a "Fix
safety" section to the rule docs.

One thing I wasn't sure about: pragma-style trailing comments like # noqa or
# fmt: off can change meaning when moved onto their own line. I left them as
ordinary comments for now (matching PIE790/PYI013), but happy to special-case
them if you'd prefer.

Test Plan

Added fixture cases to useless_return.py: the example from the issue, a method
with return None # ..., a return; with a comment, a same-line
print(...); return # ..., and a multi-line parenthesized return (the unsafe
case). The existing # [useless-return] comments now survive in the snapshot.
cargo test -p ruff_linter, clippy, and the formatting hooks all pass.

@yxshee
yxshee marked this pull request as ready for review October 7, 2026 11:07
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre October 7, 2026 11:07
@ntBre ntBre added bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations labels Oct 7, 2026
@astral-sh-bot

astral-sh-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre left a comment

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.

Thank you!

@ntBre
ntBre merged commit 9e340ae into astral-sh:main Oct 7, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PLR1711 useless-return removes comments

2 participants