]> git.lizzy.rs Git - rust.git/commitdiff
Format source codes
authorSeiichi Uchida <seuchida@gmail.com>
Sun, 9 Jul 2017 17:24:59 +0000 (02:24 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Sun, 9 Jul 2017 17:24:59 +0000 (02:24 +0900)
src/bin/cargo-fmt.rs
src/chains.rs
src/comment.rs
src/config.rs
src/expr.rs
src/items.rs
src/lib.rs
src/patterns.rs
src/types.rs
src/utils.rs
src/visitor.rs

index 95ac1a25bb9b2563654329e54e514b891ddc7c76..9a7c9680a5c2c54c5e48468d6183755fc15ad871 100644 (file)
@@ -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,
         }
     }
index e4ad0d82fd485b84edd9a8aa69a863dfbf4ee61c..9bce9c792f45038411b0db76088d684d9d174422 100644 (file)
@@ -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(..) |
index d8189df363cbd0597f040f52a0ce1c192bf5cf63..123df4927f41e14efbad75ac6e230c12df9e2329 100644 (file)
@@ -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
index 5b2f77ca668a48f04209b91bfa04c4f15cd453e3..fa72e2b85d56982e5499063cb065a9a1f1b7f418 100644 (file)
@@ -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,
         }
     }
index 3b75f6c93dbeaf60010c005ef429cd5f38545a87..b214a42a3dbf9906ac6d37ba5b3244e1a07a85a8 100644 (file)
@@ -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<String> {
         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<String> {
                     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<String> {
             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<String> {
                 }
             }
             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
         }
index 91bba5349249e11483d5e3ae3c5c942704d1adf0..87299f01574719463f934ed41bb01ca78aa551cd 100644 (file)
@@ -517,8 +517,7 @@ fn format_variant(&self, field: &ast::Variant) -> Option<String> {
         }
 
         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 == "=" {
index b27a02a17a905ead7c012d8238bd1ac66ee9e0da..a8bade234421027c815084eed72698e1332afc9d 100644 (file)
@@ -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(_) => "",
         }
     }
index cda7a108609e5201db3c9f285897a763c990556b..2fce2b7ccadcc82ce0f7bb043bc4f6c30f556410 100644 (file)
@@ -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),
index 8ba0d64de9e3c9d77e0a3407f9134ce81fbb5856..b617c433f106699b986ee1b3b2f86591f3630157 100644 (file)
@@ -758,8 +758,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 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<Strin
 
 pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize) -> 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,
     }
 }
index bc46316d39b8d364436d0e0a66036f5e5328cdf7..7873cd5d846d1642d4178cdd4315404ea8eab889 100644 (file)
@@ -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,
     }
 }
index 0f574d433fd054b47b8c4e59abf80c21740f3a7f..178f7c7691f0e2d521831a6aca4b07d05cce5e45 100644 (file)
@@ -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;
                 }