diff --git a/clippy_lints/src/should_assert_eq.rs b/clippy_lints/src/should_assert_eq.rs index 59532f3d440..93baab86347 100644 --- a/clippy_lints/src/should_assert_eq.rs +++ b/clippy_lints/src/should_assert_eq.rs @@ -1,6 +1,6 @@ use rustc::lint::*; use rustc::hir::*; -use utils::{is_direct_expn_of, implements_trait, span_lint}; +use utils::{is_direct_expn_of, is_expn_of, implements_trait, span_lint}; /// **What it does:** Checks for `assert!(x == y)` or `assert!(x != y)` which can be better written /// using `assert_eq` or `assert_ne` if `x` and `y` implement `Debug` trait. @@ -39,6 +39,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ShouldAssertEq { is_direct_expn_of(e.span, "assert").is_some(), let Some(debug_trait) = cx.tcx.lang_items.debug_trait(), ], { + let debug = is_expn_of(e.span, "debug_assert").map_or("", |_| "debug_"); let sugg = match binop.node { BinOp_::BiEq => "assert_eq", BinOp_::BiNe => "assert_ne", @@ -52,7 +53,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ShouldAssertEq { if implements_trait(cx, ty1, debug_trait, &[], Some(parent)) && implements_trait(cx, ty2, debug_trait, &[], Some(parent)) { - span_lint(cx, SHOULD_ASSERT_EQ, e.span, &format!("use `{}` for better reporting", sugg)); + span_lint(cx, SHOULD_ASSERT_EQ, e.span, &format!("use `{}{}` for better reporting", debug, sugg)); } }} } diff --git a/tests/ui/should_assert_eq.rs b/tests/ui/should_assert_eq.rs index f00278bfc7e..a5d11d907f8 100644 --- a/tests/ui/should_assert_eq.rs +++ b/tests/ui/should_assert_eq.rs @@ -18,6 +18,9 @@ fn main() { assert!(NonDebug(1) != NonDebug(2)); // ok test_generic(1, 2, 3, 4); + + debug_assert!(4 == 5); + debug_assert!(4 != 6); } fn test_generic(x: T, y: T, z: U, w: U) { diff --git a/tests/ui/should_assert_eq.stderr b/tests/ui/should_assert_eq.stderr index 8dcf71698df..3e5013fd3d8 100644 --- a/tests/ui/should_assert_eq.stderr +++ b/tests/ui/should_assert_eq.stderr @@ -27,21 +27,37 @@ error: use `assert_ne` for better reporting | = note: this error originates in a macro outside of the current crate -error: use `assert_eq` for better reporting - --> $DIR/should_assert_eq.rs:24:5 +error: use `debug_assert_eq` for better reporting + --> $DIR/should_assert_eq.rs:22:5 | -24 | assert!(x == y); +22 | debug_assert!(4 == 5); + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in a macro outside of the current crate + +error: use `debug_assert_ne` for better reporting + --> $DIR/should_assert_eq.rs:23:5 + | +23 | debug_assert!(4 != 6); + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in a macro outside of the current crate + +error: use `assert_eq` for better reporting + --> $DIR/should_assert_eq.rs:27:5 + | +27 | assert!(x == y); | ^^^^^^^^^^^^^^^^ | = note: this error originates in a macro outside of the current crate error: use `assert_ne` for better reporting - --> $DIR/should_assert_eq.rs:27:5 + --> $DIR/should_assert_eq.rs:30:5 | -27 | assert!(x != y); +30 | assert!(x != y); | ^^^^^^^^^^^^^^^^ | = note: this error originates in a macro outside of the current crate -error: aborting due to 5 previous errors +error: aborting due to 7 previous errors