From ed46a777c8f2fdd54811f8af1660fd55f7869d05 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 16:20:46 +0900 Subject: [PATCH 1/9] Use str::repeat --- src/chains.rs | 10 +++------- src/lib.rs | 9 ++++----- src/missed_spans.rs | 3 +-- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index d798981415e8..0e674f771087 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -104,7 +104,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - }; let parent_rewrite = parent .rewrite(context, parent_shape) - .map(|parent_rw| parent_rw + &repeat_try(prefix_try_num))?; + .map(|parent_rw| parent_rw + &"?".repeat(prefix_try_num))?; let parent_rewrite_contains_newline = parent_rewrite.contains('\n'); let is_small_parent = parent_rewrite.len() <= context.config.tab_spaces(); @@ -297,7 +297,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - join_rewrites(&rewrites, &connector) ) }; - let result = format!("{}{}", result, repeat_try(suffix_try_num)); + let result = format!("{}{}", result, "?".repeat(suffix_try_num)); if context.config.indent_style() == IndentStyle::Visual { wrap_str(result, context.config.max_width(), shape) } else { @@ -318,10 +318,6 @@ fn chain_only_try(exprs: &[ast::Expr]) -> bool { // Try to rewrite and replace the last non-try child. Return `true` if // replacing succeeds. -fn repeat_try(try_count: usize) -> String { - iter::repeat("?").take(try_count).collect::() -} - fn rewrite_try( expr: &ast::Expr, try_count: usize, @@ -329,7 +325,7 @@ fn rewrite_try( shape: Shape, ) -> Option { let sub_expr = expr.rewrite(context, shape.sub_width(try_count)?)?; - Some(format!("{}{}", sub_expr, repeat_try(try_count))) + Some(format!("{}{}", sub_expr, "?".repeat(try_count))) } fn join_rewrites(rewrites: &[String], connector: &str) -> String { diff --git a/src/lib.rs b/src/lib.rs index 77589038c669..151b3acada27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,6 @@ extern crate unicode_segmentation; use std::collections::HashMap; use std::fmt; use std::io::{self, stdout, BufRead, Write}; -use std::iter::repeat; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::path::PathBuf; use std::rc::Rc; @@ -200,7 +199,7 @@ impl FormatReport { for (file, errors) in &self.file_error_map { for error in errors { let prefix_space_len = error.line.to_string().len(); - let prefix_spaces: String = repeat(" ").take(1 + prefix_space_len).collect(); + let prefix_spaces = " ".repeat(1 + prefix_space_len); // First line: the overview of error t.fg(term::color::RED)?; @@ -259,8 +258,8 @@ impl FormatReport { } fn target_str(space_len: usize, target_len: usize) -> String { - let empty_line: String = repeat(" ").take(space_len).collect(); - let overflowed: String = repeat("^").take(target_len).collect(); + let empty_line = " ".repeat(space_len); + let overflowed = "^".repeat(target_len); empty_line + &overflowed } @@ -270,7 +269,7 @@ impl fmt::Display for FormatReport { for (file, errors) in &self.file_error_map { for error in errors { let prefix_space_len = error.line.to_string().len(); - let prefix_spaces: String = repeat(" ").take(1 + prefix_space_len).collect(); + let prefix_spaces = " ".repeat(1 + prefix_space_len); let error_line_buffer = if error.line_buffer.is_empty() { String::from(" ") diff --git a/src/missed_spans.rs b/src/missed_spans.rs index f5794c65c4c4..5140ac35be99 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -9,7 +9,6 @@ // except according to those terms. use std::borrow::Cow; -use std::iter::repeat; use syntax::codemap::{BytePos, FileName, Pos, Span}; @@ -122,7 +121,7 @@ impl<'a> FmtVisitor<'a> { } } - let blank_lines: String = repeat('\n').take(newline_count).collect(); + let blank_lines = "\n".repeat(newline_count); self.push_str(&blank_lines); } From 6b3811a3587cc9d9d0734c44e24b969319a7fab5 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 18:43:52 +0900 Subject: [PATCH 2/9] Use Iterator methods --- src/config/options.rs | 8 +------- src/git-rustfmt/main.rs | 20 ++------------------ 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/config/options.rs b/src/config/options.rs index 31f7d106d1a3..eca206a75fe6 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -271,13 +271,7 @@ impl IgnoreList { } fn skip_file_inner(&self, file: &Path) -> bool { - for path in &self.0 { - if file.starts_with(path) { - return true; - } - } - - false + self.0.iter().any(|path| file.starts_with(path)) } pub fn skip_file(&self, file: &FileName) -> bool { diff --git a/src/git-rustfmt/main.rs b/src/git-rustfmt/main.rs index 41e4ee2931d4..3ff9455d5f79 100644 --- a/src/git-rustfmt/main.rs +++ b/src/git-rustfmt/main.rs @@ -32,16 +32,7 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> { let mut pruned_prefixes = vec![]; for p1 in prefixes { - let mut include = true; - if !p1.starts_with("src/bin/") { - for p2 in &pruned_prefixes { - if p1.starts_with(p2) { - include = false; - break; - } - } - } - if include { + if p1.starts_with("src/bin/") || pruned_prefixes.iter().all(|p2| !p1.starts_with(p2)) { pruned_prefixes.push(p1); } } @@ -50,17 +41,10 @@ fn prune_files(files: Vec<&str>) -> Vec<&str> { files .into_iter() .filter(|f| { - let mut include = true; if f.ends_with("mod.rs") || f.ends_with("lib.rs") || f.starts_with("src/bin/") { return true; } - for pp in &pruned_prefixes { - if f.starts_with(pp) { - include = false; - break; - } - } - include + pruned_prefixes.iter().all(|pp| !f.starts_with(pp)) }) .collect() } From 89200f40ff7c0b4e896dd9d87ab2655c623b196e Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 18:12:50 +0900 Subject: [PATCH 3/9] Remove unnecessary `[..]`s --- src/chains.rs | 2 +- src/items.rs | 4 ++-- src/macros.rs | 6 +++--- src/patterns.rs | 2 +- src/types.rs | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 0e674f771087..8326f735e21f 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -336,7 +336,7 @@ fn join_rewrites(rewrites: &[String], connector: &str) -> String { if rewrite != "?" { result.push_str(connector); } - result.push_str(&rewrite[..]); + result.push_str(&rewrite); } result diff --git a/src/items.rs b/src/items.rs index 377d777c4099..be21e1fd7875 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1287,7 +1287,7 @@ fn format_tuple_struct( result.push(')'); } else { let shape = Shape::indented(offset, context.config).sub_width(1)?; - let fields = &fields.iter().collect::>()[..]; + let fields = &fields.iter().collect::>(); result = overflow::rewrite_with_parens( context, &result, @@ -2312,7 +2312,7 @@ fn rewrite_generics( return Some(ident.to_owned()); } - let params = &generics.params.iter().map(|e| &*e).collect::>()[..]; + let params = &generics.params.iter().map(|e| &*e).collect::>(); overflow::rewrite_with_angle_brackets(context, ident, params, shape, span) } diff --git a/src/macros.rs b/src/macros.rs index c5671911936c..aded2508aaa3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -239,7 +239,7 @@ pub fn rewrite_macro_inner( overflow::rewrite_with_parens( context, ¯o_name, - &arg_vec.iter().map(|e| &*e).collect::>()[..], + &arg_vec.iter().map(|e| &*e).collect::>(), shape, mac.span, context.config.width_heuristics().fn_call_width, @@ -301,7 +301,7 @@ pub fn rewrite_macro_inner( }; } // Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter. - let arg_vec = &arg_vec.iter().map(|e| &*e).collect::>()[..]; + let arg_vec = &arg_vec.iter().map(|e| &*e).collect::>(); let rewrite = rewrite_array( macro_name, arg_vec, @@ -991,7 +991,7 @@ fn next_space(tok: &Token) -> SpaceState { /// when the macro is not an instance of try! (or parsing the inner expression /// failed). pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option { - if &format!("{}", mac.node.path)[..] == "try" { + if &format!("{}", mac.node.path) == "try" { let ts: TokenStream = mac.node.tts.clone().into(); let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect()); diff --git a/src/patterns.rs b/src/patterns.rs index 44e32462a8d4..9923eea418a4 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -369,7 +369,7 @@ fn rewrite_tuple_pat( overflow::rewrite_with_parens( &context, &path_str, - &pat_ref_vec[..], + &pat_ref_vec, shape, span, context.config.max_width(), diff --git a/src/types.rs b/src/types.rs index 676095ff4ce4..5be963481294 100644 --- a/src/types.rs +++ b/src/types.rs @@ -247,7 +247,7 @@ fn rewrite_segment( let generics_str = overflow::rewrite_with_angle_brackets( context, "", - ¶m_list.iter().map(|e| &*e).collect::>()[..], + ¶m_list.iter().map(|e| &*e).collect::>(), shape, mk_sp(*span_lo, span_hi), )?; From a2325375ed2a18af8b41e9d5147646511c582aab Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 19:35:00 +0900 Subject: [PATCH 4/9] Do not collect into a Vec by hand --- src/patterns.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/patterns.rs b/src/patterns.rs index 9923eea418a4..53051c71ba71 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -361,10 +361,7 @@ fn rewrite_tuple_pat( // add comma if `(x,)` let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none(); let path_str = path_str.unwrap_or_default(); - let mut pat_ref_vec = Vec::with_capacity(pat_vec.len()); - for pat in pat_vec { - pat_ref_vec.push(pat); - } + let pat_ref_vec = pat_vec.iter().collect::>(); overflow::rewrite_with_parens( &context, From 39e85281f33f40742041fd8e1d8ef1cd7fa760b1 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 21:10:31 +0900 Subject: [PATCH 5/9] Remove stray comment Its pointee was removed in 04a6d16c7b68587f8e9fa8a1e6715f80408b023b --- src/chains.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index 8326f735e21f..6c3b6d1033dd 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -316,8 +316,6 @@ fn chain_only_try(exprs: &[ast::Expr]) -> bool { }) } -// Try to rewrite and replace the last non-try child. Return `true` if -// replacing succeeds. fn rewrite_try( expr: &ast::Expr, try_count: usize, From 71448ff3c25db6cba8dc40191416e9e1490e7964 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 21:21:52 +0900 Subject: [PATCH 6/9] Return String instead of always returing Cow::Owned --- src/missed_spans.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 5140ac35be99..db4f01fa6887 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -175,7 +175,7 @@ impl<'a> FmtVisitor<'a> { let mut status = SnippetStatus::new(char_pos.line); let snippet = &*match self.config.write_mode() { - WriteMode::Coverage => replace_chars(old_snippet), + WriteMode::Coverage => Cow::from(replace_chars(old_snippet)), _ => Cow::from(old_snippet), }; @@ -320,11 +320,9 @@ impl<'a> FmtVisitor<'a> { } } -fn replace_chars(string: &str) -> Cow { - Cow::from( - string - .chars() - .map(|ch| if ch.is_whitespace() { ch } else { 'X' }) - .collect::(), - ) +fn replace_chars(string: &str) -> String { + string + .chars() + .map(|ch| if ch.is_whitespace() { ch } else { 'X' }) + .collect() } From ba792a7fa2410fdb488ffa90ef6aa69269ee38c6 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 21:35:51 +0900 Subject: [PATCH 7/9] Remove redudant parens --- src/visitor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/visitor.rs b/src/visitor.rs index 22f892d4fb4a..d15d2ad7aefb 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -335,7 +335,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let snippet = self.snippet(item.span); let where_span_end = snippet .find_uncommented("{") - .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo()); + .map(|x| BytePos(x as u32) + source!(self, item.span).lo()); let rw = format_impl(&self.get_context(), item, self.block_indent, where_span_end); self.push_rewrite(item.span, rw); } From 3467b4dafebc6e9a52d1cf7051abc057e23defe7 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 21:43:01 +0900 Subject: [PATCH 8/9] Use ListItem::has_comment --- src/patterns.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/patterns.rs b/src/patterns.rs index 53051c71ba71..bbfe55ec6e99 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -405,7 +405,7 @@ fn count_wildcard_suffix_len( }) { suffix_len += 1; - if item.pre_comment.is_some() || item.post_comment.is_some() { + if item.has_comment() { break; } } From 56e10aa6e9aca94a61ff2eeaee6416ee374115cd Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 1 Apr 2018 21:47:55 +0900 Subject: [PATCH 9/9] Fix typo --- src/items.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/items.rs b/src/items.rs index be21e1fd7875..621b2a2074db 100644 --- a/src/items.rs +++ b/src/items.rs @@ -204,7 +204,7 @@ impl<'a> FnSig<'a> { fn_kind: &'a visit::FnKind, generics: &'a ast::Generics, decl: &'a ast::FnDecl, - defualtness: ast::Defaultness, + defaultness: ast::Defaultness, ) -> FnSig<'a> { match *fn_kind { visit::FnKind::ItemFn(_, unsafety, constness, abi, visibility, _) => FnSig { @@ -212,13 +212,13 @@ impl<'a> FnSig<'a> { generics, abi, constness: constness.node, - defaultness: defualtness, + defaultness, unsafety, visibility: visibility.clone(), }, visit::FnKind::Method(_, method_sig, vis, _) => { let mut fn_sig = FnSig::from_method_sig(method_sig, generics); - fn_sig.defaultness = defualtness; + fn_sig.defaultness = defaultness; if let Some(vis) = vis { fn_sig.visibility = vis.clone(); }