Skip to content

Commit 7bbcd00

Browse files
committed
Auto merge of #52805 - ljedrz:format_str_literal, r=petrochenkov
Don't format!() string literals Prefer `to_string()` to `format!()` take 2, this time targetting string literals. In some cases (`&format!("...")` -> `"..."`) also removes allocations. Occurences of `format!("")` are changed to `String::new()`.
2 parents a3f519d + 421b2ba commit 7bbcd00

File tree

42 files changed

+168
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+168
-172
lines changed

src/bootstrap/dist.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Step for Docs {
9494

9595
builder.info(&format!("Dist docs ({})", host));
9696
if !builder.config.docs {
97-
builder.info(&format!("\tskipping - docs disabled"));
97+
builder.info("\tskipping - docs disabled");
9898
return distdir(builder).join(format!("{}-{}.tar.gz", name, host));
9999
}
100100

@@ -156,7 +156,7 @@ impl Step for RustcDocs {
156156

157157
builder.info(&format!("Dist compiler docs ({})", host));
158158
if !builder.config.compiler_docs {
159-
builder.info(&format!("\tskipping - compiler docs disabled"));
159+
builder.info("\tskipping - compiler docs disabled");
160160
return distdir(builder).join(format!("{}-{}.tar.gz", name, host));
161161
}
162162

@@ -639,7 +639,7 @@ impl Step for Std {
639639
// The only true set of target libraries came from the build triple, so
640640
// let's reduce redundant work by only producing archives from that host.
641641
if compiler.host != builder.config.build {
642-
builder.info(&format!("\tskipping, not a build host"));
642+
builder.info("\tskipping, not a build host");
643643
return distdir(builder).join(format!("{}-{}.tar.gz", name, target));
644644
}
645645

@@ -715,11 +715,11 @@ impl Step for Analysis {
715715
let compiler = self.compiler;
716716
let target = self.target;
717717
assert!(builder.config.extended);
718-
builder.info(&format!("Dist analysis"));
718+
builder.info("Dist analysis");
719719
let name = pkgname(builder, "rust-analysis");
720720

721721
if &compiler.host != builder.config.build {
722-
builder.info(&format!("\tskipping, not a build host"));
722+
builder.info("\tskipping, not a build host");
723723
return distdir(builder).join(format!("{}-{}.tar.gz", name, target));
724724
}
725725

@@ -824,7 +824,7 @@ impl Step for Src {
824824

825825
/// Creates the `rust-src` installer component
826826
fn run(self, builder: &Builder) -> PathBuf {
827-
builder.info(&format!("Dist src"));
827+
builder.info("Dist src");
828828

829829
let name = pkgname(builder, "rust-src");
830830
let image = tmpdir(builder).join(format!("{}-image", name));
@@ -918,7 +918,7 @@ impl Step for PlainSourceTarball {
918918

919919
/// Creates the plain source tarball
920920
fn run(self, builder: &Builder) -> PathBuf {
921-
builder.info(&format!("Create plain source tarball"));
921+
builder.info("Create plain source tarball");
922922

923923
// Make sure that the root folder of tarball has the correct name
924924
let plain_name = format!("{}-src", pkgname(builder, "rustc"));
@@ -998,7 +998,7 @@ impl Step for PlainSourceTarball {
998998
if let Some(dir) = tarball.parent() {
999999
builder.create_dir(&dir);
10001000
}
1001-
builder.info(&format!("running installer"));
1001+
builder.info("running installer");
10021002
let mut cmd = rust_installer(builder);
10031003
cmd.arg("tarball")
10041004
.arg("--input").arg(&plain_name)

src/bootstrap/doc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ impl Step for Rustc {
686686
};
687687

688688
if !builder.config.compiler_docs {
689-
builder.info(&format!("\tskipping - compiler/librustdoc docs disabled"));
689+
builder.info("\tskipping - compiler/librustdoc docs disabled");
690690
return;
691691
}
692692

@@ -788,7 +788,7 @@ impl Step for Rustdoc {
788788
};
789789

790790
if !builder.config.compiler_docs {
791-
builder.info(&format!("\tskipping - compiler/librustdoc docs disabled"));
791+
builder.info("\tskipping - compiler/librustdoc docs disabled");
792792
return;
793793
}
794794

src/bootstrap/native.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl Step for TestHelpers {
474474
}
475475

476476
let _folder = builder.fold_output(|| "build_test_helpers");
477-
builder.info(&format!("Building test helpers"));
477+
builder.info("Building test helpers");
478478
t!(fs::create_dir_all(&dst));
479479
let mut cfg = cc::Build::new();
480480

src/librustc/hir/check_attr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
248248
self.emit_repr_error(
249249
attr.span,
250250
stmt.span,
251-
&format!("attribute should not be applied to a statement"),
252-
&format!("not a struct, enum or union"),
251+
"attribute should not be applied to a statement",
252+
"not a struct, enum or union",
253253
);
254254
}
255255
}
@@ -269,8 +269,8 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
269269
self.emit_repr_error(
270270
attr.span,
271271
expr.span,
272-
&format!("attribute should not be applied to an expression"),
273-
&format!("not defining a struct, enum or union"),
272+
"attribute should not be applied to an expression",
273+
"not defining a struct, enum or union",
274274
);
275275
}
276276
}

src/librustc/infer/error_reporting/need_type_info.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
137137
// ^ consider giving this closure parameter a type
138138
// ```
139139
labels.clear();
140-
labels.push((pattern.span, format!("consider giving this closure parameter a type")));
140+
labels.push(
141+
(pattern.span, "consider giving this closure parameter a type".to_string()));
141142
} else if let Some(pattern) = local_visitor.found_local_pattern {
142143
if let Some(simple_ident) = pattern.simple_ident() {
143144
match pattern.span.compiler_desugaring_kind() {
@@ -150,7 +151,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
150151
_ => {}
151152
}
152153
} else {
153-
labels.push((pattern.span, format!("consider giving the pattern a type")));
154+
labels.push((pattern.span, "consider giving the pattern a type".to_string()));
154155
}
155156
}
156157

src/librustc/infer/error_reporting/nice_region_error/different_lifetimes.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,26 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
9999
let span_label_var1 = if let Some(simple_ident) = anon_arg_sup.pat.simple_ident() {
100100
format!(" from `{}`", simple_ident)
101101
} else {
102-
format!("")
102+
String::new()
103103
};
104104

105105
let span_label_var2 = if let Some(simple_ident) = anon_arg_sub.pat.simple_ident() {
106106
format!(" into `{}`", simple_ident)
107107
} else {
108-
format!("")
108+
String::new()
109109
};
110110

111111

112112
let (span_1, span_2, main_label, span_label) = match (sup_is_ret_type, sub_is_ret_type) {
113113
(None, None) => {
114114
let (main_label_1, span_label_1) = if ty_sup.id == ty_sub.id {
115115
(
116-
format!("this type is declared with multiple lifetimes..."),
117-
format!(
118-
"...but data{} flows{} here",
119-
format!(" with one lifetime"),
120-
format!(" into the other")
121-
),
116+
"this type is declared with multiple lifetimes...".to_string(),
117+
"...but data with one lifetime flows into the other here".to_string()
122118
)
123119
} else {
124120
(
125-
format!("these two types are declared with different lifetimes..."),
121+
"these two types are declared with different lifetimes...".to_string(),
126122
format!(
127123
"...but data{} flows{} here",
128124
span_label_var1,
@@ -136,27 +132,25 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
136132
(Some(ret_span), _) => (
137133
ty_sub.span,
138134
ret_span,
139-
format!(
140-
"this parameter and the return type are declared \
141-
with different lifetimes...",
142-
),
135+
"this parameter and the return type are declared \
136+
with different lifetimes...".to_string()
137+
,
143138
format!("...but data{} is returned here", span_label_var1),
144139
),
145140
(_, Some(ret_span)) => (
146141
ty_sup.span,
147142
ret_span,
148-
format!(
149-
"this parameter and the return type are declared \
150-
with different lifetimes...",
151-
),
143+
"this parameter and the return type are declared \
144+
with different lifetimes...".to_string()
145+
,
152146
format!("...but data{} is returned here", span_label_var1),
153147
),
154148
};
155149

156150

157151
struct_span_err!(self.tcx.sess, span, E0623, "lifetime mismatch")
158152
.span_label(span_1, main_label)
159-
.span_label(span_2, format!(""))
153+
.span_label(span_2, String::new())
160154
.span_label(span, span_label)
161155
.emit();
162156
return Some(ErrorReported);

src/librustc/infer/lexical_region_resolve/graphviz.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'a, 'gcx, 'tcx> dot::Labeller<'a> for ConstraintGraph<'a, 'gcx, 'tcx> {
204204
match *e {
205205
Edge::Constraint(ref c) =>
206206
dot::LabelText::label(format!("{:?}", self.map.get(c).unwrap())),
207-
Edge::EnclScope(..) => dot::LabelText::label(format!("(enclosed)")),
207+
Edge::EnclScope(..) => dot::LabelText::label("(enclosed)".to_string()),
208208
}
209209
}
210210
}
@@ -273,7 +273,7 @@ fn dump_region_data_to<'a, 'gcx, 'tcx>(region_rels: &RegionRelations<'a, 'gcx, '
273273
debug!("dump_region_data map (len: {}) path: {}",
274274
map.len(),
275275
path);
276-
let g = ConstraintGraph::new(format!("region_data"), region_rels, map);
276+
let g = ConstraintGraph::new("region_data".to_string(), region_rels, map);
277277
debug!("dump_region_data calling render");
278278
let mut v = Vec::new();
279279
dot::render(&g, &mut v).unwrap();

src/librustc/lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl BuiltinLintDiagnostics {
424424
Ok(ref s) if is_global => (format!("dyn ({})", s),
425425
Applicability::MachineApplicable),
426426
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
427-
Err(_) => (format!("dyn <type>"), Applicability::HasPlaceholders)
427+
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders)
428428
};
429429
db.span_suggestion_with_applicability(span, "use `dyn`", sugg, app);
430430
}
@@ -441,7 +441,7 @@ impl BuiltinLintDiagnostics {
441441

442442
(format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
443443
}
444-
Err(_) => (format!("crate::<path>"), Applicability::HasPlaceholders)
444+
Err(_) => ("crate::<path>".to_string(), Applicability::HasPlaceholders)
445445
};
446446
db.span_suggestion_with_applicability(span, "use `crate`", sugg, app);
447447
}

src/librustc/middle/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
107107
}
108108
Err(LayoutError::Unknown(bad)) => {
109109
if bad == ty {
110-
format!("this type's size can vary")
110+
"this type's size can vary".to_string()
111111
} else {
112112
format!("size can vary because of {}", bad)
113113
}

src/librustc/middle/mem_categorization.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1512,15 +1512,15 @@ impl<'tcx> cmt_<'tcx> {
15121512
None => {
15131513
match pk {
15141514
Unique => {
1515-
format!("`Box` content")
1515+
"`Box` content".to_string()
15161516
}
15171517
UnsafePtr(..) => {
1518-
format!("dereference of raw pointer")
1518+
"dereference of raw pointer".to_string()
15191519
}
15201520
BorrowedPtr(..) => {
15211521
match self.note {
1522-
NoteIndex => format!("indexed content"),
1523-
_ => format!("borrowed content"),
1522+
NoteIndex => "indexed content".to_string(),
1523+
_ => "borrowed content".to_string(),
15241524
}
15251525
}
15261526
}

src/librustc/middle/resolve_lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2665,7 +2665,7 @@ pub fn report_missing_lifetime_specifiers(
26652665
let msg = if count > 1 {
26662666
format!("expected {} lifetime parameters", count)
26672667
} else {
2668-
format!("expected lifetime parameter")
2668+
"expected lifetime parameter".to_string()
26692669
};
26702670

26712671
err.span_label(span, msg);

src/librustc/traits/specialize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub(super) fn specialization_graph_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx
364364
match tcx.span_of_impl(overlap.with_impl) {
365365
Ok(span) => {
366366
err.span_label(tcx.sess.codemap().def_span(span),
367-
format!("first implementation here"));
367+
"first implementation here".to_string());
368368
err.span_label(impl_span,
369369
format!("conflicting implementation{}",
370370
overlap.self_desc

src/librustc/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
204204
format!("&{}", tymut_string)
205205
}
206206
}
207-
ty::TyFnDef(..) => format!("fn item"),
207+
ty::TyFnDef(..) => "fn item".to_string(),
208208
ty::TyFnPtr(_) => "fn pointer".to_string(),
209209
ty::TyDynamic(ref inner, ..) => {
210210
inner.principal().map_or_else(|| "trait".to_string(),

0 commit comments

Comments
 (0)