Skip to content

Commit bbfec7c

Browse files
authored
Rollup merge of #69567 - matthiaskrgr:useless_fmt, r=nagisa
use .to_string() instead of format!() macro to create strings handles what is left after #69541
2 parents 1bb6760 + 85e59b7 commit bbfec7c

File tree

15 files changed

+44
-45
lines changed

15 files changed

+44
-45
lines changed

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::borrow::Cow;
1818

1919
fn describe_as_module(def_id: DefId, tcx: TyCtxt<'_>) -> String {
2020
if def_id.is_top_level_module() {
21-
format!("top-level module")
21+
"top-level module".to_string()
2222
} else {
2323
format!("module `{}`", tcx.def_path_str(def_id))
2424
}

src/librustc_attr/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
5353
err.span_suggestion(
5454
span,
5555
"consider removing the prefix",
56-
format!("{}", &lint_str[1..]),
56+
lint_str[1..].to_string(),
5757
Applicability::MaybeIncorrect,
5858
);
5959
}

src/librustc_errors/diagnostic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ impl Diagnostic {
167167
found: DiagnosticStyledString,
168168
) -> &mut Self {
169169
let mut msg: Vec<_> =
170-
vec![(format!("required when trying to coerce from type `"), Style::NoStyle)];
170+
vec![("required when trying to coerce from type `".to_string(), Style::NoStyle)];
171171
msg.extend(expected.0.iter().map(|x| match *x {
172172
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
173173
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
174174
}));
175-
msg.push((format!("` to type '"), Style::NoStyle));
175+
msg.push(("` to type '".to_string(), Style::NoStyle));
176176
msg.extend(found.0.iter().map(|x| match *x {
177177
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
178178
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
179179
}));
180-
msg.push((format!("`"), Style::NoStyle));
180+
msg.push(("`".to_string(), Style::NoStyle));
181181

182182
// For now, just attach these as notes
183183
self.highlighted_note(msg);

src/librustc_infer/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub(super) fn note_and_explain_region(
143143
// uh oh, hope no user ever sees THIS
144144
ty::ReEmpty(ui) => (format!("the empty lifetime in universe {:?}", ui), None),
145145

146-
ty::RePlaceholder(_) => (format!("any other region"), None),
146+
ty::RePlaceholder(_) => ("any other region".to_string(), None),
147147

148148
// FIXME(#13998) RePlaceholder should probably print like
149149
// ReFree rather than dumping Debug output on the user.

src/librustc_lint/builtin.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1919,21 +1919,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19191919
use rustc::ty::TyKind::*;
19201920
match ty.kind {
19211921
// Primitive types that don't like 0 as a value.
1922-
Ref(..) => Some((format!("references must be non-null"), None)),
1923-
Adt(..) if ty.is_box() => Some((format!("`Box` must be non-null"), None)),
1924-
FnPtr(..) => Some((format!("function pointers must be non-null"), None)),
1925-
Never => Some((format!("the `!` type has no valid value"), None)),
1922+
Ref(..) => Some(("references must be non-null".to_string(), None)),
1923+
Adt(..) if ty.is_box() => Some(("`Box` must be non-null".to_string(), None)),
1924+
FnPtr(..) => Some(("function pointers must be non-null".to_string(), None)),
1925+
Never => Some(("the `!` type has no valid value".to_string(), None)),
19261926
RawPtr(tm) if matches!(tm.ty.kind, Dynamic(..)) =>
19271927
// raw ptr to dyn Trait
19281928
{
1929-
Some((format!("the vtable of a wide raw pointer must be non-null"), None))
1929+
Some(("the vtable of a wide raw pointer must be non-null".to_string(), None))
19301930
}
19311931
// Primitive types with other constraints.
19321932
Bool if init == InitKind::Uninit => {
1933-
Some((format!("booleans must be either `true` or `false`"), None))
1933+
Some(("booleans must be either `true` or `false`".to_string(), None))
19341934
}
19351935
Char if init == InitKind::Uninit => {
1936-
Some((format!("characters must be a valid Unicode codepoint"), None))
1936+
Some(("characters must be a valid Unicode codepoint".to_string(), None))
19371937
}
19381938
// Recurse and checks for some compound types.
19391939
Adt(adt_def, substs) if !adt_def.is_union() => {
@@ -1961,7 +1961,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19611961
}
19621962
// Now, recurse.
19631963
match adt_def.variants.len() {
1964-
0 => Some((format!("enums with no variants have no valid value"), None)),
1964+
0 => Some(("enums with no variants have no valid value".to_string(), None)),
19651965
1 => {
19661966
// Struct, or enum with exactly one variant.
19671967
// Proceed recursively, check all fields.

src/librustc_lint/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ fn lint_overflowing_range_endpoint<'a, 'tcx>(
8383
// We need to preserve the literal's suffix,
8484
// as it may determine typing information.
8585
let suffix = match lit.node {
86-
LitKind::Int(_, LitIntType::Signed(s)) => format!("{}", s.name_str()),
87-
LitKind::Int(_, LitIntType::Unsigned(s)) => format!("{}", s.name_str()),
88-
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(),
86+
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str().to_string(),
87+
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str().to_string(),
88+
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_string(),
8989
_ => bug!(),
9090
};
9191
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);

src/librustc_mir/borrow_check/diagnostics/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,14 @@ pub(super) enum BorrowedContentSource<'tcx> {
619619
impl BorrowedContentSource<'tcx> {
620620
pub(super) fn describe_for_unnamed_place(&self) -> String {
621621
match *self {
622-
BorrowedContentSource::DerefRawPointer => format!("a raw pointer"),
623-
BorrowedContentSource::DerefSharedRef => format!("a shared reference"),
624-
BorrowedContentSource::DerefMutableRef => format!("a mutable reference"),
622+
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
623+
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
624+
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
625625
BorrowedContentSource::OverloadedDeref(ty) => {
626626
if ty.is_rc() {
627-
format!("an `Rc`")
627+
"an `Rc`".to_string()
628628
} else if ty.is_arc() {
629-
format!("an `Arc`")
629+
"an `Arc`".to_string()
630630
} else {
631631
format!("dereference of `{}`", ty)
632632
}
@@ -649,16 +649,16 @@ impl BorrowedContentSource<'tcx> {
649649

650650
pub(super) fn describe_for_immutable_place(&self) -> String {
651651
match *self {
652-
BorrowedContentSource::DerefRawPointer => format!("a `*const` pointer"),
653-
BorrowedContentSource::DerefSharedRef => format!("a `&` reference"),
652+
BorrowedContentSource::DerefRawPointer => "a `*const` pointer".to_string(),
653+
BorrowedContentSource::DerefSharedRef => "a `&` reference".to_string(),
654654
BorrowedContentSource::DerefMutableRef => {
655655
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
656656
}
657657
BorrowedContentSource::OverloadedDeref(ty) => {
658658
if ty.is_rc() {
659-
format!("an `Rc`")
659+
"an `Rc`".to_string()
660660
} else if ty.is_arc() {
661-
format!("an `Arc`")
661+
"an `Arc`".to_string()
662662
} else {
663663
format!("a dereference of `{}`", ty)
664664
}

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
443443
let place_ty = move_from.ty(*self.body, self.infcx.tcx).ty;
444444
let place_desc = match self.describe_place(move_from.as_ref()) {
445445
Some(desc) => format!("`{}`", desc),
446-
None => format!("value"),
446+
None => "value".to_string(),
447447
};
448448

449449
self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span));
@@ -466,7 +466,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
466466
let place_ty = original_path.ty(*self.body, self.infcx.tcx).ty;
467467
let place_desc = match self.describe_place(original_path.as_ref()) {
468468
Some(desc) => format!("`{}`", desc),
469-
None => format!("value"),
469+
None => "value".to_string(),
470470
};
471471
self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span));
472472

src/librustc_mir/util/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ fn write_scope_tree(
477477
indented_decl.push_str(";");
478478

479479
let local_name =
480-
if local == RETURN_PLACE { format!(" return place") } else { String::new() };
480+
if local == RETURN_PLACE { " return place".to_string() } else { String::new() };
481481

482482
writeln!(
483483
w,

src/librustc_parse/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl<'a> Parser<'a> {
216216
.span_suggestion(
217217
seq_span,
218218
"...or a vertical bar to match on multiple alternatives",
219-
format!("{}", seq_snippet.replace(",", " |")),
219+
seq_snippet.replace(",", " |"),
220220
Applicability::MachineApplicable,
221221
);
222222
}

src/librustc_passes/check_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl NonConstExpr {
3535
match self {
3636
Self::Loop(src) => format!("`{}`", src.name()),
3737
Self::Match(src) => format!("`{}`", src.name()),
38-
Self::OrPattern => format!("or-pattern"),
38+
Self::OrPattern => "or-pattern".to_string(),
3939
}
4040
}
4141

src/librustc_resolve/diagnostics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'a> Resolver<'a> {
9898
E0401,
9999
"can't use generic parameters from outer function",
100100
);
101-
err.span_label(span, format!("use of generic parameter from outer function"));
101+
err.span_label(span, "use of generic parameter from outer function".to_string());
102102

103103
let sm = self.session.source_map();
104104
match outer_res {
@@ -155,7 +155,8 @@ impl<'a> Resolver<'a> {
155155
} else if let Some(sp) = sm.generate_fn_name_span(span) {
156156
err.span_label(
157157
sp,
158-
format!("try adding a local generic parameter in this method instead"),
158+
"try adding a local generic parameter in this method instead"
159+
.to_string(),
159160
);
160161
} else {
161162
err.help("try using a local generic parameter instead");

src/librustc_resolve/late/diagnostics.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
177177
err.code(rustc_errors::error_code!(E0411));
178178
err.span_label(
179179
span,
180-
format!("`Self` is only available in impls, traits, and type definitions"),
180+
"`Self` is only available in impls, traits, and type definitions".to_string(),
181181
);
182182
return (err, Vec::new());
183183
}
@@ -186,12 +186,10 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
186186

187187
err.code(rustc_errors::error_code!(E0424));
188188
err.span_label(span, match source {
189-
PathSource::Pat => format!(
190-
"`self` value is a keyword and may not be bound to variables or shadowed",
191-
),
192-
_ => format!(
193-
"`self` value is a keyword only available in methods with a `self` parameter",
194-
),
189+
PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed"
190+
.to_string(),
191+
_ => "`self` value is a keyword only available in methods with a `self` parameter"
192+
.to_string(),
195193
});
196194
if let Some(span) = &self.diagnostic_metadata.current_function {
197195
err.span_label(*span, "this function doesn't have a `self` parameter");
@@ -558,7 +556,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
558556
if is_expected(ctor_def) && !accessible_ctor {
559557
err.span_label(
560558
span,
561-
format!("constructor is not visible here due to private fields"),
559+
"constructor is not visible here due to private fields".to_string(),
562560
);
563561
}
564562
} else {

src/librustc_typeck/check/op.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
605605
if lstring.starts_with('&') {
606606
// let a = String::new();
607607
// let _ = &a + "bar";
608-
format!("{}", &lstring[1..])
608+
lstring[1..].to_string()
609609
} else {
610610
format!("{}.to_owned()", lstring)
611611
},
@@ -633,7 +633,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
633633
let to_string = if l.starts_with('&') {
634634
// let a = String::new(); let b = String::new();
635635
// let _ = &a + b;
636-
format!("{}", &l[1..])
636+
l[1..].to_string()
637637
} else {
638638
format!("{}.to_owned()", l)
639639
};

src/librustc_typeck/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ crate fn placeholder_type_error(
160160
}) {
161161
// Account for `_` already present in cases like `struct S<_>(_);` and suggest
162162
// `struct S<T>(T);` instead of `struct S<_, T>(T);`.
163-
sugg.push((arg.span, format!("{}", type_name)));
163+
sugg.push((arg.span, type_name.to_string()));
164164
} else {
165165
sugg.push((
166166
generics.iter().last().unwrap().span.shrink_to_hi(),
@@ -475,7 +475,7 @@ fn get_new_lifetime_name<'tcx>(
475475

476476
let a_to_z_repeat_n = |n| {
477477
(b'a'..=b'z').map(move |c| {
478-
let mut s = format!("'");
478+
let mut s = '\''.to_string();
479479
s.extend(std::iter::repeat(char::from(c)).take(n));
480480
s
481481
})

0 commit comments

Comments
 (0)