From 71ece3bb5b894a32e2c7b3c1b64608b15db64133 Mon Sep 17 00:00:00 2001 From: harupy Date: Thu, 5 Jan 2023 00:49:46 +0900 Subject: [PATCH] Fix SIM105 --- resources/test/fixtures/flake8_simplify/SIM105.py | 12 ++++++++++++ src/checkers/ast.rs | 9 +++++++-- .../plugins/use_contextlib_suppress.rs | 5 +++-- ...ff__flake8_simplify__tests__SIM105_SIM105.py.snap | 10 ++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/resources/test/fixtures/flake8_simplify/SIM105.py b/resources/test/fixtures/flake8_simplify/SIM105.py index 8024bfd2a28ac..178947b5f5ebc 100644 --- a/resources/test/fixtures/flake8_simplify/SIM105.py +++ b/resources/test/fixtures/flake8_simplify/SIM105.py @@ -16,6 +16,11 @@ def foo(): except: # SIM105 pass +try: + foo() +except (a.Error, b.Error): # SIM105 + pass + try: foo() except ValueError: @@ -29,3 +34,10 @@ def foo(): pass else: print('bar') + +try: + foo() +except ValueError: + pass +finally: + print('bar') diff --git a/src/checkers/ast.rs b/src/checkers/ast.rs index 01f76dfda66ea..16cd4d672c75d 100644 --- a/src/checkers/ast.rs +++ b/src/checkers/ast.rs @@ -1248,7 +1248,10 @@ where } } StmtKind::Try { - handlers, orelse, .. + handlers, + orelse, + finalbody, + .. } => { if self.settings.enabled.contains(&CheckCode::F707) { if let Some(check) = @@ -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, .. } => { diff --git a/src/flake8_simplify/plugins/use_contextlib_suppress.rs b/src/flake8_simplify/plugins/use_contextlib_suppress.rs index a97d8a8f1dfb1..e09cb16ca3632 100644 --- a/src/flake8_simplify/plugins/use_contextlib_suppress.rs +++ b/src/flake8_simplify/plugins/use_contextlib_suppress.rs @@ -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]; @@ -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() diff --git a/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM105_SIM105.py.snap b/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM105_SIM105.py.snap index e062c1774b249..83cde9f7c0d2e 100644 --- a/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM105_SIM105.py.snap +++ b/src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM105_SIM105.py.snap @@ -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: ~