This commit is contained in:
Ada Alakbarova 2025-10-12 00:36:54 +02:00
parent 35f8bffb91
commit a4924e2727
No known key found for this signature in database
2 changed files with 9 additions and 14 deletions

View file

@ -1,7 +1,6 @@
use super::ERR_EXPECT;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::res::MaybeDef;
use clippy_utils::ty::has_debug_impl;
use rustc_errors::Applicability;
use rustc_lint::LateContext;
@ -17,12 +16,10 @@ pub(super) fn check(
err_span: Span,
msrv: Msrv,
) {
if cx.typeck_results().expr_ty(recv).is_diag_item(cx, sym::Result)
// Grabs the `Result<T, E>` type
&& let result_type = cx.typeck_results().expr_ty(recv)
// Tests if the T type in a `Result<T, E>` is not None
&& let Some(data_type) = get_data_type(cx, result_type)
// Tests if the T type in a `Result<T, E>` implements debug
let result_ty = cx.typeck_results().expr_ty(recv);
// Grabs the `Result<T, E>` type
if let Some(data_type) = get_data_type(cx, result_ty)
// Tests if the T type in a `Result<T, E>` implements Debug
&& has_debug_impl(cx, data_type)
&& msrv.meets(cx, msrvs::EXPECT_ERR)
{
@ -41,7 +38,7 @@ pub(super) fn check(
/// Given a `Result<T, E>` type, return its data (`T`).
fn get_data_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
match ty.kind() {
ty::Adt(_, args) if ty.is_diag_item(cx, sym::Result) => args.types().next(),
ty::Adt(adt, args) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => args.types().next(),
_ => None,
}
}

View file

@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::res::MaybeDef;
use clippy_utils::ty::has_debug_impl;
use rustc_hir as hir;
use rustc_lint::LateContext;
@ -10,10 +9,9 @@ use super::OK_EXPECT;
/// lint use of `ok().expect()` for `Result`s
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
if cx.typeck_results().expr_ty(recv).is_diag_item(cx, sym::Result)
// lint if the caller of `ok()` is a `Result`
&& let result_type = cx.typeck_results().expr_ty(recv)
&& let Some(error_type) = get_error_type(cx, result_type)
let result_ty = cx.typeck_results().expr_ty(recv);
// lint if the caller of `ok()` is a `Result`
if let Some(error_type) = get_error_type(cx, result_ty)
&& has_debug_impl(cx, error_type)
{
span_lint_and_help(
@ -30,7 +28,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
/// Given a `Result<T, E>` type, return its error type (`E`).
fn get_error_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
match ty.kind() {
ty::Adt(_, args) if ty.is_diag_item(cx, sym::Result) => args.types().nth(1),
ty::Adt(adt, args) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => args.types().nth(1),
_ => None,
}
}