Fix false-positive for TRY302 if exception cause is given (#4559)

This commit is contained in:
Arne de Laat 2023-05-21 17:49:53 +02:00 committed by GitHub
parent 6db05d8cc6
commit 8ca3977602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -68,6 +68,18 @@ def bad():
except Exception as e: except Exception as e:
raise 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(): def fine():
try: try:
process() process()

View File

@ -44,7 +44,7 @@ pub(crate) fn useless_try_except(checker: &mut Checker, handlers: &[Excepthandle
.iter() .iter()
.map(|handler| { .map(|handler| {
let ExceptHandler(ExcepthandlerExceptHandler { name, body, .. }) = 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; return None;
}; };
if let Some(expr) = exc { if let Some(expr) = exc {