Skip to content

Commit f244ec7

Browse files
committed
add extraction of fmt parameter
1 parent b004b62 commit f244ec7

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

clippy_utils/src/higher.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,42 @@ pub fn extract_assert_macro_args<'tcx>(e: &'tcx Expr<'tcx>) -> Option<Vec<&'tcx
428428
/// compared
429429
fn ast_matchblock(matchblock_expr: &'tcx Expr<'tcx>) -> Option<Vec<&Expr<'_>>> {
430430
if_chain! {
431-
if let ExprKind::Match(headerexpr, _, _) = &matchblock_expr.kind;
431+
if let ExprKind::Match(headerexpr, arms, _) = &matchblock_expr.kind;
432432
if let ExprKind::Tup([lhs, rhs]) = &headerexpr.kind;
433433
if let ExprKind::AddrOf(BorrowKind::Ref, _, lhs) = lhs.kind;
434434
if let ExprKind::AddrOf(BorrowKind::Ref, _, rhs) = rhs.kind;
435435
then {
436-
return Some(vec![lhs, rhs]);
436+
let mut vec_arg = vec![lhs, rhs];
437+
if_chain! {
438+
if !arms.is_empty();
439+
if let ExprKind::Block(Block{expr: Some(if_expr),..},_) = arms[0].body.kind;
440+
if let ExprKind::If(_, if_block, _) = if_expr.kind;
441+
if let ExprKind::Block(Block{stmts: stmts_if_block,..},_) = if_block.kind;
442+
if stmts_if_block.len() >= 2;
443+
if let StmtKind::Expr(call_assert_failed)
444+
| StmtKind::Semi(call_assert_failed) = stmts_if_block[1].kind;
445+
if let ExprKind::Call(_, args_assert_failed) = call_assert_failed.kind;
446+
if args_assert_failed.len() >= 4;
447+
if let ExprKind::Call(_, args) = args_assert_failed[3].kind;
448+
if !args.is_empty();
449+
if let ExprKind::Call(_, args_fmt) = args[0].kind;
450+
if !args_fmt.is_empty();
451+
then {
452+
vec_arg.push(&args_fmt[0]);
453+
if_chain! {
454+
if args_fmt.len() >= 2;
455+
if let ExprKind::AddrOf(_, _, expr_match) = args_fmt[1].kind;
456+
if let ExprKind::Match(tup_match, _, _) = expr_match.kind;
457+
if let ExprKind::Tup(tup_args_list) = tup_match.kind;
458+
then{
459+
for arg in tup_args_list {
460+
vec_arg.push(arg);
461+
}
462+
}
463+
}
464+
}
465+
}
466+
return Some(vec_arg);
437467
}
438468
}
439469
None

tests/ui/bool_assert_comparison.rs

+2
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ fn main() {
112112
assert_eq!("a".is_empty(), false, "tadam {}", true);
113113
assert_eq!(false, "a".is_empty(), "tadam {}", true);
114114
assert_eq!(a, true, "tadam {}", false);
115+
assert_eq!("a".is_empty(), true, "tadam {} {}", false, 6);
115116

116117
debug_assert_eq!("a".len(), 1, "tadam {}", 1);
117118
debug_assert_eq!("a".len(), 1, "tadam {}", true);
118119
debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
119120
debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
120121
debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
121122
debug_assert_eq!(a, true, "tadam {}", false);
123+
debug_assert_eq!("a".is_empty(), true, "tadam {} {}", false, "b");
122124
}

tests/ui/bool_assert_comparison.stderr

+22-10
Original file line numberDiff line numberDiff line change
@@ -100,37 +100,49 @@ error: used `assert_eq!` with a literal bool
100100
--> $DIR/bool_assert_comparison.rs:111:5
101101
|
102102
LL | assert_eq!("a".is_empty(), false, "tadam {}", 1);
103-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
103+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty(), "tadam {}", 1)`
104104

105105
error: used `assert_eq!` with a literal bool
106106
--> $DIR/bool_assert_comparison.rs:112:5
107107
|
108108
LL | assert_eq!("a".is_empty(), false, "tadam {}", true);
109-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
109+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty(), "tadam {}", true)`
110110

111111
error: used `assert_eq!` with a literal bool
112112
--> $DIR/bool_assert_comparison.rs:113:5
113113
|
114114
LL | assert_eq!(false, "a".is_empty(), "tadam {}", true);
115-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty())`
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(!"a".is_empty(), "tadam {}", true)`
116+
117+
error: used `assert_eq!` with a literal bool
118+
--> $DIR/bool_assert_comparison.rs:115:5
119+
|
120+
LL | assert_eq!("a".is_empty(), true, "tadam {} {}", false, 6);
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!("a".is_empty(), "tadam {} {}", false, 6)`
116122

117123
error: used `debug_assert_eq!` with a literal bool
118-
--> $DIR/bool_assert_comparison.rs:118:5
124+
--> $DIR/bool_assert_comparison.rs:119:5
119125
|
120126
LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
121-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty(), "tadam {}", 1)`
122128

123129
error: used `debug_assert_eq!` with a literal bool
124-
--> $DIR/bool_assert_comparison.rs:119:5
130+
--> $DIR/bool_assert_comparison.rs:120:5
125131
|
126132
LL | debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
127-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
133+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty(), "tadam {}", true)`
128134

129135
error: used `debug_assert_eq!` with a literal bool
130-
--> $DIR/bool_assert_comparison.rs:120:5
136+
--> $DIR/bool_assert_comparison.rs:121:5
131137
|
132138
LL | debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
133-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty())`
139+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!(!"a".is_empty(), "tadam {}", true)`
140+
141+
error: used `debug_assert_eq!` with a literal bool
142+
--> $DIR/bool_assert_comparison.rs:123:5
143+
|
144+
LL | debug_assert_eq!("a".is_empty(), true, "tadam {} {}", false, "b");
145+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `debug_assert!("a".is_empty(), "tadam {} {}", false, "b")`
134146

135-
error: aborting due to 22 previous errors
147+
error: aborting due to 24 previous errors
136148

0 commit comments

Comments
 (0)