use rustc::lint::*;
use rustc::hir::*;
use utils::{is_direct_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 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", sugg));
}
}}
}
}