Skip to content

Commit a68923d

Browse files
committed
rename function, recursively call peel_non_expn_blocks
1 parent 53f57b1 commit a68923d

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

clippy_lints/src/methods/format_collect.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ use rustc_hir::LangItem;
99
use rustc_lint::LateContext;
1010
use rustc_span::Span;
1111

12-
fn tail_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
12+
/// Same as `peel_blocks` but only actually considers blocks that are not from an expansion.
13+
/// This is needed because always calling `peel_blocks` would otherwise remove parts of the
14+
/// `format!` macro, which would cause `root_macro_call_first_node` to return `None`.
15+
fn peel_non_expn_blocks<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
1316
match expr.kind {
14-
ExprKind::Block(block, _) if !expr.span.from_expansion() => block.expr,
17+
ExprKind::Block(block, _) if !expr.span.from_expansion() => peel_non_expn_blocks(block.expr?),
1518
_ => Some(expr),
1619
}
1720
}
@@ -20,7 +23,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, map_arg: &Expr<'_>, m
2023
if is_type_lang_item(cx, cx.typeck_results().expr_ty(expr), LangItem::String)
2124
&& let ExprKind::Closure(closure) = map_arg.kind
2225
&& let body = cx.tcx.hir().body(closure.body)
23-
&& let Some(value) = tail_expr(body.value)
26+
&& let Some(value) = peel_non_expn_blocks(body.value)
2427
&& let Some(mac) = root_macro_call_first_node(cx, value)
2528
&& is_format_macro(cx, mac.def_id)
2629
{

tests/ui/format_collect.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ fn hex_encode(bytes: &[u8]) -> String {
55
bytes.iter().map(|b| format!("{b:02X}")).collect()
66
}
77

8+
#[rustfmt::skip]
9+
fn hex_encode_deep(bytes: &[u8]) -> String {
10+
bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect()
11+
}
12+
813
macro_rules! fmt {
914
($x:ident) => {
1015
format!("{x:02X}", x = $x)

tests/ui/format_collect.stderr

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,25 @@ LL | bytes.iter().map(|b| format!("{b:02X}")).collect()
1818
= note: `-D clippy::format-collect` implied by `-D warnings`
1919

2020
error: use of `format!` to build up a string from an iterator
21-
--> $DIR/format_collect.rs:19:5
21+
--> $DIR/format_collect.rs:10:5
22+
|
23+
LL | bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect()
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
|
26+
help: call `fold` instead
27+
--> $DIR/format_collect.rs:10:18
28+
|
29+
LL | bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect()
30+
| ^^^
31+
help: ... and use the `write!` macro here
32+
--> $DIR/format_collect.rs:10:32
33+
|
34+
LL | bytes.iter().map(|b| {{{{{ format!("{b:02X}") }}}}}).collect()
35+
| ^^^^^^^^^^^^^^^^^^
36+
= note: this can be written more efficiently by appending to a `String` directly
37+
38+
error: use of `format!` to build up a string from an iterator
39+
--> $DIR/format_collect.rs:24:5
2240
|
2341
LL | / (1..10)
2442
LL | | .map(|s| {
@@ -29,16 +47,16 @@ LL | | .collect()
2947
| |__________________^
3048
|
3149
help: call `fold` instead
32-
--> $DIR/format_collect.rs:20:10
50+
--> $DIR/format_collect.rs:25:10
3351
|
3452
LL | .map(|s| {
3553
| ^^^
3654
help: ... and use the `write!` macro here
37-
--> $DIR/format_collect.rs:22:13
55+
--> $DIR/format_collect.rs:27:13
3856
|
3957
LL | format!("{s} {y}")
4058
| ^^^^^^^^^^^^^^^^^^
4159
= note: this can be written more efficiently by appending to a `String` directly
4260

43-
error: aborting due to 2 previous errors
61+
error: aborting due to 3 previous errors
4462

0 commit comments

Comments
 (0)