Cargo fmt

This commit is contained in:
topecongiro 2017-11-02 21:45:00 +09:00
parent 960fec898b
commit b17de6228f
8 changed files with 65 additions and 46 deletions

View File

@ -133,8 +133,10 @@ fn format_crate(
let files: Vec<_> = targets
.into_iter()
.filter(|t| t.kind.should_format())
.inspect(|t| if verbosity == Verbosity::Verbose {
println!("[{:?}] {:?}", t.kind, t.path)
.inspect(|t| {
if verbosity == Verbosity::Verbose {
println!("[{:?}] {:?}", t.kind, t.path)
}
})
.map(|t| t.path)
.collect();

View File

@ -297,10 +297,12 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
// True if the chain is only `?`s.
fn chain_only_try(exprs: &[ast::Expr]) -> bool {
exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node {
true
} else {
false
exprs.iter().all(|e| {
if let ast::ExprKind::Try(_) = e.node {
true
} else {
false
}
})
}

View File

@ -311,10 +311,12 @@ fn rewrite_comment_inner(
line
})
.map(|s| left_trim_comment_line(s, &style))
.map(|line| if orig.starts_with("/*") && line_breaks == 0 {
line.trim_left()
} else {
line
.map(|line| {
if orig.starts_with("/*") && line_breaks == 0 {
line.trim_left()
} else {
line
}
});
let mut result = opener.to_owned();

View File

@ -1198,12 +1198,13 @@ fn rewrite_cond(
context
.codemap
.span_after(mk_sp(lo, self.span.hi()), self.keyword.trim()),
self.pat
.map_or(cond_span.lo(), |p| if self.matcher.is_empty() {
self.pat.map_or(cond_span.lo(), |p| {
if self.matcher.is_empty() {
p.span.lo()
} else {
context.codemap.span_before(self.span, self.matcher.trim())
}),
}
}),
);
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
@ -2753,13 +2754,17 @@ fn rewrite_tuple_in_visual_indent_style<'a, T>(
if items.len() == 1 {
// 3 = "(" + ",)"
let nested_shape = shape.sub_width(3)?.visual_indent(1);
return items.next().unwrap().rewrite(context, nested_shape).map(
|s| if context.config.spaces_within_parens() {
format!("( {}, )", s)
} else {
format!("({},)", s)
},
);
return items
.next()
.unwrap()
.rewrite(context, nested_shape)
.map(|s| {
if context.config.spaces_within_parens() {
format!("( {}, )", s)
} else {
format!("({},)", s)
}
});
}
let list_lo = context.codemap.span_after(span, "(");

View File

@ -670,10 +670,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
ast::TyKind::Paren(ref ty) => {
let budget = shape.width.checked_sub(2)?;
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
.map(|ty_str| if context.config.spaces_within_parens() {
format!("( {} )", ty_str)
} else {
format!("({})", ty_str)
.map(|ty_str| {
if context.config.spaces_within_parens() {
format!("( {} )", ty_str)
} else {
format!("({})", ty_str)
}
})
}
ast::TyKind::Slice(ref ty) => {
@ -683,10 +685,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
shape.width.checked_sub(2)?
};
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
.map(|ty_str| if context.config.spaces_within_square_brackets() {
format!("[ {} ]", ty_str)
} else {
format!("[{}]", ty_str)
.map(|ty_str| {
if context.config.spaces_within_square_brackets() {
format!("[ {} ]", ty_str)
} else {
format!("[{}]", ty_str)
}
})
}
ast::TyKind::Tup(ref items) => rewrite_tuple(

View File

@ -191,13 +191,13 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
fields
.iter()
.map(|field| {
field
.rewrite_prefix(context, shape)
.and_then(|field_str| if field_str.contains('\n') {
field.rewrite_prefix(context, shape).and_then(|field_str| {
if field_str.contains('\n') {
None
} else {
Some(field_str.len())
})
}
})
})
.fold(Some((0, ::std::usize::MAX)), |acc, len| match (acc, len) {
(Some((max_len, min_len)), Some(len)) => {

View File

@ -136,13 +136,15 @@ pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribu
self.last_pos,
attr_lo.unwrap_or(first_stmt.span.lo()),
));
let len = CommentCodeSlices::new(&snippet).nth(0).and_then(
|(kind, _, s)| if kind == CodeCharKind::Normal {
s.rfind('\n')
} else {
None
},
);
let len = CommentCodeSlices::new(&snippet)
.nth(0)
.and_then(|(kind, _, s)| {
if kind == CodeCharKind::Normal {
s.rfind('\n')
} else {
None
}
});
if let Some(len) = len {
self.last_pos = self.last_pos + BytePos::from_usize(len);
}

View File

@ -446,15 +446,17 @@ fn string_eq_ignore_newline_repr(left: &str, right: &str) -> bool {
impl<'a> Iterator for CharsIgnoreNewlineRepr<'a> {
type Item = char;
fn next(&mut self) -> Option<char> {
self.0.next().map(|c| if c == '\r' {
if *self.0.peek().unwrap_or(&'\0') == '\n' {
self.0.next();
'\n'
self.0.next().map(|c| {
if c == '\r' {
if *self.0.peek().unwrap_or(&'\0') == '\n' {
self.0.next();
'\n'
} else {
'\r'
}
} else {
'\r'
c
}
} else {
c
})
}
}