1+ use crate :: utils:: ast_utils:: eq_expr;
12use crate :: utils:: {
23 eq_expr_value, implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then,
34} ;
5+ use if_chain:: if_chain;
6+ use rustc_ast:: { ast, token} ;
47use rustc_errors:: Applicability ;
58use rustc_hir:: { BinOp , BinOpKind , BorrowKind , Expr , ExprKind } ;
6- use rustc_lint:: { LateContext , LateLintPass } ;
9+ use rustc_lint:: { EarlyContext , EarlyLintPass , LateContext , LateLintPass } ;
10+ use rustc_parse:: parser;
711use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
812
913declare_clippy_lint ! {
@@ -23,6 +27,12 @@ declare_clippy_lint! {
2327 /// # let x = 1;
2428 /// if x + 1 == x + 1 {}
2529 /// ```
30+ /// or
31+ /// ```rust
32+ /// # let a = 3;
33+ /// # let b = 4;
34+ /// assert_eq!(a, a);
35+ /// ```
2636 pub EQ_OP ,
2737 correctness,
2838 "equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
@@ -52,6 +62,31 @@ declare_clippy_lint! {
5262
5363declare_lint_pass ! ( EqOp => [ EQ_OP , OP_REF ] ) ;
5464
65+ impl EarlyLintPass for EqOp {
66+ fn check_mac ( & mut self , cx : & EarlyContext < ' _ > , mac : & ast:: MacCall ) {
67+ if_chain ! {
68+ if mac. path == sym!( assert_eq) ;
69+ let tokens = mac. args. inner_tokens( ) ;
70+ let mut parser = parser:: Parser :: new( & cx. sess. parse_sess, tokens, false , None ) ;
71+ if let Ok ( left) = parser. parse_expr( ) ;
72+ if parser. eat( & token:: Comma ) ;
73+ if let Ok ( right) = parser. parse_expr( ) ;
74+ let left_expr = left. into_inner( ) ;
75+ let right_expr = right. into_inner( ) ;
76+ if eq_expr( & left_expr, & right_expr) ;
77+
78+ then {
79+ span_lint(
80+ cx,
81+ EQ_OP ,
82+ left_expr. span. to( right_expr. span) ,
83+ "identical args used in this `assert_eq!` macro call" ,
84+ ) ;
85+ }
86+ }
87+ }
88+ }
89+
5590impl < ' tcx > LateLintPass < ' tcx > for EqOp {
5691 #[ allow( clippy:: similar_names, clippy:: too_many_lines) ]
5792 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & ' tcx Expr < ' _ > ) {
0 commit comments