Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use .to_string() instead of format!() macro to create strings #69567

Merged
merged 1 commit into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::borrow::Cow;

fn describe_as_module(def_id: DefId, tcx: TyCtxt<'_>) -> String {
if def_id.is_top_level_module() {
format!("top-level module")
"top-level module".to_string()
} else {
format!("module `{}`", tcx.def_path_str(def_id))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
err.span_suggestion(
span,
"consider removing the prefix",
format!("{}", &lint_str[1..]),
lint_str[1..].to_string(),
Applicability::MaybeIncorrect,
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ impl Diagnostic {
found: DiagnosticStyledString,
) -> &mut Self {
let mut msg: Vec<_> =
vec![(format!("required when trying to coerce from type `"), Style::NoStyle)];
vec![("required when trying to coerce from type `".to_string(), Style::NoStyle)];
msg.extend(expected.0.iter().map(|x| match *x {
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
}));
msg.push((format!("` to type '"), Style::NoStyle));
msg.push(("` to type '".to_string(), Style::NoStyle));
msg.extend(found.0.iter().map(|x| match *x {
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
}));
msg.push((format!("`"), Style::NoStyle));
msg.push(("`".to_string(), Style::NoStyle));

// For now, just attach these as notes
self.highlighted_note(msg);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub(super) fn note_and_explain_region(
// uh oh, hope no user ever sees THIS
ty::ReEmpty(ui) => (format!("the empty lifetime in universe {:?}", ui), None),

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

// FIXME(#13998) RePlaceholder should probably print like
// ReFree rather than dumping Debug output on the user.
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1919,21 +1919,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
use rustc::ty::TyKind::*;
match ty.kind {
// Primitive types that don't like 0 as a value.
Ref(..) => Some((format!("references must be non-null"), None)),
Adt(..) if ty.is_box() => Some((format!("`Box` must be non-null"), None)),
FnPtr(..) => Some((format!("function pointers must be non-null"), None)),
Never => Some((format!("the `!` type has no valid value"), None)),
Ref(..) => Some(("references must be non-null".to_string(), None)),
Adt(..) if ty.is_box() => Some(("`Box` must be non-null".to_string(), None)),
FnPtr(..) => Some(("function pointers must be non-null".to_string(), None)),
Never => Some(("the `!` type has no valid value".to_string(), None)),
RawPtr(tm) if matches!(tm.ty.kind, Dynamic(..)) =>
// raw ptr to dyn Trait
{
Some((format!("the vtable of a wide raw pointer must be non-null"), None))
Some(("the vtable of a wide raw pointer must be non-null".to_string(), None))
}
// Primitive types with other constraints.
Bool if init == InitKind::Uninit => {
Some((format!("booleans must be either `true` or `false`"), None))
Some(("booleans must be either `true` or `false`".to_string(), None))
}
Char if init == InitKind::Uninit => {
Some((format!("characters must be a valid Unicode codepoint"), None))
Some(("characters must be a valid Unicode codepoint".to_string(), None))
}
// Recurse and checks for some compound types.
Adt(adt_def, substs) if !adt_def.is_union() => {
Expand Down Expand Up @@ -1961,7 +1961,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
}
// Now, recurse.
match adt_def.variants.len() {
0 => Some((format!("enums with no variants have no valid value"), None)),
0 => Some(("enums with no variants have no valid value".to_string(), None)),
1 => {
// Struct, or enum with exactly one variant.
// Proceed recursively, check all fields.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ fn lint_overflowing_range_endpoint<'a, 'tcx>(
// We need to preserve the literal's suffix,
// as it may determine typing information.
let suffix = match lit.node {
LitKind::Int(_, LitIntType::Signed(s)) => format!("{}", s.name_str()),
LitKind::Int(_, LitIntType::Unsigned(s)) => format!("{}", s.name_str()),
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_owned(),
LitKind::Int(_, LitIntType::Signed(s)) => s.name_str().to_string(),
LitKind::Int(_, LitIntType::Unsigned(s)) => s.name_str().to_string(),
LitKind::Int(_, LitIntType::Unsuffixed) => "".to_string(),
_ => bug!(),
};
let suggestion = format!("{}..={}{}", start, lit_val - 1, suffix);
Expand Down
18 changes: 9 additions & 9 deletions src/librustc_mir/borrow_check/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,14 @@ pub(super) enum BorrowedContentSource<'tcx> {
impl BorrowedContentSource<'tcx> {
pub(super) fn describe_for_unnamed_place(&self) -> String {
match *self {
BorrowedContentSource::DerefRawPointer => format!("a raw pointer"),
BorrowedContentSource::DerefSharedRef => format!("a shared reference"),
BorrowedContentSource::DerefMutableRef => format!("a mutable reference"),
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
BorrowedContentSource::OverloadedDeref(ty) => {
if ty.is_rc() {
format!("an `Rc`")
"an `Rc`".to_string()
} else if ty.is_arc() {
format!("an `Arc`")
"an `Arc`".to_string()
} else {
format!("dereference of `{}`", ty)
}
Expand All @@ -649,16 +649,16 @@ impl BorrowedContentSource<'tcx> {

pub(super) fn describe_for_immutable_place(&self) -> String {
match *self {
BorrowedContentSource::DerefRawPointer => format!("a `*const` pointer"),
BorrowedContentSource::DerefSharedRef => format!("a `&` reference"),
BorrowedContentSource::DerefRawPointer => "a `*const` pointer".to_string(),
BorrowedContentSource::DerefSharedRef => "a `&` reference".to_string(),
BorrowedContentSource::DerefMutableRef => {
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
}
BorrowedContentSource::OverloadedDeref(ty) => {
if ty.is_rc() {
format!("an `Rc`")
"an `Rc`".to_string()
} else if ty.is_arc() {
format!("an `Arc`")
"an `Arc`".to_string()
} else {
format!("a dereference of `{}`", ty)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let place_ty = move_from.ty(*self.body, self.infcx.tcx).ty;
let place_desc = match self.describe_place(move_from.as_ref()) {
Some(desc) => format!("`{}`", desc),
None => format!("value"),
None => "value".to_string(),
};

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ fn write_scope_tree(
indented_decl.push_str(";");

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

writeln!(
w,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl<'a> Parser<'a> {
.span_suggestion(
seq_span,
"...or a vertical bar to match on multiple alternatives",
format!("{}", seq_snippet.replace(",", " |")),
seq_snippet.replace(",", " |"),
Applicability::MachineApplicable,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl NonConstExpr {
match self {
Self::Loop(src) => format!("`{}`", src.name()),
Self::Match(src) => format!("`{}`", src.name()),
Self::OrPattern => format!("or-pattern"),
Self::OrPattern => "or-pattern".to_string(),
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a> Resolver<'a> {
E0401,
"can't use generic parameters from outer function",
);
err.span_label(span, format!("use of generic parameter from outer function"));
err.span_label(span, "use of generic parameter from outer function".to_string());

let sm = self.session.source_map();
match outer_res {
Expand Down Expand Up @@ -155,7 +155,8 @@ impl<'a> Resolver<'a> {
} else if let Some(sp) = sm.generate_fn_name_span(span) {
err.span_label(
sp,
format!("try adding a local generic parameter in this method instead"),
"try adding a local generic parameter in this method instead"
.to_string(),
);
} else {
err.help("try using a local generic parameter instead");
Expand Down
14 changes: 6 additions & 8 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
err.code(rustc_errors::error_code!(E0411));
err.span_label(
span,
format!("`Self` is only available in impls, traits, and type definitions"),
"`Self` is only available in impls, traits, and type definitions".to_string(),
);
return (err, Vec::new());
}
Expand All @@ -186,12 +186,10 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {

err.code(rustc_errors::error_code!(E0424));
err.span_label(span, match source {
PathSource::Pat => format!(
"`self` value is a keyword and may not be bound to variables or shadowed",
),
_ => format!(
"`self` value is a keyword only available in methods with a `self` parameter",
),
PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed"
.to_string(),
_ => "`self` value is a keyword only available in methods with a `self` parameter"
.to_string(),
});
if let Some(span) = &self.diagnostic_metadata.current_function {
err.span_label(*span, "this function doesn't have a `self` parameter");
Expand Down Expand Up @@ -558,7 +556,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
if is_expected(ctor_def) && !accessible_ctor {
err.span_label(
span,
format!("constructor is not visible here due to private fields"),
"constructor is not visible here due to private fields".to_string(),
);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if lstring.starts_with('&') {
// let a = String::new();
// let _ = &a + "bar";
format!("{}", &lstring[1..])
lstring[1..].to_string()
} else {
format!("{}.to_owned()", lstring)
},
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let to_string = if l.starts_with('&') {
// let a = String::new(); let b = String::new();
// let _ = &a + b;
format!("{}", &l[1..])
l[1..].to_string()
} else {
format!("{}.to_owned()", l)
};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ crate fn placeholder_type_error(
}) {
// Account for `_` already present in cases like `struct S<_>(_);` and suggest
// `struct S<T>(T);` instead of `struct S<_, T>(T);`.
sugg.push((arg.span, format!("{}", type_name)));
sugg.push((arg.span, type_name.to_string()));
} else {
sugg.push((
generics.iter().last().unwrap().span.shrink_to_hi(),
Expand Down Expand Up @@ -475,7 +475,7 @@ fn get_new_lifetime_name<'tcx>(

let a_to_z_repeat_n = |n| {
(b'a'..=b'z').map(move |c| {
let mut s = format!("'");
let mut s = '\''.to_string();
s.extend(std::iter::repeat(char::from(c)).take(n));
s
})
Expand Down