@@ -17,6 +17,8 @@ use rustc_target::spec::abi::Abi;
17
17
use rustc_typeck:: hir_ty_to_ty;
18
18
use syntax:: ast:: { FloatTy , IntTy , LitIntType , LitKind , UintTy } ;
19
19
use syntax:: errors:: DiagnosticBuilder ;
20
+ use syntax:: ext:: base:: MacroKind ;
21
+ use syntax:: ext:: hygiene:: ExpnKind ;
20
22
use syntax:: source_map:: Span ;
21
23
use syntax:: symbol:: { sym, Symbol } ;
22
24
@@ -485,7 +487,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnitValue {
485
487
}
486
488
487
489
declare_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.
489
492
///
490
493
/// **Why is this bad?** Unit is always equal to itself, and thus is just a
491
494
/// clumsily written constant. Mostly this happens when someone accidentally
@@ -517,6 +520,14 @@ declare_clippy_lint! {
517
520
/// baz();
518
521
/// }
519
522
/// ```
523
+ ///
524
+ /// For asserts:
525
+ /// ```rust
526
+ /// # fn foo() {};
527
+ /// # fn bar() {};
528
+ /// assert_eq!({ foo(); }, { bar(); });
529
+ /// ```
530
+ /// will always succeed
520
531
pub UNIT_CMP ,
521
532
correctness,
522
533
"comparing unit values"
@@ -527,6 +538,30 @@ declare_lint_pass!(UnitCmp => [UNIT_CMP]);
527
538
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnitCmp {
528
539
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) {
529
540
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
+ }
530
565
return ;
531
566
}
532
567
if let ExprKind :: Binary ( ref cmp, ref left, _) = expr. kind {
0 commit comments