mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-18 09:03:18 +00:00
distinguish debug_assert
This commit is contained in:
parent
fe85cde91c
commit
e75d7ffd2d
3 changed files with 28 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
use rustc::lint::*;
|
use rustc::lint::*;
|
||||||
use rustc::hir::*;
|
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
|
/// **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.
|
/// 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(),
|
is_direct_expn_of(e.span, "assert").is_some(),
|
||||||
let Some(debug_trait) = cx.tcx.lang_items.debug_trait(),
|
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 {
|
let sugg = match binop.node {
|
||||||
BinOp_::BiEq => "assert_eq",
|
BinOp_::BiEq => "assert_eq",
|
||||||
BinOp_::BiNe => "assert_ne",
|
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)) &&
|
if implements_trait(cx, ty1, debug_trait, &[], Some(parent)) &&
|
||||||
implements_trait(cx, ty2, 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));
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,9 @@ fn main() {
|
||||||
assert!(NonDebug(1) != NonDebug(2)); // ok
|
assert!(NonDebug(1) != NonDebug(2)); // ok
|
||||||
|
|
||||||
test_generic(1, 2, 3, 4);
|
test_generic(1, 2, 3, 4);
|
||||||
|
|
||||||
|
debug_assert!(4 == 5);
|
||||||
|
debug_assert!(4 != 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_generic<T: std::fmt::Debug + Eq, U: Eq>(x: T, y: T, z: U, w: U) {
|
fn test_generic<T: std::fmt::Debug + Eq, U: Eq>(x: T, y: T, z: U, w: U) {
|
||||||
|
|
|
@ -27,21 +27,37 @@ error: use `assert_ne` for better reporting
|
||||||
|
|
|
|
||||||
= note: this error originates in a macro outside of the current crate
|
= note: this error originates in a macro outside of the current crate
|
||||||
|
|
||||||
error: use `assert_eq` for better reporting
|
error: use `debug_assert_eq` for better reporting
|
||||||
--> $DIR/should_assert_eq.rs:24:5
|
--> $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
|
= note: this error originates in a macro outside of the current crate
|
||||||
|
|
||||||
error: use `assert_ne` for better reporting
|
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
|
= 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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue