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

Commit
58 days ago
[`pylint`] Preserve trailing comments in `useless-return` fix (`PLR1711`) (#29180) ## 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.
Author
Parents
Loading