diff --git a/clippy_utils/src/higher.rs b/clippy_utils/src/higher.rs index 05a4a0143195..c2677db9ea4c 100644 --- a/clippy_utils/src/higher.rs +++ b/clippy_utils/src/higher.rs @@ -428,12 +428,42 @@ pub fn extract_assert_macro_args<'tcx>(e: &'tcx Expr<'tcx>) -> Option) -> Option>> { if_chain! { - if let ExprKind::Match(headerexpr, _, _) = &matchblock_expr.kind; + if let ExprKind::Match(headerexpr, arms, _) = &matchblock_expr.kind; if let ExprKind::Tup([lhs, rhs]) = &headerexpr.kind; if let ExprKind::AddrOf(BorrowKind::Ref, _, lhs) = lhs.kind; if let ExprKind::AddrOf(BorrowKind::Ref, _, rhs) = rhs.kind; then { - return Some(vec![lhs, rhs]); + let mut vec_arg = vec![lhs, rhs]; + if_chain! { + if !arms.is_empty(); + if let ExprKind::Block(Block{expr: Some(if_expr),..},_) = arms[0].body.kind; + if let ExprKind::If(_, if_block, _) = if_expr.kind; + if let ExprKind::Block(Block{stmts: stmts_if_block,..},_) = if_block.kind; + if stmts_if_block.len() >= 2; + if let StmtKind::Expr(call_assert_failed) + | StmtKind::Semi(call_assert_failed) = stmts_if_block[1].kind; + if let ExprKind::Call(_, args_assert_failed) = call_assert_failed.kind; + if args_assert_failed.len() >= 4; + if let ExprKind::Call(_, args) = args_assert_failed[3].kind; + if !args.is_empty(); + if let ExprKind::Call(_, args_fmt) = args[0].kind; + if !args_fmt.is_empty(); + then { + vec_arg.push(&args_fmt[0]); + if_chain! { + if args_fmt.len() >= 2; + if let ExprKind::AddrOf(_, _, expr_match) = args_fmt[1].kind; + if let ExprKind::Match(tup_match, _, _) = expr_match.kind; + if let ExprKind::Tup(tup_args_list) = tup_match.kind; + then{ + for arg in tup_args_list { + vec_arg.push(arg); + } + } + } + } + } + return Some(vec_arg); } } None diff --git a/tests/ui/bool_assert_comparison.rs b/tests/ui/bool_assert_comparison.rs index ec4d6f3ff840..0d0f27874eec 100644 --- a/tests/ui/bool_assert_comparison.rs +++ b/tests/ui/bool_assert_comparison.rs @@ -112,6 +112,7 @@ fn main() { assert_eq!("a".is_empty(), false, "tadam {}", true); assert_eq!(false, "a".is_empty(), "tadam {}", true); assert_eq!(a, true, "tadam {}", false); + assert_eq!("a".is_empty(), true, "tadam {} {}", false, 6); debug_assert_eq!("a".len(), 1, "tadam {}", 1); debug_assert_eq!("a".len(), 1, "tadam {}", true); @@ -119,4 +120,5 @@ fn main() { debug_assert_eq!("a".is_empty(), false, "tadam {}", true); debug_assert_eq!(false, "a".is_empty(), "tadam {}", true); debug_assert_eq!(a, true, "tadam {}", false); + debug_assert_eq!("a".is_empty(), true, "tadam {} {}", false, "b"); } diff --git a/tests/ui/bool_assert_comparison.stderr b/tests/ui/bool_assert_comparison.stderr index 3005f906509e..8e6574cb9120 100644 --- a/tests/ui/bool_assert_comparison.stderr +++ b/tests/ui/bool_assert_comparison.stderr @@ -100,37 +100,49 @@ error: used `assert_eq!` with a literal bool --> $DIR/bool_assert_comparison.rs:111:5 | LL | assert_eq!("a".is_empty(), false, "tadam {}", 1); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty(), "tadam {}", 1)` error: used `assert_eq!` with a literal bool --> $DIR/bool_assert_comparison.rs:112:5 | LL | assert_eq!("a".is_empty(), false, "tadam {}", true); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty(), "tadam {}", true)` error: used `assert_eq!` with a literal bool --> $DIR/bool_assert_comparison.rs:113:5 | LL | assert_eq!(false, "a".is_empty(), "tadam {}", true); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty(), "tadam {}", true)` + +error: used `assert_eq!` with a literal bool + --> $DIR/bool_assert_comparison.rs:115:5 + | +LL | assert_eq!("a".is_empty(), true, "tadam {} {}", false, 6); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!("a".is_empty(), "tadam {} {}", false, 6)` error: used `debug_assert_eq!` with a literal bool - --> $DIR/bool_assert_comparison.rs:118:5 + --> $DIR/bool_assert_comparison.rs:119:5 | LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", 1); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty(), "tadam {}", 1)` error: used `debug_assert_eq!` with a literal bool - --> $DIR/bool_assert_comparison.rs:119:5 + --> $DIR/bool_assert_comparison.rs:120:5 | LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", true); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty(), "tadam {}", true)` error: used `debug_assert_eq!` with a literal bool - --> $DIR/bool_assert_comparison.rs:120:5 + --> $DIR/bool_assert_comparison.rs:121:5 | LL | debug_assert_eq!(false, "a".is_empty(), "tadam {}", true); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty(), "tadam {}", true)` + +error: used `debug_assert_eq!` with a literal bool + --> $DIR/bool_assert_comparison.rs:123:5 + | +LL | debug_assert_eq!("a".is_empty(), true, "tadam {} {}", false, "b"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!("a".is_empty(), "tadam {} {}", false, "b")` -error: aborting due to 22 previous errors +error: aborting due to 24 previous errors