fn check_use_bail_and_ensure()

in gazebo_lint/src/lib.rs [481:508]


fn check_use_bail_and_ensure(cx: &LateContext, expr: &Expr) {
    // it's hard to check pre-expanded macros as we have no information on what the tokens actually
    // refer to. i.e. we can detect `anyhow::bail` and `bail` (with `use anyhow::`) only as pure
    // ast tokens. We cannot properly resolve them to make sure those are actually the anyhow
    // macros of interest.
    // Luckily, anyhow macros all expand to using `anyhow::private::Err`, so we can use the post
    // macro expansion and detect for that.
    if let ExprKind::Call(call, _) = expr.kind {
        if let ExprKind::Path(QPath::Resolved(_, path)) = &call.kind {
            if path.segments.len() == 3 {
                // the path here should be `anyhow::private::Err` if its a macro.
                // check for a length of 3.
                if clippy::path_to_res(cx, &["anyhow", "private", "Err"])
                    == clippy::path_to_res(cx, &["core", "result", "Result", "Err"])
                {
                    if let Some(res) = path.segments[1].res.and_then(unpack_non_local) {
                        if clippy::path_to_res(cx, &["anyhow", "private"])
                            .map_or(false, |x| x == res)
                            && path.segments[2].ident.as_str() == "Err"
                        {
                            emit_lint(cx, GAZEBO_LINT_ANYHOW_AVOID_BAIL_AND_ENSURE, expr.span);
                        }
                    }
                }
            }
        }
    }
}