Skip to content

[flake8-return] RET501 can delete comments #18774

@MeGaGiGaGon

Description

@MeGaGiGaGon

Summary

RET501 can delete comments if they are inside parenthesis with the None, in which case the comments should be moved, the fix should be unsafe, or the fix should not apply. playground

PS ~\Desktop\New_folder>Get-Content issue.py
def foo(bar):
    if not bar:
        return
    return (
        None # comment
    )
PS ~\Desktop\New_folder>uvx ruff check issue.py --isolated --select RET
issue.py:4:5: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
  |
2 |       if not bar:
3 |           return
4 | /     return (
5 | |         None # comment
6 | |     )
  | |_____^ RET501
  |
  = help: Remove explicit `return None`

Found 1 error.
[*] 1 fixable with the `--fix` option.
PS ~\Desktop\New_folder>uvx ruff check issue.py --isolated --select RET --fix --diff
--- issue.py
+++ issue.py
@@ -1,6 +1,4 @@
 def foo(bar):
     if not bar:
         return
-    return (
-        None # comment
-    )
+    return

Would fix 1 error.

Version

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions