1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::{has_debug_impl, is_type_diagnostic_item};
3 use if_chain::if_chain;
5 use rustc_lint::LateContext;
6 use rustc_middle::ty::{self, Ty};
11 /// lint use of `ok().expect()` for `Result`s
12 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
14 // lint if the caller of `ok()` is a `Result`
15 if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
16 let result_type = cx.typeck_results().expr_ty(recv);
17 if let Some(error_type) = get_error_type(cx, result_type);
18 if has_debug_impl(cx, error_type);
25 "called `ok().expect()` on a `Result` value",
27 "you can call `expect()` directly on the `Result`",
33 /// Given a `Result<T, E>` type, return its error type (`E`).
34 fn get_error_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
36 ty::Adt(_, substs) if is_type_diagnostic_item(cx, ty, sym::Result) => substs.types().nth(1),