1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use rustc::lint::*;
use rustc::hir::*;
use utils::{is_direct_expn_of, is_expn_of, implements_trait, span_lint};
declare_lint! {
pub SHOULD_ASSERT_EQ,
Warn,
"using `assert` macro for asserting equality"
}
pub struct ShouldAssertEq;
impl LintPass for ShouldAssertEq {
fn get_lints(&self) -> LintArray {
lint_array![SHOULD_ASSERT_EQ]
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ShouldAssertEq {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if_let_chain! {[
let ExprIf(ref cond, ..) = e.node,
let ExprUnary(UnOp::UnNot, ref cond) = cond.node,
let ExprBinary(ref binop, ref expr1, ref expr2) = cond.node,
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",
_ => return,
};
let ty1 = cx.tables.expr_ty(expr1);
let ty2 = cx.tables.expr_ty(expr2);
let parent = cx.tcx.hir.get_parent(e.id);
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", debug, sugg));
}
}}
}
}