]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/try_err.rs
Rollup merge of #91562 - dtolnay:asyncspace, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_lints / src / 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_lang_ctor, match_def_path, paths};
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, LateLintPass};
10 use rustc_middle::lint::in_external_macro;
11 use rustc_middle::ty::{self, Ty};
12 use rustc_session::{declare_lint_pass, declare_tool_lint};
13 use rustc_span::{hygiene, sym};
14
15 declare_clippy_lint! {
16     /// ### What it does
17     /// Checks for usages of `Err(x)?`.
18     ///
19     /// ### Why is this bad?
20     /// The `?` operator is designed to allow calls that
21     /// can fail to be easily chained. For example, `foo()?.bar()` or
22     /// `foo(bar()?)`. Because `Err(x)?` can't be used that way (it will
23     /// always return), it is more clear to write `return Err(x)`.
24     ///
25     /// ### Example
26     /// ```rust
27     /// fn foo(fail: bool) -> Result<i32, String> {
28     ///     if fail {
29     ///       Err("failed")?;
30     ///     }
31     ///     Ok(0)
32     /// }
33     /// ```
34     /// Could be written:
35     ///
36     /// ```rust
37     /// fn foo(fail: bool) -> Result<i32, String> {
38     ///     if fail {
39     ///       return Err("failed".into());
40     ///     }
41     ///     Ok(0)
42     /// }
43     /// ```
44     #[clippy::version = "1.38.0"]
45     pub TRY_ERR,
46     style,
47     "return errors explicitly rather than hiding them behind a `?`"
48 }
49
50 declare_lint_pass!(TryErr => [TRY_ERR]);
51
52 impl<'tcx> LateLintPass<'tcx> for TryErr {
53     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
54         // Looks for a structure like this:
55         // match ::std::ops::Try::into_result(Err(5)) {
56         //     ::std::result::Result::Err(err) =>
57         //         #[allow(unreachable_code)]
58         //         return ::std::ops::Try::from_error(::std::convert::From::from(err)),
59         //     ::std::result::Result::Ok(val) =>
60         //         #[allow(unreachable_code)]
61         //         val,
62         // };
63         if_chain! {
64             if !in_external_macro(cx.tcx.sess, expr.span);
65             if let ExprKind::Match(match_arg, _, MatchSource::TryDesugar) = expr.kind;
66             if let ExprKind::Call(match_fun, try_args) = match_arg.kind;
67             if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
68             if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, _));
69             if let Some(try_arg) = try_args.get(0);
70             if let ExprKind::Call(err_fun, err_args) = try_arg.kind;
71             if let Some(err_arg) = err_args.get(0);
72             if let ExprKind::Path(ref err_fun_path) = err_fun.kind;
73             if is_lang_ctor(cx, err_fun_path, ResultErr);
74             if let Some(return_ty) = find_return_type(cx, &expr.kind);
75             then {
76                 let prefix;
77                 let suffix;
78                 let err_ty;
79
80                 if let Some(ty) = result_error_type(cx, return_ty) {
81                     prefix = "Err(";
82                     suffix = ")";
83                     err_ty = ty;
84                 } else if let Some(ty) = poll_result_error_type(cx, return_ty) {
85                     prefix = "Poll::Ready(Err(";
86                     suffix = "))";
87                     err_ty = ty;
88                 } else if let Some(ty) = poll_option_result_error_type(cx, return_ty) {
89                     prefix = "Poll::Ready(Some(Err(";
90                     suffix = ")))";
91                     err_ty = ty;
92                 } else {
93                     return;
94                 };
95
96                 let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
97                 let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
98                 let mut applicability = Applicability::MachineApplicable;
99                 let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
100                 let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
101                     "" // already returns
102                 } else {
103                     "return "
104                 };
105                 let suggestion = if err_ty == expr_err_ty {
106                     format!("{}{}{}{}", ret_prefix, prefix, origin_snippet, suffix)
107                 } else {
108                     format!("{}{}{}.into(){}", ret_prefix, prefix, origin_snippet, suffix)
109                 };
110
111                 span_lint_and_sugg(
112                     cx,
113                     TRY_ERR,
114                     expr.span,
115                     "returning an `Err(_)` with the `?` operator",
116                     "try this",
117                     suggestion,
118                     applicability,
119                 );
120             }
121         }
122     }
123 }
124
125 /// Finds function return type by examining return expressions in match arms.
126 fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
127     if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
128         for arm in arms.iter() {
129             if let ExprKind::Ret(Some(ret)) = arm.body.kind {
130                 return Some(cx.typeck_results().expr_ty(ret));
131             }
132         }
133     }
134     None
135 }
136
137 /// Extracts the error type from Result<T, E>.
138 fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
139     if_chain! {
140         if let ty::Adt(_, subst) = ty.kind();
141         if is_type_diagnostic_item(cx, ty, sym::Result);
142         then {
143             Some(subst.type_at(1))
144         } else {
145             None
146         }
147     }
148 }
149
150 /// Extracts the error type from Poll<Result<T, E>>.
151 fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
152     if_chain! {
153         if let ty::Adt(def, subst) = ty.kind();
154         if match_def_path(cx, def.did, &paths::POLL);
155         let ready_ty = subst.type_at(0);
156
157         if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
158         if cx.tcx.is_diagnostic_item(sym::Result, ready_def.did);
159         then {
160             Some(ready_subst.type_at(1))
161         } else {
162             None
163         }
164     }
165 }
166
167 /// Extracts the error type from Poll<Option<Result<T, E>>>.
168 fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
169     if_chain! {
170         if let ty::Adt(def, subst) = ty.kind();
171         if match_def_path(cx, def.did, &paths::POLL);
172         let ready_ty = subst.type_at(0);
173
174         if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
175         if cx.tcx.is_diagnostic_item(sym::Option, ready_def.did);
176         let some_ty = ready_subst.type_at(0);
177
178         if let ty::Adt(some_def, some_subst) = some_ty.kind();
179         if cx.tcx.is_diagnostic_item(sym::Result, some_def.did);
180         then {
181             Some(some_subst.type_at(1))
182         } else {
183             None
184         }
185     }
186 }