Skip to content

Commit

Permalink
Fix SIM105
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy committed Jan 4, 2023
1 parent 0df28bd commit 8db056c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
12 changes: 12 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM105.py
Expand Up @@ -16,6 +16,11 @@ def foo():
except: # SIM105
pass

try:
foo()
except (a.Error, b.Error): # SIM105
pass

try:
foo()
except ValueError:
Expand All @@ -29,3 +34,10 @@ def foo():
pass
else:
print('bar')

try:
foo()
except ValueError:
pass
finally:
print('bar')
9 changes: 7 additions & 2 deletions src/checkers/ast.rs
Expand Up @@ -1248,7 +1248,10 @@ where
}
}
StmtKind::Try {
handlers, orelse, ..
handlers,
orelse,
finalbody,
..
} => {
if self.settings.enabled.contains(&CheckCode::F707) {
if let Some(check) =
Expand All @@ -1275,7 +1278,9 @@ where
);
}
if self.settings.enabled.contains(&CheckCode::SIM105) {
flake8_simplify::plugins::use_contextlib_suppress(self, stmt, handlers, orelse);
flake8_simplify::plugins::use_contextlib_suppress(
self, stmt, handlers, orelse, finalbody,
);
}
}
StmtKind::Assign { targets, value, .. } => {
Expand Down
5 changes: 3 additions & 2 deletions src/flake8_simplify/plugins/use_contextlib_suppress.rs
Expand Up @@ -11,8 +11,9 @@ pub fn use_contextlib_suppress(
stmt: &Stmt,
handlers: &[Excepthandler],
orelse: &[Stmt],
finalbody: &[Stmt],
) {
if handlers.len() != 1 || !orelse.is_empty() {
if handlers.len() != 1 || !orelse.is_empty() || !finalbody.is_empty() {
return;
}
let handler = &handlers[0];
Expand All @@ -21,7 +22,7 @@ pub fn use_contextlib_suppress(
if matches!(body[0].node, StmtKind::Pass) {
let handler_names: Vec<_> = helpers::extract_handler_names(handlers)
.into_iter()
.flatten()
.map(|v| v.join("."))
.collect();
let exception = if handler_names.is_empty() {
"Exception".to_string()
Expand Down
Expand Up @@ -32,4 +32,14 @@ expression: checks
column: 8
fix: ~
parent: ~
- kind:
UseContextlibSuppress: "a.Error, b.Error"
location:
row: 19
column: 0
end_location:
row: 22
column: 8
fix: ~
parent: ~

0 comments on commit 8db056c

Please sign in to comment.