2019-08-19 11:30:32 -05:00
|
|
|
use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg};
|
2019-06-18 22:22:51 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{Arm, Expr, ExprKind, MatchSource};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_middle::ty::Ty;
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2019-06-18 22:22:51 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for usages of `Err(x)?`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The `?` operator is designed to allow calls that
|
|
|
|
/// can fail to be easily chained. For example, `foo()?.bar()` or
|
|
|
|
/// `foo(bar()?)`. Because `Err(x)?` can't be used that way (it will
|
|
|
|
/// always return), it is more clear to write `return Err(x)`.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-06-22 15:34:07 -05:00
|
|
|
/// ```rust
|
2019-06-18 22:22:51 -05:00
|
|
|
/// fn foo(fail: bool) -> Result<i32, String> {
|
|
|
|
/// if fail {
|
|
|
|
/// Err("failed")?;
|
|
|
|
/// }
|
|
|
|
/// Ok(0)
|
|
|
|
/// }
|
2019-06-22 15:34:07 -05:00
|
|
|
/// ```
|
|
|
|
/// Could be written:
|
2019-06-18 22:22:51 -05:00
|
|
|
///
|
2019-06-22 15:34:07 -05:00
|
|
|
/// ```rust
|
2019-06-18 22:22:51 -05:00
|
|
|
/// fn foo(fail: bool) -> Result<i32, String> {
|
|
|
|
/// if fail {
|
|
|
|
/// return Err("failed".into());
|
|
|
|
/// }
|
|
|
|
/// Ok(0)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub TRY_ERR,
|
|
|
|
style,
|
|
|
|
"return errors explicitly rather than hiding them behind a `?`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(TryErr => [TRY_ERR]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TryErr {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-06-18 22:22:51 -05:00
|
|
|
// Looks for a structure like this:
|
|
|
|
// match ::std::ops::Try::into_result(Err(5)) {
|
|
|
|
// ::std::result::Result::Err(err) =>
|
|
|
|
// #[allow(unreachable_code)]
|
|
|
|
// return ::std::ops::Try::from_error(::std::convert::From::from(err)),
|
|
|
|
// ::std::result::Result::Ok(val) =>
|
|
|
|
// #[allow(unreachable_code)]
|
|
|
|
// val,
|
|
|
|
// };
|
|
|
|
if_chain! {
|
2019-10-24 00:52:01 -05:00
|
|
|
if !in_external_macro(cx.tcx.sess, expr.span);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.kind;
|
|
|
|
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.kind;
|
|
|
|
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
|
2019-06-22 15:34:07 -05:00
|
|
|
if match_qpath(match_fun_path, &paths::TRY_INTO_RESULT);
|
2019-06-18 22:22:51 -05:00
|
|
|
if let Some(ref try_arg) = try_args.get(0);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.kind;
|
2019-06-18 22:22:51 -05:00
|
|
|
if let Some(ref err_arg) = err_args.get(0);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Path(ref err_fun_path) = err_fun.kind;
|
2019-06-18 22:22:51 -05:00
|
|
|
if match_qpath(err_fun_path, &paths::RESULT_ERR);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let Some(return_type) = find_err_return_type(cx, &expr.kind);
|
2019-06-18 22:22:51 -05:00
|
|
|
|
|
|
|
then {
|
2020-07-17 03:47:04 -05:00
|
|
|
let err_type = cx.typeck_results().expr_ty(err_arg);
|
2019-08-19 11:30:32 -05:00
|
|
|
let origin_snippet = if err_arg.span.from_expansion() {
|
2019-08-08 07:33:34 -05:00
|
|
|
snippet_with_macro_callsite(cx, err_arg.span, "_")
|
2019-08-08 07:33:34 -05:00
|
|
|
} else {
|
2019-08-08 07:33:34 -05:00
|
|
|
snippet(cx, err_arg.span, "_")
|
2019-08-08 07:33:34 -05:00
|
|
|
};
|
2019-06-18 22:22:51 -05:00
|
|
|
let suggestion = if err_type == return_type {
|
2019-08-08 07:33:34 -05:00
|
|
|
format!("return Err({})", origin_snippet)
|
2019-06-18 22:22:51 -05:00
|
|
|
} else {
|
2019-08-08 07:33:34 -05:00
|
|
|
format!("return Err({}.into())", origin_snippet)
|
2019-06-18 22:22:51 -05:00
|
|
|
};
|
|
|
|
|
2019-06-22 15:34:07 -05:00
|
|
|
span_lint_and_sugg(
|
2019-06-18 22:22:51 -05:00
|
|
|
cx,
|
|
|
|
TRY_ERR,
|
|
|
|
expr.span,
|
2019-06-22 15:34:07 -05:00
|
|
|
"returning an `Err(_)` with the `?` operator",
|
|
|
|
"try this",
|
|
|
|
suggestion,
|
2019-06-24 20:28:46 -05:00
|
|
|
Applicability::MachineApplicable
|
2019-06-18 22:22:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to determine whether to suggest `.into()` or not, we need to find the error type the
|
|
|
|
// function returns. To do that, we look for the From::from call (see tree above), and capture
|
|
|
|
// its output type.
|
2020-06-25 15:41:36 -05:00
|
|
|
fn find_err_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
|
2019-06-18 22:22:51 -05:00
|
|
|
if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
|
2019-06-22 15:34:07 -05:00
|
|
|
arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))
|
2019-06-18 22:22:51 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for From::from in one of the match arms.
|
2020-06-25 15:41:36 -05:00
|
|
|
fn find_err_return_type_arm<'tcx>(cx: &LateContext<'tcx>, arm: &'tcx Arm<'_>) -> Option<Ty<'tcx>> {
|
2019-06-18 22:22:51 -05:00
|
|
|
if_chain! {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Ret(Some(ref err_ret)) = arm.body.kind;
|
|
|
|
if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.kind;
|
|
|
|
if let ExprKind::Path(ref from_error_fn) = from_error_path.kind;
|
2019-06-22 15:34:07 -05:00
|
|
|
if match_qpath(from_error_fn, &paths::TRY_FROM_ERROR);
|
2019-06-18 22:22:51 -05:00
|
|
|
if let Some(from_error_arg) = from_error_args.get(0);
|
|
|
|
then {
|
2020-07-17 03:47:04 -05:00
|
|
|
Some(cx.typeck_results().expr_ty(from_error_arg))
|
2019-06-18 22:22:51 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|