Avoid SIM110/SIM110 errors with else statements (#1832)

Closes #1831.
This commit is contained in:
Charlie Marsh
2023-01-12 16:17:27 -05:00
committed by GitHub
parent 590bec57f4
commit e0fdc4c5e8
5 changed files with 108 additions and 22 deletions

View File

@@ -25,6 +25,7 @@ fn return_values<'a>(stmt: &'a Stmt, sibling: &'a Stmt) -> Option<Loop<'a>> {
body,
target,
iter,
orelse,
..
} = &stmt.node else {
return None;
@@ -35,16 +36,23 @@ fn return_values<'a>(stmt: &'a Stmt, sibling: &'a Stmt) -> Option<Loop<'a>> {
if body.len() != 1 {
return None;
}
// TODO(charlie): If we have `else: return True` or `else: return False`, we
// should still be able to simplify.
if !orelse.is_empty() {
return None;
}
let StmtKind::If {
body: nested_body,
test: nested_test,
..
test: nested_test, orelse: nested_orelse,
} = &body[0].node else {
return None;
};
if nested_body.len() != 1 {
return None;
}
if !nested_orelse.is_empty() {
return None;
}
let StmtKind::Return { value } = &nested_body[0].node else {
return None;
};

View File

@@ -1,22 +1,22 @@
---
source: src/flake8_simplify/mod.rs
expression: checks
expression: diagnostics
---
- kind:
ConvertLoopToAny: return any(check(x) for x in iterable)
location:
row: 2
row: 3
column: 4
end_location:
row: 4
row: 5
column: 23
fix:
content: return any(check(x) for x in iterable)
location:
row: 2
row: 3
column: 4
end_location:
row: 5
row: 6
column: 16
parent: ~

View File

@@ -1,39 +1,39 @@
---
source: src/flake8_simplify/mod.rs
expression: checks
expression: diagnostics
---
- kind:
ConvertLoopToAll: return all(not check(x) for x in iterable)
location:
row: 16
row: 25
column: 4
end_location:
row: 18
row: 27
column: 24
fix:
content: return all(not check(x) for x in iterable)
location:
row: 16
row: 25
column: 4
end_location:
row: 19
row: 28
column: 15
parent: ~
- kind:
ConvertLoopToAll: return all(x.is_empty() for x in iterable)
location:
row: 23
row: 33
column: 4
end_location:
row: 25
row: 35
column: 24
fix:
content: return all(x.is_empty() for x in iterable)
location:
row: 23
row: 33
column: 4
end_location:
row: 26
row: 36
column: 15
parent: ~