Skip to content

Commit 5f4c836

Browse files
committed
Swap verification of debug impl on data type
This makes the lint check if T (instead of E) in `Result<T, E>` implements Debug.
1 parent 231c697 commit 5f4c836

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

clippy_lints/src/methods/err_expect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub(super) fn check(
2222
if meets_msrv(msrv, &msrvs::EXPECT_ERR);
2323

2424
let result_type = cx.typeck_results().expr_ty(recv);
25-
if let Some(error_type) = get_error_type(cx, result_type);
26-
if has_debug_impl(error_type, cx);
25+
if let Some(data_type) = get_data_type(cx, result_type);
26+
if has_debug_impl(data_type, cx);
2727

2828
then {
2929
span_lint_and_sugg(
@@ -39,10 +39,10 @@ pub(super) fn check(
3939
};
4040
}
4141

42-
/// Given a `Result<T, E>` type, return its error type (`E`).
43-
fn get_error_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
42+
/// Given a `Result<T, E>` type, return its data (`T`).
43+
fn get_data_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
4444
match ty.kind() {
45-
ty::Adt(_, substs) if is_type_diagnostic_item(cx, ty, sym::Result) => substs.types().nth(1),
45+
ty::Adt(_, substs) if is_type_diagnostic_item(cx, ty, sym::Result) => substs.types().nth(0),
4646
_ => None,
4747
}
4848
}

tests/ui/err_expect.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ struct MyTypeNonDebug;
66
struct MyTypeDebug;
77

88
fn main() {
9-
let test_debug: Result<u32, MyTypeDebug> = Ok(0);
9+
let test_debug: Result<MyTypeDebug, u32> = Ok(MyTypeDebug);
1010
test_debug.expect_err("Testing debug type");
1111

12-
let test_non_debug: Result<u32, MyTypeNonDebug> = Ok(0);
12+
let test_non_debug: Result<MyTypeNonDebug, u32> = Ok(MyTypeNonDebug);
1313
test_non_debug.err().expect("Testing non debug type");
1414
}

tests/ui/err_expect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ struct MyTypeNonDebug;
66
struct MyTypeDebug;
77

88
fn main() {
9-
let test_debug: Result<u32, MyTypeDebug> = Ok(0);
9+
let test_debug: Result<MyTypeDebug, u32> = Ok(MyTypeDebug);
1010
test_debug.err().expect("Testing debug type");
1111

12-
let test_non_debug: Result<u32, MyTypeNonDebug> = Ok(0);
12+
let test_non_debug: Result<MyTypeNonDebug, u32> = Ok(MyTypeNonDebug);
1313
test_non_debug.err().expect("Testing non debug type");
1414
}

0 commit comments

Comments
 (0)