-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
290 additions
and
3 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
crates/ruff/resources/test/fixtures/pylint/useless_return.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sys | ||
|
||
|
||
def print_python_version(): | ||
print(sys.version) | ||
return None # [useless-return] | ||
|
||
|
||
def print_python_version(): | ||
print(sys.version) | ||
return None # [useless-return] | ||
|
||
|
||
def print_python_version(): | ||
print(sys.version) | ||
return None # [useless-return] | ||
|
||
|
||
class SomeClass: | ||
def print_python_version(self): | ||
print(sys.version) | ||
return None # [useless-return] | ||
|
||
|
||
def print_python_version(): | ||
if 2 * 2 == 4: | ||
return | ||
print(sys.version) | ||
|
||
|
||
def print_python_version(): | ||
if 2 * 2 == 4: | ||
return None | ||
return | ||
|
||
|
||
def print_python_version(): | ||
if 2 * 2 == 4: | ||
return None | ||
|
||
|
||
def print_python_version(): | ||
"""This function returns None.""" | ||
return None | ||
|
||
|
||
def print_python_version(): | ||
"""This function returns None.""" | ||
print(sys.version) | ||
return None # [useless-return] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use log::error; | ||
use rustpython_parser::ast::{Constant, ExprKind, Stmt, StmtKind}; | ||
|
||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::{is_const_none, ReturnStatementVisitor}; | ||
use ruff_python_ast::types::{Range, RefEquality}; | ||
use ruff_python_ast::visitor::Visitor; | ||
|
||
use crate::autofix::helpers::delete_stmt; | ||
use crate::checkers::ast::Checker; | ||
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 AlwaysAutofixableViolation for UselessReturn { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Useless `return` statement at end of function") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
format!("Remove useless `return` statement") | ||
} | ||
} | ||
|
||
/// PLR1711 | ||
pub fn useless_return<'a>(checker: &mut Checker<'a>, stmt: &'a Stmt, body: &'a [Stmt]) { | ||
// Skip empty functions. | ||
if body.is_empty() { | ||
return; | ||
} | ||
|
||
// Find the last statement in the function. | ||
let last_stmt = body.last().unwrap(); | ||
if !matches!(last_stmt.node, StmtKind::Return { .. }) { | ||
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 StmtKind::Expr { value } = &body[0].node { | ||
if matches!( | ||
value.node, | ||
ExprKind::Constant { | ||
value: Constant::Str(_), | ||
.. | ||
} | ||
) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// Verify that the last statement is a return statement. | ||
let StmtKind::Return { value} = &last_stmt.node 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, Range::from(last_stmt)); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
let deleted: Vec<&Stmt> = checker.deletions.iter().map(Into::into).collect(); | ||
match delete_stmt( | ||
last_stmt, | ||
Some(stmt), | ||
&deleted, | ||
checker.locator, | ||
checker.indexer, | ||
checker.stylist, | ||
) { | ||
Ok(fix) => { | ||
if fix.content.is_empty() || fix.content == "pass" { | ||
checker.deletions.insert(RefEquality(last_stmt)); | ||
} | ||
diagnostic.amend(fix); | ||
} | ||
Err(e) => { | ||
error!("Failed to delete `return` statement: {}", e); | ||
} | ||
}; | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} |
105 changes: 105 additions & 0 deletions
105
...uff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLR1711_useless_return.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: UselessReturn | ||
body: "Useless `return` statement at end of function" | ||
suggestion: "Remove useless `return` statement" | ||
fixable: true | ||
location: | ||
row: 6 | ||
column: 4 | ||
end_location: | ||
row: 6 | ||
column: 15 | ||
fix: | ||
content: "" | ||
location: | ||
row: 6 | ||
column: 0 | ||
end_location: | ||
row: 7 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: UselessReturn | ||
body: "Useless `return` statement at end of function" | ||
suggestion: "Remove useless `return` statement" | ||
fixable: true | ||
location: | ||
row: 11 | ||
column: 4 | ||
end_location: | ||
row: 11 | ||
column: 15 | ||
fix: | ||
content: "" | ||
location: | ||
row: 11 | ||
column: 0 | ||
end_location: | ||
row: 12 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: UselessReturn | ||
body: "Useless `return` statement at end of function" | ||
suggestion: "Remove useless `return` statement" | ||
fixable: true | ||
location: | ||
row: 16 | ||
column: 4 | ||
end_location: | ||
row: 16 | ||
column: 15 | ||
fix: | ||
content: "" | ||
location: | ||
row: 16 | ||
column: 0 | ||
end_location: | ||
row: 17 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: UselessReturn | ||
body: "Useless `return` statement at end of function" | ||
suggestion: "Remove useless `return` statement" | ||
fixable: true | ||
location: | ||
row: 22 | ||
column: 8 | ||
end_location: | ||
row: 22 | ||
column: 19 | ||
fix: | ||
content: "" | ||
location: | ||
row: 22 | ||
column: 0 | ||
end_location: | ||
row: 23 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: UselessReturn | ||
body: "Useless `return` statement at end of function" | ||
suggestion: "Remove useless `return` statement" | ||
fixable: true | ||
location: | ||
row: 50 | ||
column: 4 | ||
end_location: | ||
row: 50 | ||
column: 15 | ||
fix: | ||
content: "" | ||
location: | ||
row: 50 | ||
column: 0 | ||
end_location: | ||
row: 51 | ||
column: 0 | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.