Skip to content

Commit

Permalink
add extraction of fmt parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ABouttefeux committed Sep 8, 2021
1 parent b004b62 commit f244ec7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
34 changes: 32 additions & 2 deletions clippy_utils/src/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,42 @@ pub fn extract_assert_macro_args<'tcx>(e: &'tcx Expr<'tcx>) -> Option<Vec<&'tcx
/// compared
fn ast_matchblock(matchblock_expr: &'tcx Expr<'tcx>) -> Option<Vec<&Expr<'_>>> {
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
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/bool_assert_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ 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);
debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
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");
}
32 changes: 22 additions & 10 deletions tests/ui/bool_assert_comparison.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f244ec7

Please sign in to comment.