Skip to content

Commit e44bb26

Browse files
committed
add extraction of fmt parameter
1 parent 7703887 commit e44bb26

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

clippy_utils/src/higher.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{is_expn_of, match_def_path, paths};
77
use if_chain::if_chain;
88
use rustc_ast::ast::{self, LitKind};
99
use rustc_hir as hir;
10-
use rustc_hir::{BorrowKind, Expr, ExprKind, StmtKind, UnOp};
10+
use rustc_hir::{Block, BorrowKind, Expr, ExprKind, StmtKind, UnOp};
1111
use rustc_lint::LateContext;
1212
use rustc_span::{sym, ExpnKind, Span, Symbol};
1313

@@ -202,12 +202,42 @@ pub fn extract_assert_macro_args<'tcx>(e: &'tcx Expr<'tcx>) -> Option<Vec<&'tcx
202202
/// compared
203203
fn ast_matchblock(matchblock_expr: &'tcx Expr<'tcx>) -> Option<Vec<&Expr<'_>>> {
204204
if_chain! {
205-
if let ExprKind::Match(headerexpr, _, _) = &matchblock_expr.kind;
205+
if let ExprKind::Match(headerexpr, arms, _) = &matchblock_expr.kind;
206206
if let ExprKind::Tup([lhs, rhs]) = &headerexpr.kind;
207207
if let ExprKind::AddrOf(BorrowKind::Ref, _, lhs) = lhs.kind;
208208
if let ExprKind::AddrOf(BorrowKind::Ref, _, rhs) = rhs.kind;
209209
then {
210-
return Some(vec![lhs, rhs]);
210+
let mut vec_arg = vec![lhs, rhs];
211+
if_chain! {
212+
if !arms.is_empty();
213+
if let ExprKind::Block(Block{expr: Some(if_expr),..},_) = arms[0].body.kind;
214+
if let ExprKind::If(_, if_block, _) = if_expr.kind;
215+
if let ExprKind::Block(Block{stmts: stmts_if_block,..},_) = if_block.kind;
216+
if stmts_if_block.len() >= 2;
217+
if let StmtKind::Expr(call_assert_failed)
218+
| StmtKind::Semi(call_assert_failed) = stmts_if_block[1].kind;
219+
if let ExprKind::Call(_, args_assert_failed) = call_assert_failed.kind;
220+
if args_assert_failed.len() >= 4;
221+
if let ExprKind::Call(_, args) = args_assert_failed[3].kind;
222+
if !args.is_empty();
223+
if let ExprKind::Call(_, args_fmt) = args[0].kind;
224+
if !args_fmt.is_empty();
225+
then {
226+
vec_arg.push(&args_fmt[0]);
227+
if_chain! {
228+
if args_fmt.len() >= 2;
229+
if let ExprKind::AddrOf(_, _, expr_match) = args_fmt[1].kind;
230+
if let ExprKind::Match(tup_match, _, _) = expr_match.kind;
231+
if let ExprKind::Tup(tup_args_list) = tup_match.kind;
232+
then{
233+
for arg in tup_args_list {
234+
vec_arg.push(arg);
235+
}
236+
}
237+
}
238+
}
239+
}
240+
return Some(vec_arg);
211241
}
212242
}
213243
None

tests/ui/bool_assert_comparison.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 22 additions & 10 deletions
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)