diff --git a/src/chains.rs b/src/chains.rs index d798981415e8..6c3b6d1033dd 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 { @@ -316,12 +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 repeat_try(try_count: usize) -> String { - iter::repeat("?").take(try_count).collect::() -} - fn rewrite_try( expr: &ast::Expr, try_count: usize, @@ -329,7 +323,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 { @@ -340,7 +334,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/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() } diff --git a/src/items.rs b/src/items.rs index 377d777c4099..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(); } @@ -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/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/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/missed_spans.rs b/src/missed_spans.rs index 20cd5e6dca1d..b27ef9b87697 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}; @@ -128,7 +127,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); } @@ -182,7 +181,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), }; @@ -327,11 +326,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() } diff --git a/src/patterns.rs b/src/patterns.rs index 44e32462a8d4..bbfe55ec6e99 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -361,15 +361,12 @@ 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, &path_str, - &pat_ref_vec[..], + &pat_ref_vec, shape, span, context.config.max_width(), @@ -408,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; } } 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), )?; 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); }