2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-12-06 05:33:31 -06:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-12-06 05:33:31 -06:00
|
|
|
use clippy_utils::{get_parent_expr, is_lang_ctor, match_def_path, paths};
|
2019-06-18 22:22:51 -05:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
2021-04-22 04:31:13 -05:00
|
|
|
use rustc_hir::LangItem::ResultErr;
|
2020-08-28 09:10:16 -05:00
|
|
|
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath};
|
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;
|
2020-08-11 08:43:21 -05:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-12-06 05:33:31 -06:00
|
|
|
use rustc_span::{hygiene, sym};
|
2019-06-18 22:22:51 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usages of `Err(x)?`.
|
2019-06-18 22:22:51 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The `?` operator is designed to allow calls that
|
2019-06-18 22:22:51 -05:00
|
|
|
/// 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)`.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### 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)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.38.0"]
|
2019-06-18 22:22:51 -05:00
|
|
|
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);
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Match(match_arg, _, MatchSource::TryDesugar) = expr.kind;
|
|
|
|
if let ExprKind::Call(match_fun, try_args) = match_arg.kind;
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
|
2021-11-16 14:44:25 -06:00
|
|
|
if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..));
|
2021-04-08 10:50:13 -05:00
|
|
|
if let Some(try_arg) = try_args.get(0);
|
|
|
|
if let ExprKind::Call(err_fun, err_args) = try_arg.kind;
|
|
|
|
if let Some(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;
|
2021-04-22 04:31:13 -05:00
|
|
|
if is_lang_ctor(cx, err_fun_path, ResultErr);
|
2020-08-11 08:43:21 -05:00
|
|
|
if let Some(return_ty) = find_return_type(cx, &expr.kind);
|
2019-06-18 22:22:51 -05:00
|
|
|
then {
|
2020-08-11 08:43:21 -05:00
|
|
|
let prefix;
|
|
|
|
let suffix;
|
|
|
|
let err_ty;
|
|
|
|
|
|
|
|
if let Some(ty) = result_error_type(cx, return_ty) {
|
|
|
|
prefix = "Err(";
|
|
|
|
suffix = ")";
|
|
|
|
err_ty = ty;
|
|
|
|
} else if let Some(ty) = poll_result_error_type(cx, return_ty) {
|
|
|
|
prefix = "Poll::Ready(Err(";
|
|
|
|
suffix = "))";
|
|
|
|
err_ty = ty;
|
|
|
|
} else if let Some(ty) = poll_option_result_error_type(cx, return_ty) {
|
|
|
|
prefix = "Poll::Ready(Some(Err(";
|
|
|
|
suffix = ")))";
|
|
|
|
err_ty = ty;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
|
2021-12-06 05:33:31 -06:00
|
|
|
let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
|
2021-04-22 04:31:13 -05:00
|
|
|
let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
|
|
|
|
"" // already returns
|
|
|
|
} else {
|
|
|
|
"return "
|
|
|
|
};
|
2020-08-11 08:43:21 -05:00
|
|
|
let suggestion = if err_ty == expr_err_ty {
|
2021-04-22 04:31:13 -05:00
|
|
|
format!("{}{}{}{}", ret_prefix, prefix, origin_snippet, suffix)
|
2019-06-18 22:22:51 -05:00
|
|
|
} else {
|
2021-04-22 04:31:13 -05:00
|
|
|
format!("{}{}{}.into(){}", ret_prefix, prefix, origin_snippet, suffix)
|
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,
|
2021-12-06 05:33:31 -06:00
|
|
|
applicability,
|
2019-06-18 22:22:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 08:43:21 -05:00
|
|
|
/// Finds function return type by examining return expressions in match arms.
|
|
|
|
fn find_return_type<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx ExprKind<'_>) -> Option<Ty<'tcx>> {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Match(_, arms, MatchSource::TryDesugar) = expr {
|
2020-08-11 08:43:21 -05:00
|
|
|
for arm in arms.iter() {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let ExprKind::Ret(Some(ret)) = arm.body.kind {
|
2020-08-11 08:43:21 -05:00
|
|
|
return Some(cx.typeck_results().expr_ty(ret));
|
|
|
|
}
|
|
|
|
}
|
2019-06-18 22:22:51 -05:00
|
|
|
}
|
2020-08-11 08:43:21 -05:00
|
|
|
None
|
2019-06-18 22:22:51 -05:00
|
|
|
}
|
|
|
|
|
2020-08-11 08:43:21 -05:00
|
|
|
/// Extracts the error type from Result<T, E>.
|
|
|
|
fn result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
2019-06-18 22:22:51 -05:00
|
|
|
if_chain! {
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(_, subst) = ty.kind();
|
2021-10-02 18:51:01 -05:00
|
|
|
if is_type_diagnostic_item(cx, ty, sym::Result);
|
2020-08-11 08:43:21 -05:00
|
|
|
then {
|
2021-04-08 10:50:13 -05:00
|
|
|
Some(subst.type_at(1))
|
2020-08-11 08:43:21 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the error type from Poll<Result<T, E>>.
|
|
|
|
fn poll_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
if_chain! {
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(def, subst) = ty.kind();
|
2020-08-11 08:43:21 -05:00
|
|
|
if match_def_path(cx, def.did, &paths::POLL);
|
|
|
|
let ready_ty = subst.type_at(0);
|
|
|
|
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
|
2021-10-02 18:51:01 -05:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Result, ready_def.did);
|
2020-08-11 08:43:21 -05:00
|
|
|
then {
|
2021-04-08 10:50:13 -05:00
|
|
|
Some(ready_subst.type_at(1))
|
2020-08-11 08:43:21 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the error type from Poll<Option<Result<T, E>>>.
|
|
|
|
fn poll_option_result_error_type<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
if_chain! {
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(def, subst) = ty.kind();
|
2020-08-11 08:43:21 -05:00
|
|
|
if match_def_path(cx, def.did, &paths::POLL);
|
|
|
|
let ready_ty = subst.type_at(0);
|
|
|
|
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(ready_def, ready_subst) = ready_ty.kind();
|
2021-10-02 18:51:01 -05:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Option, ready_def.did);
|
2020-08-11 08:43:21 -05:00
|
|
|
let some_ty = ready_subst.type_at(0);
|
|
|
|
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Adt(some_def, some_subst) = some_ty.kind();
|
2021-10-02 18:51:01 -05:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Result, some_def.did);
|
2019-06-18 22:22:51 -05:00
|
|
|
then {
|
2021-04-08 10:50:13 -05:00
|
|
|
Some(some_subst.type_at(1))
|
2019-06-18 22:22:51 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|