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