]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/try_err.rs
Making try_err machine applicable
[rust.git] / clippy_lints / src / try_err.rs
1 use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc::hir::*;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::ty::Ty;
6 use rustc::{declare_lint_pass, declare_tool_lint};
7 use rustc_errors::Applicability;
8
9 declare_clippy_lint! {
10     /// **What it does:** Checks for usages of `Err(x)?`.
11     ///
12     /// **Why is this bad?** The `?` operator is designed to allow calls that
13     /// can fail to be easily chained. For example, `foo()?.bar()` or
14     /// `foo(bar()?)`. Because `Err(x)?` can't be used that way (it will
15     /// always return), it is more clear to write `return Err(x)`.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     /// ```rust
21     /// fn foo(fail: bool) -> Result<i32, String> {
22     ///     if fail {
23     ///       Err("failed")?;
24     ///     }
25     ///     Ok(0)
26     /// }
27     /// ```
28     /// Could be written:
29     ///
30     /// ```rust
31     /// fn foo(fail: bool) -> Result<i32, String> {
32     ///     if fail {
33     ///       return Err("failed".into());
34     ///     }
35     ///     Ok(0)
36     /// }
37     /// ```
38     pub TRY_ERR,
39     style,
40     "return errors explicitly rather than hiding them behind a `?`"
41 }
42
43 declare_lint_pass!(TryErr => [TRY_ERR]);
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
46     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
47         // Looks for a structure like this:
48         // match ::std::ops::Try::into_result(Err(5)) {
49         //     ::std::result::Result::Err(err) =>
50         //         #[allow(unreachable_code)]
51         //         return ::std::ops::Try::from_error(::std::convert::From::from(err)),
52         //     ::std::result::Result::Ok(val) =>
53         //         #[allow(unreachable_code)]
54         //         val,
55         // };
56         if_chain! {
57             if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.node;
58             if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.node;
59             if let ExprKind::Path(ref match_fun_path) = match_fun.node;
60             if match_qpath(match_fun_path, &paths::TRY_INTO_RESULT);
61             if let Some(ref try_arg) = try_args.get(0);
62             if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.node;
63             if let Some(ref err_arg) = err_args.get(0);
64             if let ExprKind::Path(ref err_fun_path) = err_fun.node;
65             if match_qpath(err_fun_path, &paths::RESULT_ERR);
66             if let Some(return_type) = find_err_return_type(cx, &expr.node);
67
68             then {
69                 let err_type = cx.tables.expr_ty(err_arg);
70                 let suggestion = if err_type == return_type {
71                     format!("return Err({})", snippet(cx, err_arg.span, "_"))
72                 } else {
73                     format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
74                 };
75
76                 span_lint_and_sugg(
77                     cx,
78                     TRY_ERR,
79                     expr.span,
80                     "returning an `Err(_)` with the `?` operator",
81                     "try this",
82                     suggestion,
83                     Applicability::MachineApplicable
84                 );
85             }
86         }
87     }
88 }
89
90 // In order to determine whether to suggest `.into()` or not, we need to find the error type the
91 // function returns. To do that, we look for the From::from call (see tree above), and capture
92 // its output type.
93 fn find_err_return_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx ExprKind) -> Option<Ty<'tcx>> {
94     if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
95         arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))
96     } else {
97         None
98     }
99 }
100
101 // Check for From::from in one of the match arms.
102 fn find_err_return_type_arm<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arm: &'tcx Arm) -> Option<Ty<'tcx>> {
103     if_chain! {
104         if let ExprKind::Ret(Some(ref err_ret)) = arm.body.node;
105         if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.node;
106         if let ExprKind::Path(ref from_error_fn) = from_error_path.node;
107         if match_qpath(from_error_fn, &paths::TRY_FROM_ERROR);
108         if let Some(from_error_arg) = from_error_args.get(0);
109         then {
110             Some(cx.tables.expr_ty(from_error_arg))
111         } else {
112             None
113         }
114     }
115 }