Skip to content

Commit a3e0446

Browse files
committed
Extend to the assert macro family
1 parent 32fdb8f commit a3e0446

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

clippy_lints/src/eq_op.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast::{ast, token};
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOp, BinOpKind, BorrowKind, Expr, ExprKind};
99
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
10+
use rustc_middle::lint::in_external_macro;
1011
use rustc_parse::parser;
1112
use rustc_session::{declare_lint_pass, declare_tool_lint};
1213

@@ -64,10 +65,20 @@ declare_lint_pass!(EqOp => [EQ_OP, OP_REF]);
6465

6566
impl EarlyLintPass for EqOp {
6667
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
68+
let macro_list = [
69+
sym!(assert_eq),
70+
sym!(assert_ne),
71+
sym!(debug_assert_eq),
72+
sym!(debug_assert_ne),
73+
];
6774
if_chain! {
68-
if mac.path == sym!(assert_eq);
75+
if !in_external_macro(cx.sess, mac.span());
76+
if mac.path.segments.len() == 1;
77+
let macro_name = mac.path.segments[0].ident.name;
78+
if macro_list.contains(&macro_name);
6979
let tokens = mac.args.inner_tokens();
70-
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tokens, false, None);
80+
let mut parser = parser::Parser::new(
81+
&cx.sess.parse_sess, tokens, false, None);
7182
if let Ok(left) = parser.parse_expr();
7283
if parser.eat(&token::Comma);
7384
if let Ok(right) = parser.parse_expr();
@@ -80,7 +91,7 @@ impl EarlyLintPass for EqOp {
8091
cx,
8192
EQ_OP,
8293
left_expr.span.to(right_expr.span),
83-
"identical args used in this `assert_eq!` macro call",
94+
&format!("identical args used in this `{}!` macro call", macro_name),
8495
);
8596
}
8697
}

tests/ui/eq_op_early.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,32 @@ fn main() {
77
// lint identical args in `assert_eq!` (see #3574)
88
assert_eq!(a, a);
99
assert_eq!(a + 1, a + 1);
10-
1110
// ok
1211
assert_eq!(a, b);
1312
assert_eq!(a, a + 1);
1413
assert_eq!(a + 1, b + 1);
14+
15+
// lint identical args in `assert_ne!`
16+
assert_ne!(a, a);
17+
assert_ne!(a + 1, a + 1);
18+
// ok
19+
assert_ne!(a, b);
20+
assert_ne!(a, a + 1);
21+
assert_ne!(a + 1, b + 1);
22+
23+
// lint identical args in `debug_assert_eq!`
24+
debug_assert_eq!(a, a);
25+
debug_assert_eq!(a + 1, a + 1);
26+
// ok
27+
debug_assert_eq!(a, b);
28+
debug_assert_eq!(a, a + 1);
29+
debug_assert_eq!(a + 1, b + 1);
30+
31+
// lint identical args in `debug_assert_ne!`
32+
debug_assert_ne!(a, a);
33+
debug_assert_ne!(a + 1, a + 1);
34+
// ok
35+
debug_assert_ne!(a, b);
36+
debug_assert_ne!(a, a + 1);
37+
debug_assert_ne!(a + 1, b + 1);
1538
}

tests/ui/eq_op_early.stderr

+37-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,41 @@ error: identical args used in this `assert_eq!` macro call
1212
LL | assert_eq!(a + 1, a + 1);
1313
| ^^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: identical args used in this `assert_ne!` macro call
16+
--> $DIR/eq_op_early.rs:16:16
17+
|
18+
LL | assert_ne!(a, a);
19+
| ^^^^
20+
21+
error: identical args used in this `assert_ne!` macro call
22+
--> $DIR/eq_op_early.rs:17:16
23+
|
24+
LL | assert_ne!(a + 1, a + 1);
25+
| ^^^^^^^^^^^^
26+
27+
error: identical args used in this `debug_assert_eq!` macro call
28+
--> $DIR/eq_op_early.rs:24:22
29+
|
30+
LL | debug_assert_eq!(a, a);
31+
| ^^^^
32+
33+
error: identical args used in this `debug_assert_eq!` macro call
34+
--> $DIR/eq_op_early.rs:25:22
35+
|
36+
LL | debug_assert_eq!(a + 1, a + 1);
37+
| ^^^^^^^^^^^^
38+
39+
error: identical args used in this `debug_assert_ne!` macro call
40+
--> $DIR/eq_op_early.rs:32:22
41+
|
42+
LL | debug_assert_ne!(a, a);
43+
| ^^^^
44+
45+
error: identical args used in this `debug_assert_ne!` macro call
46+
--> $DIR/eq_op_early.rs:33:22
47+
|
48+
LL | debug_assert_ne!(a + 1, a + 1);
49+
| ^^^^^^^^^^^^
50+
51+
error: aborting due to 8 previous errors
1652

0 commit comments

Comments
 (0)