Skip to content

Commit e36b901

Browse files
committed
Optimize format usage
Per #112156, using `&` in `format!` may cause a small perf delay, so I tried to clean up one module at a time format usage. This PR includes a few removals of the ref in format (they do compile locally without the ref), as well as a few format inlining for consistency.
1 parent 8771282 commit e36b901

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,13 @@ impl Diagnostic {
420420
let expected_label = if expected_label.is_empty() {
421421
"expected".to_string()
422422
} else {
423-
format!("expected {}", expected_label)
423+
format!("expected {expected_label}")
424424
};
425425
let found_label = found_label.to_string();
426426
let found_label = if found_label.is_empty() {
427427
"found".to_string()
428428
} else {
429-
format!("found {}", found_label)
429+
format!("found {found_label}")
430430
};
431431
let (found_padding, expected_padding) = if expected_label.len() > found_label.len() {
432432
(expected_label.len() - found_label.len(), 0)
@@ -439,13 +439,13 @@ impl Diagnostic {
439439
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
440440
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
441441
}));
442-
msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
442+
msg.push((format!("`{expected_extra}\n"), Style::NoStyle));
443443
msg.push((format!("{}{} `", " ".repeat(found_padding), found_label), Style::NoStyle));
444444
msg.extend(found.0.iter().map(|x| match *x {
445445
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
446446
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
447447
}));
448-
msg.push((format!("`{}", found_extra), Style::NoStyle));
448+
msg.push((format!("`{found_extra}"), Style::NoStyle));
449449

450450
// For now, just attach these as notes.
451451
self.highlighted_note(msg);
@@ -454,7 +454,7 @@ impl Diagnostic {
454454

455455
pub fn note_trait_signature(&mut self, name: Symbol, signature: String) -> &mut Self {
456456
self.highlighted_note(vec![
457-
(format!("`{}` from trait: `", name), Style::NoStyle),
457+
(format!("`{name}` from trait: `"), Style::NoStyle),
458458
(signature, Style::Highlight),
459459
("`".to_string(), Style::NoStyle),
460460
]);

compiler/rustc_errors/src/diagnostic_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl IntoDiagnosticArg for bool {
102102

103103
impl IntoDiagnosticArg for char {
104104
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
105-
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self)))
105+
DiagnosticArgValue::Str(Cow::Owned(format!("{self:?}")))
106106
}
107107
}
108108

compiler/rustc_errors/src/emitter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ pub trait Emitter: Translate {
279279
let msg = if substitution.is_empty() || sugg.style.hide_inline() {
280280
// This substitution is only removal OR we explicitly don't want to show the
281281
// code inline (`hide_inline`). Therefore, we don't show the substitution.
282-
format!("help: {}", &msg)
282+
format!("help: {msg}")
283283
} else {
284284
// Show the default suggestion text with the substitution
285285
format!(
286286
"help: {}{}: `{}`",
287-
&msg,
287+
msg,
288288
if self.source_map().is_some_and(|sm| is_case_difference(
289289
sm,
290290
substitution,

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ impl HandlerInner {
14851485
let _ = self.fatal(errors);
14861486
}
14871487
(_, _) => {
1488-
let _ = self.fatal(format!("{}; {}", &errors, &warnings));
1488+
let _ = self.fatal(format!("{errors}; {warnings}"));
14891489
}
14901490
}
14911491

0 commit comments

Comments
 (0)