Repository navigation
[pylint] Preserve trailing comments in useless-return fix (PLR1711) - #29180
Merged
Merged
Conversation
yxshee
marked this pull request as ready for review
October 7, 2026 11:07
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #28861
The
useless-returnrule (PLR1711) removes the finalreturn/return Nonein 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
PIE790andPYI013already do. If removing the statement would still dropa 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
# noqaor# fmt: offcan change meaning when moved onto their own line. I left them asordinary 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 methodwith
return None # ..., areturn;with a comment, a same-lineprint(...); return # ..., and a multi-line parenthesized return (the unsafecase). The existing
# [useless-return]comments now survive in the snapshot.cargo test -p ruff_linter, clippy, and the formatting hooks all pass.