1 use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc_errors::Applicability;
4 use rustc_hir::{Arm, Expr, ExprKind, MatchSource};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::lint::in_external_macro;
7 use rustc_middle::ty::Ty;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for usages of `Err(x)?`.
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)`.
18 /// **Known problems:** None.
22 /// fn foo(fail: bool) -> Result<i32, String> {
32 /// fn foo(fail: bool) -> Result<i32, String> {
34 /// return Err("failed".into());
41 "return errors explicitly rather than hiding them behind a `?`"
44 declare_lint_pass!(TryErr => [TRY_ERR]);
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)]
58 if !in_external_macro(cx.tcx.sess, expr.span);
59 if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.kind;
60 if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.kind;
61 if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
62 if match_qpath(match_fun_path, &paths::TRY_INTO_RESULT);
63 if let Some(ref try_arg) = try_args.get(0);
64 if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.kind;
65 if let Some(ref err_arg) = err_args.get(0);
66 if let ExprKind::Path(ref err_fun_path) = err_fun.kind;
67 if match_qpath(err_fun_path, &paths::RESULT_ERR);
68 if let Some(return_type) = find_err_return_type(cx, &expr.kind);
71 let err_type = cx.tables.expr_ty(err_arg);
72 let origin_snippet = if err_arg.span.from_expansion() {
73 snippet_with_macro_callsite(cx, err_arg.span, "_")
75 snippet(cx, err_arg.span, "_")
77 let suggestion = if err_type == return_type {
78 format!("return Err({})", origin_snippet)
80 format!("return Err({}.into())", origin_snippet)
87 "returning an `Err(_)` with the `?` operator",
90 Applicability::MachineApplicable
97 // In order to determine whether to suggest `.into()` or not, we need to find the error type the
98 // function returns. To do that, we look for the From::from call (see tree above), and capture
100 fn find_err_return_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
101 if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
102 arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))
108 // Check for From::from in one of the match arms.
109 fn find_err_return_type_arm<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arm: &'tcx Arm<'_>) -> Option<Ty<'tcx>> {
111 if let ExprKind::Ret(Some(ref err_ret)) = arm.body.kind;
112 if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.kind;
113 if let ExprKind::Path(ref from_error_fn) = from_error_path.kind;
114 if match_qpath(from_error_fn, &paths::TRY_FROM_ERROR);
115 if let Some(from_error_arg) = from_error_args.get(0);
117 Some(cx.tables.expr_ty(from_error_arg))