Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/flake8_blind_except/rules/blind_except.rs (Rust)

Code before:
    fn visit_stmt(&mut self, stmt: &'a Stmt) {
        if self.seen {
            return;
        }
        match stmt {
            Stmt::Expr(ast::StmtExpr { value, .. }) => {
                if let Expr::Call(ast::ExprCall {
                    func, arguments, ..
                }) = value.as_ref()
                {
                    match func.as_ref() {
                        Expr::Attribute(ast::ExprAttribute { attr, .. }) => {
                            if logging::is_logger_candidate(
                                func,
                                self.semantic,
                                self.logger_objects,
                            ) {
                                if match attr.as_str() {
                                    "exception" => true,
                                    _ if is_logger_method_name(attr) => {
                                        arguments.find_keyword("exc_info").is_some_and(|keyword| {
                                            Truthiness::from_expr(&keyword.value, |id| {
                                                self.semantic.has_builtin_binding(id)
                                            })
                                            .into_bool()
                                            .unwrap_or(true) // into_bool() can only return None if contained value is Truthiness::Unknown which can be exception object
                                        })
                                    }
                                    _ => false,
                                } {
                                    self.seen = true;
                                }
                            }
                        }
                        Expr::Name(ast::ExprName { .. }) => {
                            if self.semantic.resolve_qualified_name(func).is_some_and(
                                |qualified_name| match qualified_name.segments() {
                                    ["logging", "exception"] => true,
                                    ["logging", method] if is_logger_method_name(method) => {
                                        arguments.find_keyword("exc_info").is_some_and(|keyword| {
                                            Truthiness::from_expr(&keyword.value, |id| {
                                                self.semantic.has_builtin_binding(id)
                                            })
                                            .into_bool()
                                            .unwrap_or(true) // into_bool() can only return None if contained value is Truthiness::Unknown which can be exception object
                                        })
                                    }
                                    _ => false,
                                },
                            ) {
                                self.seen = true;

Review comment (refactor):
I think we should factor this out into a helper function since we use it twice and it's a bit unwieldy as a closure anyway.

Code after:
    } else {
        false
    }
}

/// A visitor to detect whether the exception was logged.
struct LogExceptionVisitor<'a> {
    semantic: &'a SemanticModel<'a>,
    logger_objects: &'a [String],
    settings: &'a LinterSettings,
    seen: bool,
}

impl<'a> LogExceptionVisitor<'a> {
    /// Create a new [`LogExceptionVisitor`] with the given exception name.
    fn new(
        semantic: &'a SemanticModel<'a>,
        logger_objects: &'a [String],
        settings: &'a LinterSettings,
    ) -> Self {
        Self {
            semantic,
            logger_objects,
            settings,
            seen: false,
        }
    }

    /// Returns `true` if the exception was logged.
    fn seen(&self) -> bool {
        self.seen
    }
}

impl<'a> StatementVisitor<'a> for LogExceptionVisitor<'a> {
    fn visit_stmt(&mut self, stmt: &'a Stmt) {
        if self.seen {
            return;
        }
        match stmt {
            Stmt::Expr(ast::StmtExpr { value, .. }) => {
                if let Expr::Call(ast::ExprCall {
                    func, arguments, ..
                }) = value.as_ref()
                {
                    match func.as_ref() {
                        Expr::Attribute(ast::ExprAttribute { attr, .. }) => {
                            if logging::is_logger_candidate(
                                func,
                                self.semantic,
                                self.logger_objects,