Skip to content

Commit aef0e34

Browse files
committed
Avoid ref when using format! in compiler
Clean up a few minor refs in `format!` macro, as it has a performance cost. Apparently the compiler is unable to inline `format!("{}", &variable)`, and does a run-time double-reference instead (format macro already does one level referencing). Inlining format args prevents accidental `&` misuse.
1 parent 0cd01aa commit aef0e34

File tree

23 files changed

+61
-65
lines changed

23 files changed

+61
-65
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
505505
let nop_inst = fx.bcx.ins().nop();
506506
fx.add_comment(
507507
nop_inst,
508-
format!("virtual call; self arg pass mode: {:?}", &fn_abi.args[0]),
508+
format!("virtual call; self arg pass mode: {:?}", fn_abi.args[0]),
509509
);
510510
}
511511

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ fn link_natively(
759759
sess.dcx().abort_if_errors();
760760

761761
// Invoke the system linker
762-
info!("{:?}", &cmd);
762+
info!("{cmd:?}");
763763
let retry_on_segfault = env::var("RUSTC_RETRY_LINKER_ON_SEGFAULT").is_ok();
764764
let unknown_arg_regex =
765765
Regex::new(r"(unknown|unrecognized) (command line )?(option|argument)").unwrap();
@@ -796,7 +796,7 @@ fn link_natively(
796796
cmd.arg(arg);
797797
}
798798
}
799-
info!("{:?}", &cmd);
799+
info!("{cmd:?}");
800800
continue;
801801
}
802802

@@ -817,7 +817,7 @@ fn link_natively(
817817
cmd.arg(arg);
818818
}
819819
}
820-
info!("{:?}", &cmd);
820+
info!("{cmd:?}");
821821
continue;
822822
}
823823

@@ -878,7 +878,7 @@ fn link_natively(
878878
cmd.arg(arg);
879879
}
880880
}
881-
info!("{:?}", &cmd);
881+
info!("{cmd:?}");
882882
continue;
883883
}
884884

@@ -996,7 +996,7 @@ fn link_natively(
996996
sess.dcx().emit_err(errors::UnableToExeLinker {
997997
linker_path,
998998
error: e,
999-
command_formatted: format!("{:?}", &cmd),
999+
command_formatted: format!("{cmd:?}"),
10001000
});
10011001
}
10021002

@@ -1567,7 +1567,7 @@ fn print_native_static_libs(
15671567
sess.dcx().emit_note(errors::StaticLibraryNativeArtifacts);
15681568
// Prefix for greppability
15691569
// Note: This must not be translated as tools are allowed to depend on this exact string.
1570-
sess.dcx().note(format!("native-static-libs: {}", &lib_args.join(" ")));
1570+
sess.dcx().note(format!("native-static-libs: {}", lib_args.join(" ")));
15711571
}
15721572
}
15731573
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
328328
sym::link_section => {
329329
if let Some(val) = attr.value_str() {
330330
if val.as_str().bytes().any(|b| b == 0) {
331-
let msg = format!("illegal null byte in link_section value: `{}`", &val);
331+
let msg = format!("illegal null byte in link_section value: `{val}`");
332332
tcx.dcx().span_err(attr.span, msg);
333333
} else {
334334
codegen_fn_attrs.link_section = Some(val);
@@ -726,7 +726,7 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
726726
if *ordinal <= u16::MAX as u128 {
727727
Some(ordinal.get() as u16)
728728
} else {
729-
let msg = format!("ordinal value in `link_ordinal` is too large: `{}`", &ordinal);
729+
let msg = format!("ordinal value in `link_ordinal` is too large: `{ordinal}`");
730730
tcx.dcx()
731731
.struct_span_err(attr.span, msg)
732732
.with_note("the value may not exceed `u16::MAX`")

compiler/rustc_codegen_ssa/src/mono_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
130130

131131
let symbol_name = self.symbol_name(cx.tcx()).name;
132132

133-
debug!("symbol {}", &symbol_name);
133+
debug!("symbol {symbol_name}");
134134

135135
match *self {
136136
MonoItem::Static(def_id) => {

compiler/rustc_fluent_macro/src/fluent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
253253

254254
for Attribute { id: Identifier { name: attr_name }, .. } in attributes {
255255
let snake_name = Ident::new(
256-
&format!("{}{}", &crate_prefix, &attr_name.replace('-', "_")),
256+
&format!("{crate_prefix}{}", attr_name.replace('-', "_")),
257257
resource_str.span(),
258258
);
259259
if !previous_attrs.insert(snake_name.clone()) {

compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
651651
self.path_segment.hir_id,
652652
num_params_to_take,
653653
);
654-
debug!("suggested_args: {:?}", &suggested_args);
654+
debug!("suggested_args: {suggested_args:?}");
655655

656656
match self.angle_brackets {
657657
AngleBrackets::Missing => {

compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn check_explicit_predicates<'tcx>(
249249
let explicit_predicates = explicit_map.explicit_predicates_of(tcx, def_id);
250250

251251
for (outlives_predicate, &span) in explicit_predicates.as_ref().skip_binder() {
252-
debug!("outlives_predicate = {:?}", &outlives_predicate);
252+
debug!("outlives_predicate = {outlives_predicate:?}");
253253

254254
// Careful: If we are inferring the effects of a `dyn Trait<..>`
255255
// type, then when we look up the predicates for `Trait`,
@@ -289,12 +289,12 @@ fn check_explicit_predicates<'tcx>(
289289
&& let GenericArgKind::Type(ty) = outlives_predicate.0.unpack()
290290
&& ty.walk().any(|arg| arg == self_ty.into())
291291
{
292-
debug!("skipping self ty = {:?}", &ty);
292+
debug!("skipping self ty = {ty:?}");
293293
continue;
294294
}
295295

296296
let predicate = explicit_predicates.rebind(*outlives_predicate).instantiate(tcx, args);
297-
debug!("predicate = {:?}", &predicate);
297+
debug!("predicate = {predicate:?}");
298298
insert_outlives_predicate(tcx, predicate.0, predicate.1, span, required_predicates);
299299
}
300300
}

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,9 +1265,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12651265
}
12661266
(
12671267
match parent_pred {
1268-
None => format!("`{}`", &p),
1268+
None => format!("`{p}`"),
12691269
Some(parent_pred) => match format_pred(*parent_pred) {
1270-
None => format!("`{}`", &p),
1270+
None => format!("`{p}`"),
12711271
Some((parent_p, _)) => {
12721272
if !suggested
12731273
&& !suggested_bounds.contains(pred)

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn report_unstable(
112112
) {
113113
let msg = match reason {
114114
Some(r) => format!("use of unstable library feature '{feature}': {r}"),
115-
None => format!("use of unstable library feature '{}'", &feature),
115+
None => format!("use of unstable library feature '{feature}'"),
116116
};
117117

118118
if is_soft {

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2627,7 +2627,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
26272627
self.prepare_region_info(value);
26282628
}
26292629

2630-
debug!("self.used_region_names: {:?}", &self.used_region_names);
2630+
debug!("self.used_region_names: {:?}", self.used_region_names);
26312631

26322632
let mut empty = true;
26332633
let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {

0 commit comments

Comments
 (0)