From 8ca39776024293c70686d7a519de880364bc44bd Mon Sep 17 00:00:00 2001 From: Arne de Laat Date: Sun, 21 May 2023 17:49:53 +0200 Subject: [PATCH] Fix false-positive for TRY302 if exception cause is given (#4559) --- .../resources/test/fixtures/tryceratops/TRY302.py | 12 ++++++++++++ .../rules/tryceratops/rules/useless_try_except.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/tryceratops/TRY302.py b/crates/ruff/resources/test/fixtures/tryceratops/TRY302.py index 0020926e4d2f3..3691e5472afc5 100644 --- a/crates/ruff/resources/test/fixtures/tryceratops/TRY302.py +++ b/crates/ruff/resources/test/fixtures/tryceratops/TRY302.py @@ -68,6 +68,18 @@ def bad(): except Exception as e: raise e +def fine(): + try: + process() + except Exception as e: + raise e from None + +def fine(): + try: + process() + except Exception as e: + raise e from Exception + def fine(): try: process() diff --git a/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs b/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs index e2446ddc6d679..8869e4eccdc26 100644 --- a/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs +++ b/crates/ruff/src/rules/tryceratops/rules/useless_try_except.rs @@ -44,7 +44,7 @@ pub(crate) fn useless_try_except(checker: &mut Checker, handlers: &[Excepthandle .iter() .map(|handler| { let ExceptHandler(ExcepthandlerExceptHandler { name, body, .. }) = handler; - let Some(Stmt::Raise(ast::StmtRaise { exc, .. })) = &body.first() else { + let Some(Stmt::Raise(ast::StmtRaise { exc, cause: None, .. })) = &body.first() else { return None; }; if let Some(expr) = exc {