@@ -17,6 +17,8 @@ use rustc_target::spec::abi::Abi;
1717use rustc_typeck:: hir_ty_to_ty;
1818use syntax:: ast:: { FloatTy , IntTy , LitIntType , LitKind , UintTy } ;
1919use syntax:: errors:: DiagnosticBuilder ;
20+ use syntax:: ext:: base:: MacroKind ;
21+ use syntax:: ext:: hygiene:: ExpnKind ;
2022use syntax:: source_map:: Span ;
2123use syntax:: symbol:: { sym, Symbol } ;
2224
@@ -485,7 +487,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnitValue {
485487}
486488
487489declare_clippy_lint ! {
488- /// **What it does:** Checks for comparisons to unit.
490+ /// **What it does:** Checks for comparisons to unit. This includes all binary
491+ /// comparisons (like `==` and `<`) and asserts.
489492 ///
490493 /// **Why is this bad?** Unit is always equal to itself, and thus is just a
491494 /// clumsily written constant. Mostly this happens when someone accidentally
@@ -517,6 +520,14 @@ declare_clippy_lint! {
517520 /// baz();
518521 /// }
519522 /// ```
523+ ///
524+ /// For asserts:
525+ /// ```rust
526+ /// # fn foo() {};
527+ /// # fn bar() {};
528+ /// assert_eq!({ foo(); }, { bar(); });
529+ /// ```
530+ /// will always succeed
520531 pub UNIT_CMP ,
521532 correctness,
522533 "comparing unit values"
@@ -527,6 +538,30 @@ declare_lint_pass!(UnitCmp => [UNIT_CMP]);
527538impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnitCmp {
528539 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) {
529540 if expr. span . from_expansion ( ) {
541+ if let Some ( callee) = expr. span . source_callee ( ) {
542+ if let ExpnKind :: Macro ( MacroKind :: Bang , symbol) = callee. kind {
543+ if let ExprKind :: Binary ( ref cmp, ref left, _) = expr. kind {
544+ let op = cmp. node ;
545+ if op. is_comparison ( ) && is_unit ( cx. tables . expr_ty ( left) ) {
546+ let result = match & * symbol. as_str ( ) {
547+ "assert_eq" | "debug_assert_eq" => "succeed" ,
548+ "assert_ne" | "debug_assert_ne" => "fail" ,
549+ _ => return ,
550+ } ;
551+ span_lint (
552+ cx,
553+ UNIT_CMP ,
554+ expr. span ,
555+ & format ! (
556+ "`{}` of unit values detected. This will always {}" ,
557+ symbol. as_str( ) ,
558+ result
559+ ) ,
560+ ) ;
561+ }
562+ }
563+ }
564+ }
530565 return ;
531566 }
532567 if let ExprKind :: Binary ( ref cmp, ref left, _) = expr. kind {
0 commit comments