From 36e8eb68933abd3123db303741d25f9323de3eca Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 19 Feb 2018 12:50:50 +0900 Subject: [PATCH] Use push-approach over format! in rewrite_segment --- rustfmt-core/src/types.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/rustfmt-core/src/types.rs b/rustfmt-core/src/types.rs index 6d2dc84cec9..204b5869f03 100644 --- a/rustfmt-core/src/types.rs +++ b/rustfmt-core/src/types.rs @@ -200,10 +200,13 @@ fn rewrite_segment( context: &RewriteContext, shape: Shape, ) -> Option { - let ident_len = segment.identifier.to_string().len(); + let mut result = String::with_capacity(128); + result.push_str(&segment.identifier.name.as_str()); + + let ident_len = result.len(); let shape = shape.shrink_left(ident_len)?; - let params = if let Some(ref params) = segment.parameters { + if let Some(ref params) = segment.parameters { match **params { ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() || !data.types.is_empty() @@ -225,6 +228,7 @@ fn rewrite_segment( } else { "" }; + result.push_str(separator); let generics_shape = generics_shape_from_config(context.config, shape, separator.len())?; @@ -247,29 +251,27 @@ fn rewrite_segment( // Update position of last bracket. *span_lo = next_span_lo; - format!("{}{}", separator, generics_str) + result.push_str(&generics_str) } ast::PathParameters::Parenthesized(ref data) => { let output = match data.output { Some(ref ty) => FunctionRetTy::Ty(ty.clone()), None => FunctionRetTy::Default(codemap::DUMMY_SP), }; - format_function_type( + result.push_str(&format_function_type( data.inputs.iter().map(|x| &**x), &output, false, data.span, context, shape, - )? + )?); } - _ => String::new(), + _ => (), } - } else { - String::new() - }; + } - Some(format!("{}{}", segment.identifier, params)) + Some(result) } fn format_function_type<'a, I>(