Skip to content

Commit

Permalink
Implement reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Jul 9, 2023
1 parent 496a329 commit de0df6e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
35 changes: 7 additions & 28 deletions crates/ruff/src/rules/tryceratops/rules/raise_within_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComparableExpr> = helpers::extract_handled_exceptions(handlers)
.into_iter()
let handled_exceptions = helpers::extract_handled_exceptions(handlers);
let comparables: Vec<ComparableExpr> = handled_exceptions
.iter()
.filter_map(|handler| {
if let Expr::Name(ast::ExprName { .. }) = handler {
Some(ComparableExpr::from(handler))
Some(ComparableExpr::from(*handler))
} else {
None
}
Expand All @@ -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"])
Expand All @@ -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,
}
}
1 change: 0 additions & 1 deletion ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de0df6e

Please sign in to comment.