Skip to content

Commit 76efd39

Browse files
committed
instead of collecting newly formatted Strings into one String, only create a single String and write!() to it (clippy::format_collect)
1 parent d06ca0f commit 76efd39

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

compiler/rustc_hir_typeck/src/method/prelude2021.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_span::symbol::kw::{Empty, Underscore};
1414
use rustc_span::symbol::{sym, Ident};
1515
use rustc_span::Span;
1616
use rustc_trait_selection::infer::InferCtxtExt;
17+
use std::fmt::Write;
1718

1819
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1920
pub(super) fn lint_dot_call_from_2018(
@@ -143,16 +144,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
143144

144145
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr, sp);
145146
if precise {
146-
let args = args
147-
.iter()
148-
.map(|arg| {
149-
let span = arg.span.find_ancestor_inside(sp).unwrap_or_default();
150-
format!(
151-
", {}",
152-
self.sess().source_map().span_to_snippet(span).unwrap()
153-
)
154-
})
155-
.collect::<String>();
147+
let args = args.iter().fold(String::new(), |mut string, arg| {
148+
let span = arg.span.find_ancestor_inside(sp).unwrap_or_default();
149+
write!(
150+
string,
151+
", {}",
152+
self.sess().source_map().span_to_snippet(span).unwrap()
153+
)
154+
.unwrap();
155+
string
156+
});
156157

157158
lint.span_suggestion(
158159
sp,

compiler/rustc_middle/src/ty/diagnostics.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Diagnostics related methods for `Ty`.
22
33
use std::borrow::Cow;
4+
use std::fmt::Write;
45
use std::ops::ControlFlow;
56

67
use crate::ty::{
@@ -335,10 +336,10 @@ pub fn suggest_constraining_type_params<'a>(
335336
// - insert: `, X: Bar`
336337
suggestions.push((
337338
generics.tail_span_for_predicate_suggestion(),
338-
constraints
339-
.iter()
340-
.map(|&(constraint, _)| format!(", {param_name}: {constraint}"))
341-
.collect::<String>(),
339+
constraints.iter().fold(String::new(), |mut string, &(constraint, _)| {
340+
write!(string, ", {param_name}: {constraint}").unwrap();
341+
string
342+
}),
342343
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
343344
));
344345
continue;

0 commit comments

Comments
 (0)