[flake8-return] Only add return None at end of function (RET503)#11074
[flake8-return] Only add return None at end of function (RET503)#11074MichaReiser merged 7 commits intoastral-sh:mainfrom JonathanPlasse:ret503-only-add-return-at-end-of-function
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RET503 | 256 | 122 | 134 | 0 | 0 |
| RUF100 | 2 | 2 | 0 | 0 | 0 |
| if checker.settings.preview.is_enabled() { | ||
| return result; | ||
| } |
There was a problem hiding this comment.
Early return if in preview mode because we only need to know if the function has at least one implicit return to add a single return None at the end of the function.
The stable version can add multiple return so it cannot return early.
The code should be simplified once the preview version become stable.
|
A comment should be added that when the preview version becomes stable the code can be simplified with early return everywhere. |
|
Will it be included in Ruff 0.5.0? |
Just seen this -- unfortunately I just set the release workflow in motion, so unfortunately not. Sorry :-( |
|
No worries. |
|
Will it be included in Ruff 0.6.0? |
CodSpeed Performance ReportMerging #11074 will not alter performanceComparing Summary
|
|
I don't think this requires a minor release, considering that the change is gated behind preview mode. |
|
|
||
| /// RET503 | ||
| fn implicit_return(checker: &mut Checker, function_def: &ast::StmtFunctionDef, stmt: &Stmt) { | ||
| if has_implicit_return(checker, stmt) && checker.settings.preview.is_enabled() { |
There was a problem hiding this comment.
I don't have a great solution but I do find it a bit surprising that has_implicit_return adds diagnostics in the non preview mode.
I'm somewhat inclined to duplicate the logic between preview and non-preview mode. That should also make it much easier when stabilizing the new behavior (and it's not that much code that needs copying). Unless you see a way of how we can avoid the side-effects of has_implicit_return in stable (e.g. could it return a Vec with all the statements for which diagnostics should be added?)
There was a problem hiding this comment.
I don't mind going ahead with the PR as is but I'm interested to hear what you think of duplicating the logic OR maybe you have another idea to make this a bit clearer
There was a problem hiding this comment.
I used the Vec solution to remove the side effects.
The code is nicer.
Thank you for suggesting it.
There was a problem hiding this comment.
Thanks. Will merge now and sorry for the long wait
There was a problem hiding this comment.
You are welcome
Summary
In
RET503documentation, it is said that it only addsreturn Noneat the end of functions.The current behavior can add
return Nonein multiple place in the function, but not at the end of the function.This behavior does not match the documentation and goes against other
flake8-returnrules likeRET505and caused people to open #5765.The new behavior only adds
return Noneat the end of functions like in the documentation and results in more readable code.The new behavior is only available in preview mode.
The range of
RET503is changed to be the entire function, as it will be more clear it concerns the entire function.Before
After
Test Plan
I added a preview snapshot for
RET503.I compared the diff between the
RET503stable and preview snapshot.I checked the ecosystem reports and fixed one regression.