Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SIM105 #1633

Merged
merged 1 commit into from Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: ~