Extend to the assert macro family

This commit is contained in:
ThibsG 2020-10-13 09:31:53 +02:00
parent 32fdb8fb0c
commit a3e0446afe
3 changed files with 75 additions and 5 deletions

View file

@ -7,6 +7,7 @@ use rustc_ast::{ast, token};
use rustc_errors::Applicability;
use rustc_hir::{BinOp, BinOpKind, BorrowKind, Expr, ExprKind};
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_parse::parser;
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -64,10 +65,20 @@ declare_lint_pass!(EqOp => [EQ_OP, OP_REF]);
impl EarlyLintPass for EqOp {
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
let macro_list = [
sym!(assert_eq),
sym!(assert_ne),
sym!(debug_assert_eq),
sym!(debug_assert_ne),
];
if_chain! {
if mac.path == sym!(assert_eq);
if !in_external_macro(cx.sess, mac.span());
if mac.path.segments.len() == 1;
let macro_name = mac.path.segments[0].ident.name;
if macro_list.contains(&macro_name);
let tokens = mac.args.inner_tokens();
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tokens, false, None);
let mut parser = parser::Parser::new(
&cx.sess.parse_sess, tokens, false, None);
if let Ok(left) = parser.parse_expr();
if parser.eat(&token::Comma);
if let Ok(right) = parser.parse_expr();
@ -80,7 +91,7 @@ impl EarlyLintPass for EqOp {
cx,
EQ_OP,
left_expr.span.to(right_expr.span),
"identical args used in this `assert_eq!` macro call",
&format!("identical args used in this `{}!` macro call", macro_name),
);
}
}

View file

@ -7,9 +7,32 @@ fn main() {
// lint identical args in `assert_eq!` (see #3574)
assert_eq!(a, a);
assert_eq!(a + 1, a + 1);
// ok
assert_eq!(a, b);
assert_eq!(a, a + 1);
assert_eq!(a + 1, b + 1);
// lint identical args in `assert_ne!`
assert_ne!(a, a);
assert_ne!(a + 1, a + 1);
// ok
assert_ne!(a, b);
assert_ne!(a, a + 1);
assert_ne!(a + 1, b + 1);
// lint identical args in `debug_assert_eq!`
debug_assert_eq!(a, a);
debug_assert_eq!(a + 1, a + 1);
// ok
debug_assert_eq!(a, b);
debug_assert_eq!(a, a + 1);
debug_assert_eq!(a + 1, b + 1);
// lint identical args in `debug_assert_ne!`
debug_assert_ne!(a, a);
debug_assert_ne!(a + 1, a + 1);
// ok
debug_assert_ne!(a, b);
debug_assert_ne!(a, a + 1);
debug_assert_ne!(a + 1, b + 1);
}

View file

@ -12,5 +12,41 @@ error: identical args used in this `assert_eq!` macro call
LL | assert_eq!(a + 1, a + 1);
| ^^^^^^^^^^^^
error: aborting due to 2 previous errors
error: identical args used in this `assert_ne!` macro call
--> $DIR/eq_op_early.rs:16:16
|
LL | assert_ne!(a, a);
| ^^^^
error: identical args used in this `assert_ne!` macro call
--> $DIR/eq_op_early.rs:17:16
|
LL | assert_ne!(a + 1, a + 1);
| ^^^^^^^^^^^^
error: identical args used in this `debug_assert_eq!` macro call
--> $DIR/eq_op_early.rs:24:22
|
LL | debug_assert_eq!(a, a);
| ^^^^
error: identical args used in this `debug_assert_eq!` macro call
--> $DIR/eq_op_early.rs:25:22
|
LL | debug_assert_eq!(a + 1, a + 1);
| ^^^^^^^^^^^^
error: identical args used in this `debug_assert_ne!` macro call
--> $DIR/eq_op_early.rs:32:22
|
LL | debug_assert_ne!(a, a);
| ^^^^
error: identical args used in this `debug_assert_ne!` macro call
--> $DIR/eq_op_early.rs:33:22
|
LL | debug_assert_ne!(a + 1, a + 1);
| ^^^^^^^^^^^^
error: aborting due to 8 previous errors