-
Notifications
You must be signed in to change notification settings - Fork 903
/
useless_return.rs
114 lines (101 loc) · 3.16 KB
/
useless_return.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use ruff_python_ast::{self as ast, Constant, Expr, Stmt};
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::{is_const_none, ReturnStatementVisitor};
use ruff_python_ast::statement_visitor::StatementVisitor;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::fix;
use crate::registry::AsRule;
/// ## What it does
/// Checks for functions that end with an unnecessary `return` or
/// `return None`, and contain no other `return` statements.
///
/// ## Why is this bad?
/// Python implicitly assumes a `None` return at the end of a function, making
/// it unnecessary to explicitly write `return None`.
///
/// ## Example
/// ```python
/// def f():
/// print(5)
/// return None
/// ```
///
/// Use instead:
/// ```python
/// def f():
/// print(5)
/// ```
#[violation]
pub struct UselessReturn;
impl AlwaysFixableViolation for UselessReturn {
#[derive_message_formats]
fn message(&self) -> String {
format!("Useless `return` statement at end of function")
}
fn fix_title(&self) -> String {
format!("Remove useless `return` statement")
}
}
/// PLR1711
pub(crate) fn useless_return(
checker: &mut Checker,
stmt: &Stmt,
body: &[Stmt],
returns: Option<&Expr>,
) {
// Skip functions that have a return annotation that is not `None`.
if !returns.map_or(true, is_const_none) {
return;
}
// Find the last statement in the function.
let Some(last_stmt) = body.last() else {
// Skip empty functions.
return;
};
if !last_stmt.is_return_stmt() {
return;
}
// Skip functions that consist of a single return statement.
if body.len() == 1 {
return;
}
// Skip functions that consist of a docstring and a return statement.
if body.len() == 2 {
if let Stmt::Expr(ast::StmtExpr { value, range: _ }) = &body[0] {
if matches!(
value.as_ref(),
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
})
) {
return;
}
}
}
// Verify that the last statement is a return statement.
let Stmt::Return(ast::StmtReturn { value, range: _ }) = &last_stmt else {
return;
};
// Verify that the return statement is either bare or returns `None`.
if !value.as_ref().map_or(true, |expr| is_const_none(expr)) {
return;
};
// Finally: verify that there are no _other_ return statements in the function.
let mut visitor = ReturnStatementVisitor::default();
visitor.visit_body(body);
if visitor.returns.len() > 1 {
return;
}
let mut diagnostic = Diagnostic::new(UselessReturn, last_stmt.range());
if checker.patch(diagnostic.kind.rule()) {
let edit =
fix::edits::delete_stmt(last_stmt, Some(stmt), checker.locator(), checker.indexer());
diagnostic.set_fix(Fix::automatic(edit).isolate(Checker::isolation(
checker.semantic().current_statement_id(),
)));
}
checker.diagnostics.push(diagnostic);
}