Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_return/RET501.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ def prop3(self) -> None:
def prop4(self) -> None:
print("I've run out of things to say")
return None


# https://github.com/astral-sh/ruff/issues/18774
class _:
def foo(bar):
if not bar:
return
return (
None # comment
)
19 changes: 15 additions & 4 deletions crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;

use ruff_diagnostics::Applicability;
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::helpers::{is_const_false, is_const_true};
use ruff_python_ast::stmt_if::elif_else_range;
Expand Down Expand Up @@ -51,6 +52,11 @@ use super::super::visitor::{ReturnVisitor, Stack};
/// return
/// return
/// ```
///
/// ## Fix Safety
/// This rule's fix is marked as unsafe for cases in which comments would be
/// dropped from the `return` statement.
///
#[derive(ViolationMetadata)]
pub(crate) struct UnnecessaryReturnNone;

Expand Down Expand Up @@ -382,10 +388,15 @@ fn unnecessary_return_none(checker: &Checker, decorator_list: &[Decorator], stac
}

let mut diagnostic = checker.report_diagnostic(UnnecessaryReturnNone, stmt.range());
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
"return".to_string(),
stmt.range(),
)));
let edit = Edit::range_replacement("return".to_string(), stmt.range());
diagnostic.set_fix(Fix::applicable_edit(
edit,
if checker.comment_ranges().intersects(stmt.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
},
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ RET501.py:14:9: RET501 [*] Do not explicitly `return None` in function if it is
15 15 |
16 16 | @property
17 17 | def prop(self) -> None:

RET501.py:59:9: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
|
57 | if not bar:
58 | return
59 | / return (
60 | | None # comment
61 | | )
| |_________^ RET501
|
= help: Remove explicit `return None`

ℹ Unsafe fix
56 56 | def foo(bar):
57 57 | if not bar:
58 58 | return
59 |- return (
60 |- None # comment
61 |- )
59 |+ return
Loading