Skip to content

Commit c83d58f

Browse files
committed
document that write!ing into a string never fails
1 parent c388156 commit c83d58f

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

clippy_lints/src/methods/format_collect.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use super::FORMAT_COLLECT;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::macros::is_format_macro;
4-
use clippy_utils::macros::root_macro_call_first_node;
3+
use clippy_utils::macros::{is_format_macro, root_macro_call_first_node};
54
use clippy_utils::ty::is_type_lang_item;
6-
use rustc_hir::Expr;
7-
use rustc_hir::ExprKind;
8-
use rustc_hir::LangItem;
5+
use rustc_hir::{Expr, ExprKind, LangItem};
96
use rustc_lint::LateContext;
107
use rustc_span::Span;
118

12-
fn tail_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
9+
/// Same as `peel_blocks` but only actually considers blocks that are not from an expansion.
10+
/// This is needed because always calling `peel_blocks` would otherwise remove parts of the
11+
/// `format!` macro, which would cause `root_macro_call_first_node` to return `None`.
12+
fn peel_non_expn_blocks<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
1313
match expr.kind {
14-
ExprKind::Block(block, _) if !expr.span.from_expansion() => block.expr,
14+
ExprKind::Block(block, _) if !expr.span.from_expansion() => peel_non_expn_blocks(block.expr?),
1515
_ => Some(expr),
1616
}
1717
}
@@ -20,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, map_arg: &Expr<'_>, m
2020
if is_type_lang_item(cx, cx.typeck_results().expr_ty(expr), LangItem::String)
2121
&& let ExprKind::Closure(closure) = map_arg.kind
2222
&& let body = cx.tcx.hir().body(closure.body)
23-
&& let Some(value) = tail_expr(body.value)
23+
&& let Some(value) = peel_non_expn_blocks(body.value)
2424
&& let Some(mac) = root_macro_call_first_node(cx, value)
2525
&& is_format_macro(cx, mac.def_id)
2626
{

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3385,7 +3385,12 @@ declare_clippy_lint! {
33853385
///
33863386
/// ### Why is this bad?
33873387
/// This allocates a new string for every element in the iterator.
3388-
/// This can be done more efficiently by creating the `String` once and appending to it using `Iterator::fold`.
3388+
/// This can be done more efficiently by creating the `String` once and appending to it in `Iterator::fold`,
3389+
/// using either the `write!` macro which supports exactly the same syntax as the `format!` macro,
3390+
/// or concatenating with `+` in case the iterator yields `&str`/`String`.
3391+
///
3392+
/// Note also that `write!`-ing into a `String` can never fail, despite the return type of `write!` being `std::fmt::Result`,
3393+
/// so it can be safely ignored or unwrapped.
33893394
///
33903395
/// ### Example
33913396
/// ```rust

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)