]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/matches/try_err.rs
Auto merge of #105592 - matthiaskrgr:rollup-1cazogq, r=matthiaskrgr
[rust.git] / src / tools / clippy / clippy_lints / src / matches / try_err.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_applicability;
3 use clippy_utils::ty::is_type_diagnostic_item;
4 use clippy_utils::{get_parent_expr, is_res_lang_ctor, path_res};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::LangItem::ResultErr;
8 use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath};
9 use rustc_lint::LateContext;
10 use rustc_middle::ty::{self, Ty};
11 use rustc_span::{hygiene, sym};
12
13 use super::TRY_ERR;
14
15 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutinee: &'tcx Expr<'_>) {
16     // Looks for a structure like this:
17     // match ::std::ops::Try::into_result(Err(5)) {
18     //     ::std::result::Result::Err(err) =>
19     //         #[allow(unreachable_code)]
20     //         return ::std::ops::Try::from_error(::std::convert::From::from(err)),
21     //     ::std::result::Result::Ok(val) =>
22     //         #[allow(unreachable_code)]
23     //         val,
24     // };
25     if_chain! {
26         if let ExprKind::Call(match_fun, [try_arg, ..]) = scrutinee.kind;
27         if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
28         if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..));
29         if let ExprKind::Call(err_fun, [err_arg, ..]) = try_arg.kind;
30         if is_res_lang_ctor(cx, path_res(cx, err_fun), ResultErr);
31         if let Some(return_ty) = find_return_type(cx, &expr.kind);
32         then {
33             let prefix;
34             let suffix;
35             let err_ty;
36
37             if let Some(ty) = result_error_type(cx, return_ty) {
38                 prefix = "Err(";
39                 suffix = ")";
40                 err_ty = ty;
41             } else if let Some(ty) = poll_result_error_type(cx, return_ty) {
42                 prefix = "Poll::Ready(Err(";
43                 suffix = "))";
44                 err_ty = ty;
45             } else if let Some(ty) = poll_option_result_error_type(cx, return_ty) {
46                 prefix = "Poll::Ready(Some(Err(";
47                 suffix = ")))";
48                 err_ty = ty;
49             } else {
50                 return;
51             };
52
53             let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
54             let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
55             let mut applicability = Applicability::MachineApplicable;
56             let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
57             let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
58                 "" // already returns
59             } else {
60                 "return "
61             };
62             let suggestion = if err_ty == expr_err_ty {
63                 format!("{ret_prefix}{prefix}{origin_snippet}{suffix}")
64             } else {
65                 format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}")
66             };
67
68             span_lint_and_sugg(
69                 cx,
70                 TRY_ERR,
71                 expr.span,
72                 "returning an `Err(_)` with the `?` operator",
73                 "try this",
74                 suggestion,
75                 applicability,
76             );
77         }
78     }
79 }
80
81 /// Finds function return type by examining return expressions in match arms.
82 fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
83     if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
84         for arm in arms.iter() {
85             if let ExprKind::Ret(Some(ret)) = arm.body.kind {
86                 return Some(cx.typeck_results().expr_ty(ret));
87             }
88         }
89     }
90     None
91 }
92
93 /// Extracts the error type from Result<T, E>.
94 fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
95     if_chain! {
96         if let ty::Adt(_, subst) = ty.kind();
97         if is_type_diagnostic_item(cx, ty, sym::Result);
98         then {
99             Some(subst.type_at(1))
100         } else {
101             None
102         }
103     }
104 }
105
106 /// Extracts the error type from Poll<Result<T, E>>.
107 fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
108     if_chain! {
109         if let ty::Adt(def, subst) = ty.kind();
110         if cx.tcx.lang_items().get(LangItem::Poll) == Some(def.did());
111         let ready_ty = subst.type_at(0);
112
113         if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
114         if cx.tcx.is_diagnostic_item(sym::Result, ready_def.did());
115         then {
116             Some(ready_subst.type_at(1))
117         } else {
118             None
119         }
120     }
121 }
122
123 /// Extracts the error type from Poll<Option<Result<T, E>>>.
124 fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
125     if_chain! {
126         if let ty::Adt(def, subst) = ty.kind();
127         if cx.tcx.lang_items().get(LangItem::Poll) == Some(def.did());
128         let ready_ty = subst.type_at(0);
129
130         if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
131         if cx.tcx.is_diagnostic_item(sym::Option, ready_def.did());
132         let some_ty = ready_subst.type_at(0);
133
134         if let ty::Adt(some_def, some_subst) = some_ty.kind();
135         if cx.tcx.is_diagnostic_item(sym::Result, some_def.did());
136         then {
137             Some(some_subst.type_at(1))
138         } else {
139             None
140         }
141     }
142 }