Make some `flake8-return` rules auto-fixable (#1017)

This commit is contained in:
Charlie Marsh 2022-12-03 20:13:17 -05:00 committed by GitHub
parent 291727a27d
commit 87bdda3cfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 21 deletions

View File

@ -728,9 +728,9 @@ For more, see [flake8-return](https://pypi.org/project/flake8-return/1.2.0/) on
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| RET501 | UnnecessaryReturnNone | Do not explicitly `return None` in function if it is the only possible return value | |
| RET502 | ImplicitReturnValue | Do not implicitly `return None` in function able to return non-`None` value | |
| RET503 | ImplicitReturn | Missing explicit `return` at the end of function able to return non-`None` value | |
| RET501 | UnnecessaryReturnNone | Do not explicitly `return None` in function if it is the only possible return value | 🛠 |
| RET502 | ImplicitReturnValue | Do not implicitly `return None` in function able to return non-`None` value | 🛠 |
| RET503 | ImplicitReturn | Missing explicit `return` at the end of function able to return non-`None` value | 🛠 |
| RET504 | UnnecessaryAssign | Unnecessary variable assignment before `return` statement | |
| RET505 | SuperfluousElseReturn | Unnecessary `else` after `return` statement | |
| RET506 | SuperfluousElseRaise | Unnecessary `else` after `raise` statement | |

View File

@ -2469,6 +2469,8 @@ impl CheckKind {
| CheckKind::DoNotAssignLambda
| CheckKind::DuplicateHandlerException(..)
| CheckKind::GetAttrWithConstant
| CheckKind::ImplicitReturn
| CheckKind::ImplicitReturnValue
| CheckKind::IsLiteral
| CheckKind::NewLineAfterLastParagraph
| CheckKind::NewLineAfterSectionName(..)
@ -2488,6 +2490,7 @@ impl CheckKind {
| CheckKind::PPrintFound
| CheckKind::PrintFound
| CheckKind::RaiseNotImplemented
| CheckKind::RedundantOpenModes
| CheckKind::RedundantTupleInExceptionHandler(..)
| CheckKind::SectionNameEndsInColon(..)
| CheckKind::SectionNotOverIndented(..)
@ -2502,7 +2505,6 @@ impl CheckKind {
| CheckKind::UnnecessaryComprehension(..)
| CheckKind::UnnecessaryEncodeUTF8
| CheckKind::UnnecessaryFutureImport(..)
| CheckKind::RedundantOpenModes
| CheckKind::UnnecessaryGeneratorDict
| CheckKind::UnnecessaryGeneratorList
| CheckKind::UnnecessaryGeneratorSet
@ -2514,6 +2516,7 @@ impl CheckKind {
| CheckKind::UnnecessaryLiteralSet(..)
| CheckKind::UnnecessaryLiteralWithinListCall(..)
| CheckKind::UnnecessaryLiteralWithinTupleCall(..)
| CheckKind::UnnecessaryReturnNone
| CheckKind::UnsortedImports
| CheckKind::UnusedImport(_, false)
| CheckKind::UnusedLoopControlVariable(..)

View File

@ -3,6 +3,8 @@ use rustpython_ast::{Constant, Expr, ExprKind, Location, Stmt, StmtKind};
use crate::ast::types::Range;
use crate::ast::visitor::Visitor;
use crate::ast::whitespace::indentation;
use crate::autofix::Fix;
use crate::check_ast::Checker;
use crate::checks::{Branch, CheckCode, CheckKind};
use crate::flake8_return::helpers::result_exists;
@ -20,10 +22,16 @@ fn unnecessary_return_none(checker: &mut Checker, stack: &Stack) {
..
}
) {
checker.add_check(Check::new(
CheckKind::UnnecessaryReturnNone,
Range::from_located(stmt),
));
let mut check =
Check::new(CheckKind::UnnecessaryReturnNone, Range::from_located(stmt));
if checker.patch(&CheckCode::RET501) {
check.amend(Fix::replacement(
"return".to_string(),
stmt.location,
stmt.end_location.unwrap(),
));
}
checker.add_check(check);
}
}
}
@ -33,10 +41,15 @@ fn unnecessary_return_none(checker: &mut Checker, stack: &Stack) {
fn implicit_return_value(checker: &mut Checker, stack: &Stack) {
for (stmt, expr) in &stack.returns {
if expr.is_none() {
checker.add_check(Check::new(
CheckKind::ImplicitReturnValue,
Range::from_located(stmt),
));
let mut check = Check::new(CheckKind::ImplicitReturnValue, Range::from_located(stmt));
if checker.patch(&CheckCode::RET502) {
check.amend(Fix::replacement(
"return None".to_string(),
stmt.location,
stmt.end_location.unwrap(),
));
}
checker.add_check(check);
}
}
}
@ -85,10 +98,18 @@ fn implicit_return(checker: &mut Checker, last_stmt: &Stmt) {
| StmtKind::Raise { .. }
| StmtKind::Try { .. } => {}
_ => {
checker.add_check(Check::new(
CheckKind::ImplicitReturn,
Range::from_located(last_stmt),
));
let mut check = Check::new(CheckKind::ImplicitReturn, Range::from_located(last_stmt));
if checker.patch(&CheckCode::RET503) {
let mut content = String::new();
content.push_str(&indentation(checker, last_stmt));
content.push_str("return None");
content.push('\n');
check.amend(Fix::insertion(
content,
Location::new(last_stmt.end_location.unwrap().row() + 1, 0),
));
}
checker.add_check(check);
}
}
}

View File

@ -9,5 +9,12 @@ expression: checks
end_location:
row: 4
column: 15
fix: ~
fix:
content: return
location:
row: 4
column: 4
end_location:
row: 4
column: 15

View File

@ -9,5 +9,12 @@ expression: checks
end_location:
row: 3
column: 14
fix: ~
fix:
content: return None
location:
row: 3
column: 8
end_location:
row: 3
column: 14

View File

@ -17,7 +17,14 @@ expression: checks
end_location:
row: 14
column: 15
fix: ~
fix:
content: " return None\n"
location:
row: 15
column: 0
end_location:
row: 15
column: 0
- kind: ImplicitReturn
location:
row: 23
@ -25,7 +32,14 @@ expression: checks
end_location:
row: 23
column: 11
fix: ~
fix:
content: " return None\n"
location:
row: 24
column: 0
end_location:
row: 24
column: 0
- kind: ImplicitReturn
location:
row: 29
@ -41,5 +55,12 @@ expression: checks
end_location:
row: 39
column: 15
fix: ~
fix:
content: " return None\n"
location:
row: 40
column: 0
end_location:
row: 40
column: 0