From 5247d98d31898326555c6e35780e0388d5e35514 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Thu, 30 Apr 2015 10:31:42 +0200 Subject: [PATCH] Change `to_string` to `to_owned` when it just creates a `String` from a `&str` This means that it doesn't have to go through the formatting hierarchy and can just immediately reserve enough memory. --- src/changes.rs | 6 +++--- src/functions.rs | 14 +++++++------- src/imports.rs | 2 +- src/mod.rs | 4 ++-- src/visitor.rs | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/changes.rs b/src/changes.rs index 2b42c164690..8884b05d77e 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -153,8 +153,8 @@ pub fn write_file(&self, // Do a little dance to make writing safer - write to a temp file // rename the original to a .bk, then rename the temp file to the // original. - let tmp_name = filename.to_string() + ".tmp"; - let bk_name = filename.to_string() + ".bk"; + let tmp_name = filename.to_owned() + ".tmp"; + let bk_name = filename.to_owned() + ".bk"; { // Write text to temp file let mut tmp_file = try!(File::create(&tmp_name)); @@ -165,7 +165,7 @@ pub fn write_file(&self, try!(::std::fs::rename(tmp_name, filename)); } WriteMode::NewFile(extn) => { - let filename = filename.to_string() + "." + extn; + let filename = filename.to_owned() + "." + extn; let mut file = try!(File::create(&filename)); try!(write!(file, "{}", text)); } diff --git a/src/functions.rs b/src/functions.rs index a753f38d9f7..3d6fcb88c25 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -154,7 +154,7 @@ fn rewrite_args(&self, &None => String::new(), }; let mut_str = match m { - &ast::Mutability::MutMutable => "mut ".to_string(), + &ast::Mutability::MutMutable => "mut ".to_owned(), &ast::Mutability::MutImmutable => String::new(), }; arg_item_strs[0] = format!("&{}{}self", lt_str, mut_str); @@ -164,7 +164,7 @@ fn rewrite_args(&self, arg_item_strs[0] = format!("self: {}", pprust::ty_to_string(ty)); } ast::ExplicitSelf_::SelfValue(_) => { - arg_item_strs[0] = "self".to_string(); + arg_item_strs[0] = "self".to_owned(); min_args = 2; } _ => {} @@ -174,7 +174,7 @@ fn rewrite_args(&self, // Comments between args let mut arg_comments = Vec::new(); if min_args == 2 { - arg_comments.push("".to_string()); + arg_comments.push("".to_owned()); } // TODO if there are no args, there might still be a comment, but without // spans for the comment or parens, there is no chance of getting it right. @@ -239,7 +239,7 @@ fn make_comments_for_list(&self, } else if snippet.ends_with(separator) { snippet = snippet[..snippet.len()-separator.len()].trim_matches(white_space); } - result.push(snippet.to_string()); + result.push(snippet.to_owned()); prev_end = get_hi(&item); } // Get the last commment. @@ -254,7 +254,7 @@ fn make_comments_for_list(&self, let snippet = &snippet[..snippet.find(terminator) .unwrap_or(snippet.find(separator).unwrap_or(snippet.len()))]; let snippet = snippet.trim(); - result.push(snippet.to_string()); + result.push(snippet.to_owned()); result } @@ -422,8 +422,8 @@ fn rewrite_where_clause(&self, fn rewrite_return(&self, ret: &ast::FunctionRetTy) -> String { match *ret { ast::FunctionRetTy::DefaultReturn(_) => String::new(), - ast::FunctionRetTy::NoReturn(_) => "-> !".to_string(), - ast::FunctionRetTy::Return(ref ty) => "-> ".to_string() + &pprust::ty_to_string(ty), + ast::FunctionRetTy::NoReturn(_) => "-> !".to_owned(), + ast::FunctionRetTy::Return(ref ty) => "-> ".to_owned() + &pprust::ty_to_string(ty), } } diff --git a/src/imports.rs b/src/imports.rs index 1532050522a..09a702dbaa1 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -70,7 +70,7 @@ pub fn rewrite_use_list(&mut self, false } ) { - Some(("self".to_string(), String::new())) + Some(("self".to_owned(), String::new())) } else { None }; diff --git a/src/mod.rs b/src/mod.rs index be0bea4b70c..45b2bce1638 100644 --- a/src/mod.rs +++ b/src/mod.rs @@ -305,12 +305,12 @@ fn idempotent_tests() { let path = entry.unwrap().path(); let file_name = path.to_str().unwrap(); println!("Testing '{}'...", file_name); - run(vec!["rustfmt".to_string(), file_name.to_string()], WriteMode::Return(HANDLE_RESULT)); + run(vec!["rustfmt".to_owned(), file_name.to_owned()], WriteMode::Return(HANDLE_RESULT)); count += 1; } // And also dogfood ourselves! println!("Testing 'src/mod.rs'..."); - run(vec!["rustfmt".to_string(), "src/mod.rs".to_string()], WriteMode::Return(HANDLE_RESULT)); + run(vec!["rustfmt".to_owned(), "src/mod.rs".to_owned()], WriteMode::Return(HANDLE_RESULT)); count += 1; // Display results diff --git a/src/visitor.rs b/src/visitor.rs index 75dcdb12601..7a9c8583039 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -223,7 +223,7 @@ pub fn snippet(&self, span: Span) -> String { println!("Couldn't make snippet for span {:?}->{:?}", self.codemap.lookup_char_pos(span.lo), self.codemap.lookup_char_pos(span.hi)); - "".to_string() + "".to_owned() } } } -- 2.44.0