Skip to content

Commit

Permalink
Auto merge of #52767 - ljedrz:avoid_format, r=petrochenkov
Browse files Browse the repository at this point in the history
Prefer to_string() to format!()

Simple benchmarks suggest in some cases it can be faster by even 37%:
```
test converting_f64_long  ... bench:         339 ns/iter (+/- 199)
test converting_f64_short ... bench:         136 ns/iter (+/- 34)
test converting_i32_long  ... bench:          87 ns/iter (+/- 16)
test converting_i32_short ... bench:          87 ns/iter (+/- 49)
test converting_str       ... bench:          54 ns/iter (+/- 15)
test formatting_f64_long  ... bench:         349 ns/iter (+/- 176)
test formatting_f64_short ... bench:         145 ns/iter (+/- 14)
test formatting_i32_long  ... bench:          98 ns/iter (+/- 14)
test formatting_i32_short ... bench:          93 ns/iter (+/- 15)
test formatting_str       ... bench:          86 ns/iter (+/- 23)
```
  • Loading branch information
bors committed Jul 29, 2018
2 parents a5c2d0f + 57a5a9b commit 023fd7e
Show file tree
Hide file tree
Showing 36 changed files with 95 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ impl<'a> Builder<'a> {
cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
}

cargo.env("RUSTC_VERBOSE", format!("{}", self.verbosity));
cargo.env("RUSTC_VERBOSE", self.verbosity.to_string());

// in std, we want to avoid denying warnings for stage 0 as that makes cfg's painful.
if self.config.deny_warnings && !(mode == Mode::Std && stage == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/tests/str_lossy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn chunks() {
fn display() {
assert_eq!(
"Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye",
&format!("{}", Utf8Lossy::from_bytes(b"Hello\xC0\x80 There\xE6\x83 Goodbye")));
&Utf8Lossy::from_bytes(b"Hello\xC0\x80 There\xE6\x83 Goodbye").to_string());
}

#[test]
Expand Down
26 changes: 13 additions & 13 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// Output the lifetimes fot the first type
let lifetimes = sub.regions()
.map(|lifetime| {
let s = format!("{}", lifetime);
let s = lifetime.to_string();
if s.is_empty() {
"'_".to_string()
} else {
Expand All @@ -582,7 +582,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
value.0.extend((values.0).0);
other_value.0.extend((values.1).0);
} else {
value.push_highlighted(format!("{}", type_arg));
value.push_highlighted(type_arg.to_string());
}

if len > 0 && i != len - 1 {
Expand Down Expand Up @@ -716,7 +716,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
mutbl: hir::Mutability,
s: &mut DiagnosticStyledString,
) {
let r = &format!("{}", r);
let r = &r.to_string();
s.push_highlighted(format!(
"&{}{}{}",
r,
Expand All @@ -727,7 +727,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
""
}
));
s.push_normal(format!("{}", ty));
s.push_normal(ty.to_string());
}

match (&t1.sty, &t2.sty) {
Expand Down Expand Up @@ -768,7 +768,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}

fn lifetime_display(lifetime: Region) -> String {
let s = format!("{}", lifetime);
let s = lifetime.to_string();
if s.is_empty() {
"'_".to_string()
} else {
Expand Down Expand Up @@ -863,8 +863,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// We couldn't find anything in common, highlight everything.
// let x: Bar<Qux> = y::<Foo<Zar>>();
(
DiagnosticStyledString::highlighted(format!("{}", t1)),
DiagnosticStyledString::highlighted(format!("{}", t2)),
DiagnosticStyledString::highlighted(t1.to_string()),
DiagnosticStyledString::highlighted(t2.to_string()),
)
}
}
Expand All @@ -873,12 +873,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
(&ty::TyRef(r1, ref_ty1, mutbl1), _) if equals(&ref_ty1, &t2) => {
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
values.1.push_normal(format!("{}", t2));
values.1.push_normal(t2.to_string());
values
}
(_, &ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&t1, &ref_ty2) => {
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
values.0.push_normal(format!("{}", t1));
values.0.push_normal(t1.to_string());
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
values
}
Expand All @@ -902,8 +902,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
} else {
// We couldn't find anything in common, highlight everything.
(
DiagnosticStyledString::highlighted(format!("{}", t1)),
DiagnosticStyledString::highlighted(format!("{}", t2)),
DiagnosticStyledString::highlighted(t1.to_string()),
DiagnosticStyledString::highlighted(t2.to_string()),
)
}
}
Expand Down Expand Up @@ -1073,8 +1073,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}

Some((
DiagnosticStyledString::highlighted(format!("{}", exp_found.expected)),
DiagnosticStyledString::highlighted(format!("{}", exp_found.found)),
DiagnosticStyledString::highlighted(exp_found.expected.to_string()),
DiagnosticStyledString::highlighted(exp_found.found.to_string()),
))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
let lifetime_name = match sup_r {
RegionKind::ReFree(FreeRegion {
bound_region: BoundRegion::BrNamed(_, ref name), ..
}) => format!("{}", name),
}) => name.to_string(),
_ => "'_".to_owned(),
};
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(return_sp) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2093,7 +2093,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {

// When printing regions, add trailing space if necessary.
let region = if ppaux::verbose() || ppaux::identify_regions() {
let mut region = format!("{}", region);
let mut region = region.to_string();
if region.len() > 0 {
region.push(' ');
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/session/code_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl CodeStats {
let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
let indent = if !struct_like {
let name = match name.as_ref() {
Some(name) => format!("{}", name),
None => format!("{}", i),
Some(name) => name.to_owned(),
None => i.to_string(),
};
println!("print-type-size {}variant `{}`: {} bytes",
indent, name, size - discr_size);
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
&data.parent_trait_ref);
match self.get_parent_trait_ref(&data.parent_code) {
Some(t) => Some(t),
None => Some(format!("{}", parent_trait_ref.skip_binder().self_ty())),
None => Some(parent_trait_ref.skip_binder().self_ty().to_string()),
}
}
_ => None,
Expand Down Expand Up @@ -797,12 +797,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ty::TypeVariants::TyTuple(ref tys) => ArgKind::Tuple(
Some(span),
tys.iter()
.map(|ty| ("_".to_owned(), format!("{}", ty.sty)))
.map(|ty| ("_".to_owned(), ty.sty.to_string()))
.collect::<Vec<_>>()
),
_ => ArgKind::Arg("_".to_owned(), format!("{}", t.sty)),
_ => ArgKind::Arg("_".to_owned(), t.sty.to_string()),
}).collect(),
ref sty => vec![ArgKind::Arg("_".to_owned(), format!("{}", sty))],
ref sty => vec![ArgKind::Arg("_".to_owned(), sty.to_string())],
};
if found.len() == expected.len() {
self.report_closure_arg_mismatch(span,
Expand Down Expand Up @@ -989,7 +989,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}) => {
(self.tcx.sess.codemap().def_span(span),
fields.iter().map(|field| {
ArgKind::Arg(format!("{}", field.ident), "_".to_string())
ArgKind::Arg(field.ident.to_string(), "_".to_string())
}).collect::<Vec<_>>())
}
hir::map::NodeStructCtor(ref variant_data) => {
Expand Down Expand Up @@ -1152,7 +1152,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
::rustc_target::spec::abi::Abi::Rust
)
};
format!("{}", ty::Binder::bind(sig))
ty::Binder::bind(sig).to_string()
}

let argument_is_closure = expected_ref.skip_binder().substs.type_at(0).is_closure();
Expand Down Expand Up @@ -1575,10 +1575,10 @@ impl ArgKind {
ty::TyTuple(ref tys) => ArgKind::Tuple(
None,
tys.iter()
.map(|ty| ("_".to_owned(), format!("{}", ty.sty)))
.map(|ty| ("_".to_owned(), ty.sty.to_string()))
.collect::<Vec<_>>()
),
_ => ArgKind::Arg("_".to_owned(), format!("{}", t.sty)),
_ => ArgKind::Arg("_".to_owned(), t.sty.to_string()),
}
}
}
2 changes: 1 addition & 1 deletion src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
ty::TyUint(_) |
ty::TyFloat(_) |
ty::TyStr => {
buffer.push(&format!("{}", self_ty));
buffer.push(&self_ty.to_string());
}

_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl AssociatedItem {
// late-bound regions, and we don't want method signatures to show up
// `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
// regions just fine, showing `fn(&MyType)`.
format!("{}", tcx.fn_sig(self.def_id).skip_binder())
tcx.fn_sig(self.def_id).skip_binder().to_string()
}
ty::AssociatedKind::Type => format!("type {};", self.ident),
ty::AssociatedKind::Existential => format!("existential type {};", self.ident),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub fn to_readable_str(mut val: usize) -> String {
val /= 1000;

if val == 0 {
groups.push(format!("{}", group));
groups.push(group.to_string());
break;
} else {
groups.push(format!("{:03}", group));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
let mut err = self.cannot_act_on_moved_value(use_span,
verb,
msg,
Some(format!("{}", nl)),
Some(nl.to_string()),
Origin::Ast);
let need_note = match lp.ty.sty {
ty::TypeVariants::TyClosure(id, _) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'a> ArchiveBuilder<'a> {
// Ignoring obj file starting with the crate name
// as simple comparison is not enough - there
// might be also an extra name suffix
let obj_start = format!("{}", name);
let obj_start = name.to_owned();

self.add_archive(rlib, move |fname: &str| {
// Ignore bytecode/metadata files, no matter the name.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ fn link_natively(sess: &Session,
}
};

linker_error.note(&format!("{}", e));
linker_error.note(&e.to_string());

if !linker_not_found {
linker_error.note(&format!("{:?}", &cmd));
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ fn run_compiler_with_pool<'a>(
if let Some(err) = input_err {
// Immediately stop compilation if there was an issue reading
// the input (for example if the input stream is not UTF-8).
sess.err(&format!("{}", err));
sess.err(&err.to_string());
return (Err(CompileIncomplete::Stopped), Some(sess));
}

Expand Down Expand Up @@ -1110,7 +1110,7 @@ impl RustcDefaultCalls {
cfgs.push(if let Some(value) = value {
format!("{}=\"{}\"", name, value)
} else {
format!("{}", name)
name.to_string()
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
fieldpat.span,
&format!("the `{}:` in this pattern is redundant", ident));
let subspan = cx.tcx.sess.codemap().span_through_char(fieldpat.span, ':');
err.span_suggestion_short(subspan, "remove this", format!("{}", ident));
err.span_suggestion_short(subspan, "remove this", ident.to_string());
err.emit();
}
}
Expand Down Expand Up @@ -701,7 +701,7 @@ impl EarlyLintPass for BadRepr {
attr.span,
"`repr` attribute isn't configurable with a literal",
);
match format!("{}", lit).as_ref() {
match lit.to_string().as_ref() {
| "C" | "packed" | "rust" | "transparent"
| "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ impl<'a> Context<'a> {
root.triple);
self.rejected_via_triple.push(CrateMismatch {
path: libpath.to_path_buf(),
got: format!("{}", root.triple),
got: root.triple.to_string(),
});
return None;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/borrow_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<'tcx> fmt::Display for BorrowData<'tcx> {
mir::BorrowKind::Unique => "uniq ",
mir::BorrowKind::Mut { .. } => "mut ",
};
let region = format!("{}", self.region);
let region = self.region.to_string();
let region = if region.len() > 0 {
format!("{} ", region)
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
self.append_local_to_string(local, buf)?;
}
Place::Static(ref static_) => {
buf.push_str(&format!("{}", &self.tcx.item_name(static_.def_id)));
buf.push_str(&self.tcx.item_name(static_.def_id).to_string());
}
Place::Projection(ref proj) => {
match proj.elem {
Expand Down Expand Up @@ -766,7 +766,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
let local = &self.mir.local_decls[local_index];
match local.name {
Some(name) => {
buf.push_str(&format!("{}", name));
buf.push_str(&name.to_string());
Ok(())
}
None => Err(()),
Expand Down Expand Up @@ -794,7 +794,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
ProjectionElem::Index(..)
| ProjectionElem::ConstantIndex { .. }
| ProjectionElem::Subslice { .. } => {
format!("{}", self.describe_field(&proj.base, field))
self.describe_field(&proj.base, field).to_string()
}
},
}
Expand All @@ -808,11 +808,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
} else {
match ty.sty {
ty::TyAdt(def, _) => if def.is_enum() {
format!("{}", field.index())
field.index().to_string()
} else {
format!("{}", def.non_enum_variant().fields[field.index()].ident)
def.non_enum_variant().fields[field.index()].ident.to_string()
},
ty::TyTuple(_) => format!("{}", field.index()),
ty::TyTuple(_) => field.index().to_string(),
ty::TyRef(_, ty, _) | ty::TyRawPtr(ty::TypeAndMut { ty, .. }) => {
self.describe_field_from_ty(&ty, field)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/borrow_check/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
};
saw_one = true;
let borrow_data = &self.borrows.operator().borrows()[borrow];
s.push_str(&format!("{}", borrow_data));
s.push_str(&borrow_data.to_string());
});
s.push_str("] ");

Expand All @@ -126,7 +126,7 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
};
saw_one = true;
let borrow_data = &self.borrows.operator().borrows()[borrow];
s.push_str(&format!("{}", borrow_data));
s.push_str(&borrow_data.to_string());
});
s.push_str("] ");

Expand All @@ -138,7 +138,7 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
};
saw_one = true;
let move_path = &self.uninits.operator().move_data().move_paths[mpi_uninit];
s.push_str(&format!("{}", move_path));
s.push_str(&move_path.to_string());
});
s.push_str("] ");

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
err.span_suggestion(
span,
"consider removing this dereference operator",
format!("{}", &snippet[1..]),
(&snippet[1..]).to_owned(),
);
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_traits/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl<'a, 'tcx> ClauseDumper<'a, 'tcx> {
Clause::Implies(program_clause) => program_clause,
Clause::ForAll(program_clause) => program_clause.skip_binder(),
};
format!("{}", program_clause)
program_clause.to_string()
})
.collect();

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
ty::TyFnDef(..) => {
let ptr_ty = self.tcx.mk_fn_ptr(arg_ty.fn_sig(self.tcx));
let ptr_ty = self.resolve_type_vars_if_possible(&ptr_ty);
variadic_error(tcx.sess, arg.span, arg_ty, &format!("{}", ptr_ty));
variadic_error(tcx.sess, arg.span, arg_ty, &ptr_ty.to_string());
}
_ => {}
}
Expand Down
Loading

0 comments on commit 023fd7e

Please sign in to comment.