summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/matches/try_err.rs
blob: 663277d11365fa53e11f8f886d631d2dbcb33bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{get_parent_expr, is_lang_ctor, match_def_path, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::LangItem::ResultErr;
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_span::{hygiene, sym};

use super::TRY_ERR;

pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutinee: &'tcx Expr<'_>) {
    // Looks for a structure like this:
    // match ::std::ops::Try::into_result(Err(5)) {
    //     ::std::result::Result::Err(err) =>
    //         #[allow(unreachable_code)]
    //         return ::std::ops::Try::from_error(::std::convert::From::from(err)),
    //     ::std::result::Result::Ok(val) =>
    //         #[allow(unreachable_code)]
    //         val,
    // };
    if_chain! {
        if let ExprKind::Call(match_fun, [try_arg, ..]) = scrutinee.kind;
        if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
        if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..));
        if let ExprKind::Call(err_fun, [err_arg, ..]) = try_arg.kind;
        if let ExprKind::Path(ref err_fun_path) = err_fun.kind;
        if is_lang_ctor(cx, err_fun_path, ResultErr);
        if let Some(return_ty) = find_return_type(cx, &expr.kind);
        then {
            let prefix;
            let suffix;
            let err_ty;

            if let Some(ty) = result_error_type(cx, return_ty) {
                prefix = "Err(";
                suffix = ")";
                err_ty = ty;
            } else if let Some(ty) = poll_result_error_type(cx, return_ty) {
                prefix = "Poll::Ready(Err(";
                suffix = "))";
                err_ty = ty;
            } else if let Some(ty) = poll_option_result_error_type(cx, return_ty) {
                prefix = "Poll::Ready(Some(Err(";
                suffix = ")))";
                err_ty = ty;
            } else {
                return;
            };

            let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
            let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
            let mut applicability = Applicability::MachineApplicable;
            let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
            let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
                "" // already returns
            } else {
                "return "
            };
            let suggestion = if err_ty == expr_err_ty {
                format!("{}{}{}{}", ret_prefix, prefix, origin_snippet, suffix)
            } else {
                format!("{}{}{}.into(){}", ret_prefix, prefix, origin_snippet, suffix)
            };

            span_lint_and_sugg(
                cx,
                TRY_ERR,
                expr.span,
                "returning an `Err(_)` with the `?` operator",
                "try this",
                suggestion,
                applicability,
            );
        }
    }
}

/// Finds function return type by examining return expressions in match arms.
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
    if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
        for arm in arms.iter() {
            if let ExprKind::Ret(Some(ret)) = arm.body.kind {
                return Some(cx.typeck_results().expr_ty(ret));
            }
        }
    }
    None
}

/// Extracts the error type from Result<T, E>.
fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
    if_chain! {
        if let ty::Adt(_, subst) = ty.kind();
        if is_type_diagnostic_item(cx, ty, sym::Result);
        then {
            Some(subst.type_at(1))
        } else {
            None
        }
    }
}

/// Extracts the error type from Poll<Result<T, E>>.
fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
    if_chain! {
        if let ty::Adt(def, subst) = ty.kind();
        if match_def_path(cx, def.did(), &paths::POLL);
        let ready_ty = subst.type_at(0);

        if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
        if cx.tcx.is_diagnostic_item(sym::Result, ready_def.did());
        then {
            Some(ready_subst.type_at(1))
        } else {
            None
        }
    }
}

/// Extracts the error type from Poll<Option<Result<T, E>>>.
fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
    if_chain! {
        if let ty::Adt(def, subst) = ty.kind();
        if match_def_path(cx, def.did(), &paths::POLL);
        let ready_ty = subst.type_at(0);

        if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
        if cx.tcx.is_diagnostic_item(sym::Option, ready_def.did());
        let some_ty = ready_subst.type_at(0);

        if let ty::Adt(some_def, some_subst) = some_ty.kind();
        if cx.tcx.is_diagnostic_item(sym::Result, some_def.did());
        then {
            Some(some_subst.type_at(1))
        } else {
            None
        }
    }
}