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