Skip to content

Commit

Permalink
Foo
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 16, 2023
1 parent bebfa70 commit f3bb83c
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 16 deletions.
8 changes: 8 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_return/RET503.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ def noreturn_pytest_xfail_2():
if x > 0:
return 1
py_xfail("oof")


def nested(values):
if not values:
return False

for value in values:
print(value)
53 changes: 37 additions & 16 deletions crates/ruff/src/rules/flake8_return/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,52 @@ fn is_noreturn_func(checker: &Checker, func: &Expr) -> bool {
fn implicit_return(checker: &mut Checker, stmt: &Stmt, top_level: bool) {
match &stmt.node {
StmtKind::If { body, orelse, .. } => {
if body.is_empty() || orelse.is_empty() {
checker.diagnostics.push(Diagnostic::new(
ImplicitReturn { fixable: false },
Range::from_located(stmt),
));
return;
}

if let Some(last_stmt) = body.last() {
implicit_return(checker, last_stmt, false);
}
if let Some(last_stmt) = orelse.last() {
implicit_return(checker, last_stmt, false);
implicit_return(checker, last_stmt, true);
} else {
let fixable = top_level;
let mut diagnostic =
Diagnostic::new(ImplicitReturn { fixable }, Range::from_located(stmt));
if fixable && checker.patch(diagnostic.kind.rule()) {
if let Some(indent) = indentation(checker.locator, stmt) {
let mut content = String::new();
content.push_str(indent);
content.push_str("return None");
content.push_str(checker.stylist.line_ending().as_str());
diagnostic.amend(Fix::insertion(
content,
Location::new(stmt.end_location.unwrap().row() + 1, 0),
));
}
}
checker.diagnostics.push(diagnostic);
}
}
StmtKind::For { body, orelse, .. } | StmtKind::AsyncFor { body, orelse, .. } => {
if let Some(last_stmt) = orelse.last() {
implicit_return(checker, last_stmt, false);
} else if let Some(last_stmt) = body.last() {
implicit_return(checker, last_stmt, false);
implicit_return(checker, last_stmt, true);
} else {
let fixable = top_level;
let mut diagnostic =
Diagnostic::new(ImplicitReturn { fixable }, Range::from_located(stmt));
if fixable && checker.patch(diagnostic.kind.rule()) {
if let Some(indent) = indentation(checker.locator, stmt) {
let mut content = String::new();
content.push_str(indent);
content.push_str("return None");
content.push_str(checker.stylist.line_ending().as_str());
diagnostic.amend(Fix::insertion(
content,
Location::new(stmt.end_location.unwrap().row() + 1, 0),
));
}
}
checker.diagnostics.push(diagnostic);
}
}
StmtKind::With { body, .. } | StmtKind::AsyncWith { body, .. } => {
if let Some(last_stmt) = body.last() {
implicit_return(checker, last_stmt, false);
implicit_return(checker, last_stmt, true);
}
}
StmtKind::Assert { test, .. }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
---
source: crates/ruff/src/rules/flake8_return/mod.rs
assertion_line: 33
expression: diagnostics
---
- kind:
ImplicitReturn:
fixable: true
location:
row: 18
column: 4
end_location:
row: 19
column: 16
fix:
content:
- " return None"
- ""
location:
row: 20
column: 0
end_location:
row: 20
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 34
column: 4
end_location:
row: 34
column: 11
fix:
content:
- " return None"
- ""
location:
row: 35
column: 0
end_location:
row: 35
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 39
column: 4
end_location:
row: 41
column: 20
fix:
content:
- " return None"
- ""
location:
row: 42
column: 0
end_location:
row: 42
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: false
location:
row: 50
column: 8
end_location:
row: 50
column: 15
fix: ~
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 57
column: 4
end_location:
row: 57
column: 22
fix:
content:
- " return None"
- ""
location:
row: 58
column: 0
end_location:
row: 58
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 64
column: 4
end_location:
row: 64
column: 21
fix:
content:
- " return None"
- ""
location:
row: 65
column: 0
end_location:
row: 65
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 111
column: 4
end_location:
row: 114
column: 16
fix:
content:
- " return None"
- ""
location:
row: 115
column: 0
end_location:
row: 115
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 118
column: 4
end_location:
row: 124
column: 19
fix:
content:
- " return None"
- ""
location:
row: 125
column: 0
end_location:
row: 125
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 128
column: 4
end_location:
row: 131
column: 16
fix:
content:
- " return None"
- ""
location:
row: 132
column: 0
end_location:
row: 132
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 135
column: 4
end_location:
row: 141
column: 19
fix:
content:
- " return None"
- ""
location:
row: 142
column: 0
end_location:
row: 142
column: 0
parent: ~
- kind:
ImplicitReturn:
fixable: true
location:
row: 260
column: 4
end_location:
row: 261
column: 20
fix:
content:
- " return None"
- ""
location:
row: 262
column: 0
end_location:
row: 262
column: 0
parent: ~

0 comments on commit f3bb83c

Please sign in to comment.