From: Seiichi Uchida Date: Sun, 9 Jul 2017 17:24:59 +0000 (+0900) Subject: Format source codes X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=bc63d69c3fde04404693aa685339ed9cac2dd0a4;p=rust.git Format source codes --- diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index 95ac1a25bb9..9a7c9680a5c 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -155,8 +155,13 @@ enum TargetKind { impl TargetKind { fn should_format(&self) -> bool { match *self { - TargetKind::Lib | TargetKind::Bin | TargetKind::Example | TargetKind::Test | - TargetKind::Bench | TargetKind::CustomBuild | TargetKind::ProcMacro => true, + TargetKind::Lib | + TargetKind::Bin | + TargetKind::Example | + TargetKind::Test | + TargetKind::Bench | + TargetKind::CustomBuild | + TargetKind::ProcMacro => true, _ => false, } } diff --git a/src/chains.rs b/src/chains.rs index e4ad0d82fd4..9bce9c792f4 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -390,8 +390,9 @@ fn join_rewrites(rewrites: &[String], subexps: &[ast::Expr], connector: &str) -> // parens, braces, and brackets in its idiomatic formatting. fn is_block_expr(context: &RewriteContext, expr: &ast::Expr, repr: &str) -> bool { match expr.node { - ast::ExprKind::Mac(..) | - ast::ExprKind::Call(..) => context.use_block_indent() && repr.contains('\n'), + ast::ExprKind::Mac(..) | ast::ExprKind::Call(..) => { + context.use_block_indent() && repr.contains('\n') + } ast::ExprKind::Struct(..) | ast::ExprKind::While(..) | ast::ExprKind::WhileLet(..) | diff --git a/src/comment.rs b/src/comment.rs index d8189df363c..123df4927f4 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -71,8 +71,7 @@ pub fn closer(&self) -> &'a str { CommentStyle::Custom(..) | CommentStyle::Doc => "", CommentStyle::DoubleBullet => " **/", - CommentStyle::SingleBullet | - CommentStyle::Exclamation => " */", + CommentStyle::SingleBullet | CommentStyle::Exclamation => " */", } } @@ -81,8 +80,7 @@ pub fn line_start(&self) -> &'a str { CommentStyle::DoubleSlash => "// ", CommentStyle::TripleSlash => "/// ", CommentStyle::Doc => "//! ", - CommentStyle::SingleBullet | - CommentStyle::Exclamation => " * ", + CommentStyle::SingleBullet | CommentStyle::Exclamation => " * ", CommentStyle::DoubleBullet => " ** ", CommentStyle::Custom(opener) => opener, } @@ -94,15 +92,11 @@ pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) { pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool { match *self { - CommentStyle::DoubleSlash | - CommentStyle::TripleSlash | - CommentStyle::Doc => { + CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => { line.trim_left().starts_with(self.line_start().trim_left()) || comment_style(line, normalize_comments) == *self } - CommentStyle::DoubleBullet | - CommentStyle::SingleBullet | - CommentStyle::Exclamation => { + CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => { line.trim_left().starts_with(self.closer().trim_left()) || line.trim_left().starts_with(self.line_start().trim_left()) || comment_style(line, normalize_comments) == *self diff --git a/src/config.rs b/src/config.rs index 5b2f77ca668..fa72e2b85d5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -98,8 +98,7 @@ impl Density { pub fn to_list_tactic(self) -> ListTactic { match self { Density::Compressed => ListTactic::Mixed, - Density::Tall | - Density::CompressedIfEmpty => ListTactic::HorizontalVertical, + Density::Tall | Density::CompressedIfEmpty => ListTactic::HorizontalVertical, Density::Vertical => ListTactic::Vertical, } } diff --git a/src/expr.rs b/src/expr.rs index 3b75f6c93db..b214a42a3db 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -315,8 +315,7 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool { } // We do not format these expressions yet, but they should still // satisfy our width restrictions. - ast::ExprKind::InPlace(..) | - ast::ExprKind::InlineAsm(..) => { + ast::ExprKind::InPlace(..) | ast::ExprKind::InlineAsm(..) => { wrap_str( context.snippet(expr.span), context.config.max_width(), @@ -930,8 +929,7 @@ impl Rewrite for ast::Stmt { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { let result = match self.node { ast::StmtKind::Local(ref local) => local.rewrite(context, shape), - ast::StmtKind::Expr(ref ex) | - ast::StmtKind::Semi(ref ex) => { + ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => { let suffix = if semicolon_for_stmt(self) { ";" } else { "" }; format_expr( @@ -945,8 +943,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { try_opt!(shape.sub_width(suffix.len())), ).map(|s| s + suffix) } - ast::StmtKind::Mac(..) | - ast::StmtKind::Item(..) => None, + ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None, }; result.and_then(|res| { recover_comment_removed(res, self.span, context, shape) @@ -1404,8 +1401,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { let after_else_comment = extract_comment(after_else, context, shape); let between_sep = match context.config.control_brace_style() { - ControlBraceStyle::AlwaysNextLine | - ControlBraceStyle::ClosingNextLine => &*alt_block_sep, + ControlBraceStyle::AlwaysNextLine | ControlBraceStyle::ClosingNextLine => { + &*alt_block_sep + } ControlBraceStyle::AlwaysSameLine => " ", }; let after_sep = match context.config.control_brace_style() { @@ -1729,9 +1727,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { } } ast::ExprKind::Call(_, ref args) => (args.len() == 1, &**body), - ast::ExprKind::Closure(..) | - ast::ExprKind::Struct(..) | - ast::ExprKind::Tup(..) => (true, &**body), + ast::ExprKind::Closure(..) | ast::ExprKind::Struct(..) | ast::ExprKind::Tup(..) => ( + true, + &**body, + ), _ => (false, &**body), }; extend &= context.use_block_indent(); @@ -2402,8 +2401,7 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l ast::ExprKind::WhileLet(..) => { context.config.combine_control_expr() && context.use_block_indent() && args_len == 1 } - ast::ExprKind::Block(..) | - ast::ExprKind::Closure(..) => { + ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => { context.use_block_indent() || context.config.fn_call_style() == IndentStyle::Visual && args_len > 1 } diff --git a/src/items.rs b/src/items.rs index 91bba534924..87299f01574 100644 --- a/src/items.rs +++ b/src/items.rs @@ -517,8 +517,7 @@ fn format_variant(&self, field: &ast::Variant) -> Option { } let variant_body = match field.node.data { - ast::VariantData::Tuple(..) | - ast::VariantData::Struct(..) => { + ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => { // FIXME: Should limit the width, as we have a trailing comma format_struct( &context, @@ -732,16 +731,15 @@ fn format_impl_ref_and_type( if let Some(ref trait_ref) = *trait_ref { let result_len = result.len(); - if let Some(trait_ref_str) = - rewrite_trait_ref( - context, - &trait_ref, - offset, - &generics_str, - true, - polarity_str, - result_len, - ) { + if let Some(trait_ref_str) = rewrite_trait_ref( + context, + &trait_ref, + offset, + &generics_str, + true, + polarity_str, + result_len, + ) { result.push_str(&trait_ref_str); } else { let generics_str = try_opt!(rewrite_generics_inner( @@ -2062,18 +2060,17 @@ fn rewrite_fn_base( .max_width() .checked_sub(last_line_width(&result)) ); - if let Some(where_clause_str) = - rewrite_where_clause( - context, - where_clause, - context.config.fn_brace_style(), - Shape::legacy(budget, indent), - Density::Compressed, - "{", - !has_braces, - put_args_in_block && ret_str.is_empty(), - Some(span.hi), - ) { + if let Some(where_clause_str) = rewrite_where_clause( + context, + where_clause, + context.config.fn_brace_style(), + Shape::legacy(budget, indent), + Density::Compressed, + "{", + !has_braces, + put_args_in_block && ret_str.is_empty(), + Some(span.hi), + ) { if !where_clause_str.contains('\n') { if last_line_width(&result) + where_clause_str.len() > context.config.max_width() { result.push('\n'); @@ -2628,8 +2625,7 @@ fn rewrite_where_clause( // If the brace is on the next line we don't need to count it otherwise it needs two // characters " {" match brace_style { - BraceStyle::AlwaysNextLine | - BraceStyle::SameLineWhere => 0, + BraceStyle::AlwaysNextLine | BraceStyle::SameLineWhere => 0, BraceStyle::PreferSameLine => 2, } } else if terminator == "=" { diff --git a/src/lib.rs b/src/lib.rs index b27a02a17a9..a8bade23442 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -407,16 +407,14 @@ pub struct FormattingError { impl FormattingError { fn msg_prefix(&self) -> &str { match self.kind { - ErrorKind::LineOverflow(..) | - ErrorKind::TrailingWhitespace => "Rustfmt failed at", + ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => "Rustfmt failed at", ErrorKind::BadIssue(_) => "WARNING:", } } fn msg_suffix(&self) -> &str { match self.kind { - ErrorKind::LineOverflow(..) | - ErrorKind::TrailingWhitespace => "(sorry)", + ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace => "(sorry)", ErrorKind::BadIssue(_) => "", } } diff --git a/src/patterns.rs b/src/patterns.rs index cda7a108609..2fce2b7ccad 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -252,11 +252,10 @@ pub fn can_be_overflowed_pat(context: &RewriteContext, pat: &TuplePatField, len: match pat { &TuplePatField::Pat(ref pat) => { match pat.node { - ast::PatKind::Path(..) | - ast::PatKind::Tuple(..) | - ast::PatKind::Struct(..) => context.use_block_indent() && len == 1, - ast::PatKind::Ref(ref p, _) | - ast::PatKind::Box(ref p) => { + ast::PatKind::Path(..) | ast::PatKind::Tuple(..) | ast::PatKind::Struct(..) => { + context.use_block_indent() && len == 1 + } + ast::PatKind::Ref(ref p, _) | ast::PatKind::Box(ref p) => { can_be_overflowed_pat(context, &TuplePatField::Pat(p), len) } ast::PatKind::Lit(ref expr) => can_be_overflowed_expr(context, expr, len), diff --git a/src/types.rs b/src/types.rs index 8ba0d64de9e..b617c433f10 100644 --- a/src/types.rs +++ b/src/types.rs @@ -758,8 +758,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { it.rewrite(context, shape) .map(|it_str| format!("impl {}", it_str)) } - ast::TyKind::Err | - ast::TyKind::Typeof(..) => unreachable!(), + ast::TyKind::Err | ast::TyKind::Typeof(..) => unreachable!(), } } } @@ -837,10 +836,10 @@ pub fn join_bounds(context: &RewriteContext, shape: Shape, type_strs: &Vec bool { match ty.node { - ast::TyKind::Path(..) | - ast::TyKind::Tup(..) => context.use_block_indent() && len == 1, - ast::TyKind::Rptr(_, ref mutty) | - ast::TyKind::Ptr(ref mutty) => can_be_overflowed_type(context, &*mutty.ty, len), + ast::TyKind::Path(..) | ast::TyKind::Tup(..) => context.use_block_indent() && len == 1, + ast::TyKind::Rptr(_, ref mutty) | ast::TyKind::Ptr(ref mutty) => { + can_be_overflowed_type(context, &*mutty.ty, len) + } _ => false, } } diff --git a/src/utils.rs b/src/utils.rs index bc46316d39b..7873cd5d846 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -160,9 +160,7 @@ pub fn end_typaram(typaram: &ast::TyParam) -> BytePos { #[inline] pub fn semicolon_for_expr(expr: &ast::Expr) -> bool { match expr.node { - ast::ExprKind::Ret(..) | - ast::ExprKind::Continue(..) | - ast::ExprKind::Break(..) => true, + ast::ExprKind::Ret(..) | ast::ExprKind::Continue(..) | ast::ExprKind::Break(..) => true, _ => false, } } diff --git a/src/visitor.rs b/src/visitor.rs index 0f574d433fd..178f7c7691f 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -307,23 +307,18 @@ pub fn visit_item(&mut self, item: &ast::Item) { let where_span_end = snippet .find_uncommented("{") .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo); - if let Some(impl_str) = format_impl( - &self.get_context(), - item, - self.block_indent, - where_span_end, - ) { + if let Some(impl_str) = + format_impl(&self.get_context(), item, self.block_indent, where_span_end) + { self.buffer.push_str(&impl_str); self.last_pos = source!(self, item.span).hi; } } ast::ItemKind::Trait(..) => { self.format_missing_with_indent(item.span.lo); - if let Some(trait_str) = format_trait( - &self.get_context(), - item, - self.block_indent, - ) { + if let Some(trait_str) = + format_trait(&self.get_context(), item, self.block_indent) + { self.buffer.push_str(&trait_str); self.last_pos = source!(self, item.span).hi; }