Format source codes

This commit is contained in:
topecongiro 2017-05-16 23:24:38 +09:00
parent feff6248c4
commit d7b6f1199f
10 changed files with 189 additions and 215 deletions

View File

@ -249,9 +249,7 @@ 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 {
exprs.iter().all(|e| if let ast::ExprKind::Try(_) = e.node {
true
} else {
false

View File

@ -1028,10 +1028,9 @@ impl<'a> Rewrite for ControlFlow<'a> {
let between_kwd_else_block_comment =
extract_comment(between_kwd_else_block, context, shape);
let after_else =
mk_sp(context
.codemap
.span_after(mk_sp(self.block.span.hi, else_block.span.lo), "else"),
let after_else = mk_sp(context.codemap.span_after(mk_sp(self.block.span.hi,
else_block.span.lo),
"else"),
else_block.span.lo);
let after_else_comment = extract_comment(after_else, context, shape);

View File

@ -457,10 +457,7 @@ impl<'a> FmtVisitor<'a> {
}
let indent = self.block_indent;
let mut result = try_opt!(field
.node
.attrs
.rewrite(&self.get_context(),
let mut result = try_opt!(field.node.attrs.rewrite(&self.get_context(),
Shape::indented(indent, self.config)));
if !result.is_empty() {
result.push('\n');
@ -1201,9 +1198,9 @@ impl Rewrite for ast::StructField {
let name = self.ident;
let vis = format_visibility(&self.vis);
let mut attr_str =
try_opt!(self.attrs
.rewrite(context, Shape::indented(shape.indent, context.config)));
let mut attr_str = try_opt!(self.attrs.rewrite(context,
Shape::indented(shape.indent,
context.config)));
if !attr_str.is_empty() {
attr_str.push('\n');
attr_str.push_str(&shape.indent.to_string(context.config));
@ -1223,8 +1220,8 @@ impl Rewrite for ast::StructField {
let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
let budget = try_opt!(shape.width.checked_sub(last_line_width));
let ty_rewritten = self.ty
.rewrite(context,
let ty_rewritten =
self.ty.rewrite(context,
Shape::legacy(budget, shape.indent + last_line_width));
match ty_rewritten {
Some(ref ty) if ty.contains('\n') => {
@ -1377,8 +1374,7 @@ impl Rewrite for ast::FunctionRetTy {
impl Rewrite for ast::Arg {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
if is_named_arg(self) {
let mut result = try_opt!(self.pat
.rewrite(context,
let mut result = try_opt!(self.pat.rewrite(context,
Shape::legacy(shape.width, shape.indent)));
if self.ty.node != ast::TyKind::Infer {
@ -1390,8 +1386,7 @@ impl Rewrite for ast::Arg {
result.push_str(" ");
}
let max_width = try_opt!(shape.width.checked_sub(result.len()));
let ty_str = try_opt!(self.ty
.rewrite(context,
let ty_str = try_opt!(self.ty.rewrite(context,
Shape::legacy(max_width,
shape.indent + result.len())));
result.push_str(&ty_str);
@ -1693,8 +1688,7 @@ fn rewrite_fn_base(context: &RewriteContext,
if multi_line_ret_str || ret_should_indent {
// Now that we know the proper indent and width, we need to
// re-layout the return type.
let ret_str = try_opt!(fd.output
.rewrite(context,
let ret_str = try_opt!(fd.output.rewrite(context,
Shape::indented(ret_indent, context.config)));
result.push_str(&ret_str);
} else {
@ -1985,9 +1979,7 @@ fn rewrite_generics(context: &RewriteContext,
.map(|ty_param| ty_param.rewrite(context, Shape::legacy(h_budget, offset)));
// Extract comments between generics.
let lt_spans = lifetimes
.iter()
.map(|l| {
let lt_spans = lifetimes.iter().map(|l| {
let hi = if l.bounds.is_empty() {
l.lifetime.span.hi
} else {

View File

@ -339,9 +339,7 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3>
fn next(&mut self) -> Option<Self::Item> {
let white_space: &[_] = &[' ', '\t'];
self.inner
.next()
.map(|item| {
self.inner.next().map(|item| {
let mut new_lines = false;
// Pre-comment
let pre_snippet = self.codemap

View File

@ -159,12 +159,10 @@ pub fn rewrite_macro(mac: &ast::Mac,
// Format macro invocation as array literal.
let extra_offset = macro_name.len();
let shape = try_opt!(shape.shrink_left(extra_offset));
let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
mk_sp(context
.codemap
.span_after(mac.span,
original_style
.opener()),
let rewrite =
try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
mk_sp(context.codemap.span_after(mac.span,
original_style.opener()),
mac.span.hi - BytePos(1)),
context,
shape));

View File

@ -158,8 +158,7 @@ impl<'a> FmtVisitor<'a> {
self.config.max_width -
self.block_indent.width());
self.buffer
.push_str(&rewrite_comment(subslice,
self.buffer.push_str(&rewrite_comment(subslice,
false,
Shape::legacy(comment_width,
self.block_indent),

View File

@ -39,9 +39,8 @@ impl Rewrite for Pat {
let sub_pat = match *sub_pat {
Some(ref p) => {
// 3 - ` @ `.
let width = try_opt!(shape
.width
.checked_sub(prefix.len() + mut_infix.len() +
let width = try_opt!(shape.width.checked_sub(prefix.len() +
mut_infix.len() +
id_str.len() +
3));
format!(" @ {}",
@ -328,11 +327,9 @@ fn rewrite_tuple_pat(pats: &[ptr::P<ast::Pat>],
fn count_wildcard_suffix_len(items: &[ListItem]) -> usize {
let mut suffix_len = 0;
for item in items
.iter()
.rev()
.take_while(|i| match i.item {
Some(ref internal_string) if internal_string == "_" => true,
for item in items.iter().rev().take_while(|i| match i.item {
Some(ref internal_string) if internal_string ==
"_" => true,
_ => false,
}) {
suffix_len += 1;

View File

@ -163,11 +163,10 @@ impl<'a> Rewrite for SegmentParam<'a> {
TypeDensity::Compressed => format!("{}=", binding.ident),
};
let budget = try_opt!(shape.width.checked_sub(result.len()));
let rewrite =
try_opt!(binding
.ty
.rewrite(context,
Shape::legacy(budget, shape.indent + result.len())));
let rewrite = try_opt!(binding.ty.rewrite(context,
Shape::legacy(budget,
shape.indent +
result.len())));
result.push_str(&rewrite);
Some(result)
}
@ -563,10 +562,10 @@ impl Rewrite for ast::PolyTraitRef {
// 6 is "for<> ".len()
let extra_offset = lifetime_str.len() + 6;
let max_path_width = try_opt!(shape.width.checked_sub(extra_offset));
let path_str = try_opt!(self.trait_ref
.rewrite(context,
let path_str = try_opt!(self.trait_ref.rewrite(context,
Shape::legacy(max_path_width,
shape.indent + extra_offset)));
shape.indent +
extra_offset)));
Some(if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
format!("for< {} > {}", lifetime_str, path_str)
@ -613,8 +612,7 @@ impl Rewrite for ast::Ty {
format!("&{} {}{}",
lt_str,
mut_str,
try_opt!(mt.ty
.rewrite(context,
try_opt!(mt.ty.rewrite(context,
Shape::legacy(budget,
shape.indent + 2 +
mut_len +
@ -624,8 +622,7 @@ impl Rewrite for ast::Ty {
let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
format!("&{}{}",
mut_str,
try_opt!(mt.ty
.rewrite(context,
try_opt!(mt.ty.rewrite(context,
Shape::legacy(budget,
shape.indent + 1 +
mut_len))))

View File

@ -303,10 +303,7 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
.map(|line| line.expect("Failed getting line"))
.take_while(|line| line_regex.is_match(&line))
.filter_map(|line| {
regex
.captures_iter(&line)
.next()
.map(|capture| {
regex.captures_iter(&line).next().map(|capture| {
(capture
.get(1)
.expect("Couldn't unwrap capture")

View File

@ -29,8 +29,7 @@ fn b() {
}
fn issue550() {
self.visitor
.visit_volume(self.level.sector_id(sector),
self.visitor.visit_volume(self.level.sector_id(sector),
(floor_y,
if is_sky_flat(ceil_tex) {
from_wad_height(self.height_range.1)