1
+ use crate :: utils:: ast_utils:: eq_expr;
1
2
use crate :: utils:: {
2
3
eq_expr_value, implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then,
3
4
} ;
5
+ use if_chain:: if_chain;
6
+ use rustc_ast:: { ast, token} ;
4
7
use rustc_errors:: Applicability ;
5
8
use 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;
7
11
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
8
12
9
13
declare_clippy_lint ! {
@@ -23,6 +27,12 @@ declare_clippy_lint! {
23
27
/// # let x = 1;
24
28
/// if x + 1 == x + 1 {}
25
29
/// ```
30
+ /// or
31
+ /// ```rust
32
+ /// # let a = 3;
33
+ /// # let b = 4;
34
+ /// assert_eq!(a, a);
35
+ /// ```
26
36
pub EQ_OP ,
27
37
correctness,
28
38
"equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
@@ -52,6 +62,31 @@ declare_clippy_lint! {
52
62
53
63
declare_lint_pass ! ( EqOp => [ EQ_OP , OP_REF ] ) ;
54
64
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
+
55
90
impl < ' tcx > LateLintPass < ' tcx > for EqOp {
56
91
#[ allow( clippy:: similar_names, clippy:: too_many_lines) ]
57
92
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & ' tcx Expr < ' _ > ) {
0 commit comments