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