Merge pull request #2584 from sinkuu/cleanup

Misc cleanups
This commit is contained in:
Seiichi Uchida 2018-04-01 23:14:52 +09:00 committed by GitHub
commit dec307b2fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 31 additions and 66 deletions

View File

@ -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::<String>()
}
fn rewrite_try(
expr: &ast::Expr,
try_count: usize,
@ -329,7 +323,7 @@ fn rewrite_try(
shape: Shape,
) -> Option<String> {
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

View File

@ -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 {

View File

@ -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()
}

View File

@ -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::<Vec<_>>()[..];
let fields = &fields.iter().collect::<Vec<_>>();
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::<Vec<_>>()[..];
let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
overflow::rewrite_with_angle_brackets(context, ident, params, shape, span)
}

View File

@ -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(" ")

View File

@ -239,7 +239,7 @@ pub fn rewrite_macro_inner(
overflow::rewrite_with_parens(
context,
&macro_name,
&arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..],
&arg_vec.iter().map(|e| &*e).collect::<Vec<_>>(),
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::<Vec<_>>()[..];
let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>();
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<ast::Expr> {
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());

View File

@ -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<str> {
Cow::from(
string
.chars()
.map(|ch| if ch.is_whitespace() { ch } else { 'X' })
.collect::<String>(),
)
fn replace_chars(string: &str) -> String {
string
.chars()
.map(|ch| if ch.is_whitespace() { ch } else { 'X' })
.collect()
}

View File

@ -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::<Vec<_>>();
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;
}
}

View File

@ -247,7 +247,7 @@ fn rewrite_segment(
let generics_str = overflow::rewrite_with_angle_brackets(
context,
"",
&param_list.iter().map(|e| &*e).collect::<Vec<_>>()[..],
&param_list.iter().map(|e| &*e).collect::<Vec<_>>(),
shape,
mk_sp(*span_lo, span_hi),
)?;

View File

@ -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);
}