diff --git a/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs b/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs index 0273435ced901e..76499821054ee5 100644 --- a/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs +++ b/crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs @@ -92,14 +92,12 @@ pub(crate) fn raise_within_try(checker: &mut Checker, body: &[Stmt], handlers: & return; } - // The names of exceptions handled by the `try` Stmt. We can't compare `Expr`'s since they have - // different ranges, but by virtue of this function's call path we know that the `raise` - // statements will always be within the surrounding `try`. - let handler_exprs: Vec = helpers::extract_handled_exceptions(handlers) - .into_iter() + let handled_exceptions = helpers::extract_handled_exceptions(handlers); + let comparables: Vec = handled_exceptions + .iter() .filter_map(|handler| { if let Expr::Name(ast::ExprName { .. }) = handler { - Some(ComparableExpr::from(handler)) + Some(ComparableExpr::from(*handler)) } else { None } @@ -111,18 +109,12 @@ pub(crate) fn raise_within_try(checker: &mut Checker, body: &[Stmt], handlers: & continue; }; - let Some(exc_name) = get_function_name(exception.as_ref()) else { - continue; - }; - // We can't check exception sub-classes without a type-checker implementation, so let's // just catch the blanket `Exception` for now. - // if (handler_exprs.contains(&"Exception") && checker.semantic().is_builtin("Exception")) - // || handler_exprs.contains(&exc_name) - if handler_exprs.contains(&ComparableExpr::from(map_callable(&exception))) - || handler_exprs.iter().any(|expr| { + if comparables.contains(&ComparableExpr::from(map_callable(exception))) + || handled_exceptions.iter().any(|expr| { checker - .resolve() + .semantic() .resolve_call_path(expr) .map_or(false, |call_path| { matches!(call_path.as_slice(), ["", "Exception" | "BaseException"]) @@ -135,16 +127,3 @@ pub(crate) fn raise_within_try(checker: &mut Checker, body: &[Stmt], handlers: & } } } - -/// Get the name of an [`Expr::Call`], if applicable. If the passed [`Expr`] isn't a [`Expr::Call`], return an -/// empty [`Option`]. -fn get_function_name(expr: &Expr) -> Option<&str> { - let Expr::Call(ast::ExprCall { func, .. }) = expr else { - return None; - }; - - match func.as_ref() { - Expr::Name(ast::ExprName { id, .. }) => Some(id.as_str()), - _ => None, - } -} diff --git a/ruff.schema.json b/ruff.schema.json index f283f253b88651..0948038c0a6352 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2400,7 +2400,6 @@ "RUF011", "RUF012", "RUF013", - "RUF014", "RUF1", "RUF10", "RUF100",