Skip to content

Commit ef6f0d6

Browse files
committed
Defer allocation for printf suggestions until needed
1 parent 30f168e commit ef6f0d6

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

compiler/rustc_builtin_macros/src/format.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -711,11 +711,11 @@ fn report_missing_placeholders(
711711
};
712712

713713
let pos = sub.position();
714-
let sub = String::from(sub.as_str());
715-
if explained.contains(&sub) {
714+
let sub = sub.as_string();
715+
if explained.contains(sub.as_ref()) {
716716
continue;
717717
}
718-
explained.insert(sub);
718+
explained.insert(String::from(sub));
719719

720720
if !found_foreign {
721721
found_foreign = true;

compiler/rustc_builtin_macros/src/format_foreign.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
pub(crate) mod printf {
2+
use std::borrow::Cow;
3+
24
use rustc_span::InnerSpan;
35

46
use super::strcursor::StrCursor as Cur;
@@ -13,10 +15,10 @@ pub(crate) mod printf {
1315
}
1416

1517
impl<'a> Substitution<'a> {
16-
pub(crate) fn as_str(&self) -> &str {
18+
pub(crate) fn as_string(&self) -> Cow<'_, str> {
1719
match self {
18-
Substitution::Format(fmt) => fmt.span,
19-
Substitution::Escape(_) => "%%",
20+
Substitution::Format(fmt) => fmt.span.into(),
21+
Substitution::Escape(_) => "%%".into(),
2022
}
2123
}
2224

@@ -616,6 +618,8 @@ pub(crate) mod printf {
616618
}
617619

618620
pub(crate) mod shell {
621+
use std::borrow::Cow;
622+
619623
use rustc_span::InnerSpan;
620624

621625
use super::strcursor::StrCursor as Cur;
@@ -628,10 +632,10 @@ pub(crate) mod shell {
628632
}
629633

630634
impl Substitution<'_> {
631-
pub(crate) fn as_str(&self) -> String {
635+
pub(crate) fn as_string(&self) -> Cow<'_, str> {
632636
match self {
633-
Substitution::Ordinal(n, _) => format!("${n}"),
634-
Substitution::Name(n, _) => format!("${n}"),
637+
Substitution::Ordinal(n, _) => format!("${n}").into(),
638+
Substitution::Name(n, _) => format!("${n}").into(),
635639
Substitution::Escape(_) => "$$".into(),
636640
}
637641
}

0 commit comments

Comments
 (0)