]> git.lizzy.rs Git - rust.git/commitdiff
Format source codes
authortopecongiro <seuchida@gmail.com>
Tue, 16 May 2017 14:24:38 +0000 (23:24 +0900)
committertopecongiro <seuchida@gmail.com>
Tue, 16 May 2017 14:24:38 +0000 (23:24 +0900)
src/chains.rs
src/expr.rs
src/items.rs
src/lists.rs
src/macros.rs
src/missed_spans.rs
src/patterns.rs
src/types.rs
tests/system.rs
tests/target/tuple.rs

index 4ffb297e46109c3ed528590130e2f700e14fd647..3c5f174a0c230c490005978664d15465f9d42930 100644 (file)
@@ -249,13 +249,11 @@ 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
+                     })
 }
 
 pub fn rewrite_try(expr: &ast::Expr,
index 7fb71c2e8b42e0588138db2be6868287ab78d0d7..dfe519782e1aa2a28f9701b2373a066ff7f2975b 100644 (file)
@@ -1028,11 +1028,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             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"),
-                      else_block.span.lo);
+            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);
 
             let between_sep = match context.config.control_brace_style {
index 5eb1414c2e068428b09db9c80112c7671e210acc..b41b6166ecaf5a8917b1e9b17f9ad6d04a3fae31 100644 (file)
@@ -457,11 +457,8 @@ fn format_variant(&self, field: &ast::Variant) -> Option<String> {
         }
 
         let indent = self.block_indent;
-        let mut result = try_opt!(field
-                                      .node
-                                      .attrs
-                                      .rewrite(&self.get_context(),
-                                               Shape::indented(indent, self.config)));
+        let mut result = try_opt!(field.node.attrs.rewrite(&self.get_context(),
+                                                           Shape::indented(indent, self.config)));
         if !result.is_empty() {
             result.push('\n');
             result.push_str(&indent.to_string(self.config));
@@ -1201,9 +1198,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
         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,9 +1220,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 
         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,
-                     Shape::legacy(budget, shape.indent + last_line_width));
+        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') => {
                 let new_ty = rewrite_type_in_next_line();
@@ -1377,9 +1374,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
 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,
-                                                   Shape::legacy(shape.width, shape.indent)));
+            let mut result = try_opt!(self.pat.rewrite(context,
+                                                       Shape::legacy(shape.width, shape.indent)));
 
             if self.ty.node != ast::TyKind::Infer {
                 if context.config.space_before_type_annotation {
@@ -1390,10 +1386,9 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     result.push_str(" ");
                 }
                 let max_width = try_opt!(shape.width.checked_sub(result.len()));
-                let ty_str = try_opt!(self.ty
-                                          .rewrite(context,
-                                                   Shape::legacy(max_width,
-                                                                 shape.indent + result.len())));
+                let ty_str = try_opt!(self.ty.rewrite(context,
+                                                      Shape::legacy(max_width,
+                                                                    shape.indent + result.len())));
                 result.push_str(&ty_str);
             }
 
@@ -1693,9 +1688,8 @@ 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,
-                                                Shape::indented(ret_indent, context.config)));
+            let ret_str = try_opt!(fd.output.rewrite(context,
+                                                     Shape::indented(ret_indent, context.config)));
             result.push_str(&ret_str);
         } else {
             result.push_str(&ret_str);
@@ -1985,16 +1979,14 @@ 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 hi = if l.bounds.is_empty() {
-                     l.lifetime.span.hi
-                 } else {
-                     l.bounds[l.bounds.len() - 1].span.hi
-                 };
-                 mk_sp(l.lifetime.span.lo, hi)
-             });
+    let lt_spans = lifetimes.iter().map(|l| {
+                                            let hi = if l.bounds.is_empty() {
+                                                l.lifetime.span.hi
+                                            } else {
+                                                l.bounds[l.bounds.len() - 1].span.hi
+                                            };
+                                            mk_sp(l.lifetime.span.lo, hi)
+                                        });
     let ty_spans = tys.iter().map(span_for_ty_param);
 
     let items = itemize_list(context.codemap,
index 7933ebab807e373611ab36b0999a1a00fdcdc404..c2e98de55ed539b57e6fc30c33203b8dc0043698 100644 (file)
@@ -339,116 +339,114 @@ 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| {
-                let mut new_lines = false;
-                // Pre-comment
-                let pre_snippet = self.codemap
-                    .span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item)))
-                    .unwrap();
-                let trimmed_pre_snippet = pre_snippet.trim();
-                let has_pre_comment = trimmed_pre_snippet.contains("//") ||
-                                      trimmed_pre_snippet.contains("/*");
-                let pre_comment = if has_pre_comment {
-                    Some(trimmed_pre_snippet.to_owned())
-                } else {
-                    None
-                };
-
-                // Post-comment
-                let next_start = match self.inner.peek() {
-                    Some(next_item) => (self.get_lo)(next_item),
-                    None => self.next_span_start,
-                };
-                let post_snippet = self.codemap
-                    .span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start))
-                    .unwrap();
-
-                let comment_end = match self.inner.peek() {
-                    Some(..) => {
-                        let mut block_open_index = post_snippet.find("/*");
-                        // check if it realy is a block comment (and not //*)
-                        if let Some(i) = block_open_index {
-                            if i > 0 && &post_snippet[i - 1..i] == "/" {
-                                block_open_index = None;
-                            }
-                        }
-                        let newline_index = post_snippet.find('\n');
-                        let separator_index = post_snippet.find_uncommented(",").unwrap();
-
-                        match (block_open_index, newline_index) {
-                            // Separator before comment, with the next item on same line.
-                            // Comment belongs to next item.
-                            (Some(i), None) if i > separator_index => separator_index + 1,
-                            // Block-style post-comment before the separator.
-                            (Some(i), None) => {
-                                cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
-                                         separator_index + 1)
-                            }
-                            // Block-style post-comment. Either before or after the separator.
-                            (Some(i), Some(j)) if i < j => {
-                                cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
-                                         separator_index + 1)
-                            }
-                            // Potential *single* line comment.
-                            (_, Some(j)) if j > separator_index => j + 1,
-                            _ => post_snippet.len(),
+        self.inner.next().map(|item| {
+            let mut new_lines = false;
+            // Pre-comment
+            let pre_snippet = self.codemap
+                .span_to_snippet(codemap::mk_sp(self.prev_span_end, (self.get_lo)(&item)))
+                .unwrap();
+            let trimmed_pre_snippet = pre_snippet.trim();
+            let has_pre_comment = trimmed_pre_snippet.contains("//") ||
+                                  trimmed_pre_snippet.contains("/*");
+            let pre_comment = if has_pre_comment {
+                Some(trimmed_pre_snippet.to_owned())
+            } else {
+                None
+            };
+
+            // Post-comment
+            let next_start = match self.inner.peek() {
+                Some(next_item) => (self.get_lo)(next_item),
+                None => self.next_span_start,
+            };
+            let post_snippet = self.codemap
+                .span_to_snippet(codemap::mk_sp((self.get_hi)(&item), next_start))
+                .unwrap();
+
+            let comment_end = match self.inner.peek() {
+                Some(..) => {
+                    let mut block_open_index = post_snippet.find("/*");
+                    // check if it realy is a block comment (and not //*)
+                    if let Some(i) = block_open_index {
+                        if i > 0 && &post_snippet[i - 1..i] == "/" {
+                            block_open_index = None;
                         }
                     }
-                    None => {
-                        post_snippet
-                            .find_uncommented(self.terminator)
-                            .unwrap_or(post_snippet.len())
-                    }
-                };
-
-                if !post_snippet.is_empty() && comment_end > 0 {
-                    // Account for extra whitespace between items. This is fiddly
-                    // because of the way we divide pre- and post- comments.
-
-                    // Everything from the separator to the next item.
-                    let test_snippet = &post_snippet[comment_end - 1..];
-                    let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
-                    // From the end of the first line of comments.
-                    let test_snippet = &test_snippet[first_newline..];
-                    let first = test_snippet
-                        .find(|c: char| !c.is_whitespace())
-                        .unwrap_or(test_snippet.len());
-                    // From the end of the first line of comments to the next non-whitespace char.
-                    let test_snippet = &test_snippet[..first];
-
-                    if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
-                        // There were multiple line breaks which got trimmed to nothing.
-                        new_lines = true;
+                    let newline_index = post_snippet.find('\n');
+                    let separator_index = post_snippet.find_uncommented(",").unwrap();
+
+                    match (block_open_index, newline_index) {
+                        // Separator before comment, with the next item on same line.
+                        // Comment belongs to next item.
+                        (Some(i), None) if i > separator_index => separator_index + 1,
+                        // Block-style post-comment before the separator.
+                        (Some(i), None) => {
+                            cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
+                                     separator_index + 1)
+                        }
+                        // Block-style post-comment. Either before or after the separator.
+                        (Some(i), Some(j)) if i < j => {
+                            cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i,
+                                     separator_index + 1)
+                        }
+                        // Potential *single* line comment.
+                        (_, Some(j)) if j > separator_index => j + 1,
+                        _ => post_snippet.len(),
                     }
                 }
-
-                // Cleanup post-comment: strip separators and whitespace.
-                self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
-                let post_snippet = post_snippet[..comment_end].trim();
-
-                let post_snippet_trimmed = if post_snippet.starts_with(',') {
-                    post_snippet[1..].trim_matches(white_space)
-                } else if post_snippet.ends_with(',') {
-                    post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
-                } else {
+                None => {
                     post_snippet
-                };
-
-                let post_comment = if !post_snippet_trimmed.is_empty() {
-                    Some(post_snippet_trimmed.to_owned())
-                } else {
-                    None
-                };
-
-                ListItem {
-                    pre_comment: pre_comment,
-                    item: (self.get_item_string)(&item),
-                    post_comment: post_comment,
-                    new_lines: new_lines,
+                        .find_uncommented(self.terminator)
+                        .unwrap_or(post_snippet.len())
+                }
+            };
+
+            if !post_snippet.is_empty() && comment_end > 0 {
+                // Account for extra whitespace between items. This is fiddly
+                // because of the way we divide pre- and post- comments.
+
+                // Everything from the separator to the next item.
+                let test_snippet = &post_snippet[comment_end - 1..];
+                let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
+                // From the end of the first line of comments.
+                let test_snippet = &test_snippet[first_newline..];
+                let first = test_snippet
+                    .find(|c: char| !c.is_whitespace())
+                    .unwrap_or(test_snippet.len());
+                // From the end of the first line of comments to the next non-whitespace char.
+                let test_snippet = &test_snippet[..first];
+
+                if test_snippet.chars().filter(|c| c == &'\n').count() > 1 {
+                    // There were multiple line breaks which got trimmed to nothing.
+                    new_lines = true;
                 }
-            })
+            }
+
+            // Cleanup post-comment: strip separators and whitespace.
+            self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
+            let post_snippet = post_snippet[..comment_end].trim();
+
+            let post_snippet_trimmed = if post_snippet.starts_with(',') {
+                post_snippet[1..].trim_matches(white_space)
+            } else if post_snippet.ends_with(',') {
+                post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
+            } else {
+                post_snippet
+            };
+
+            let post_comment = if !post_snippet_trimmed.is_empty() {
+                Some(post_snippet_trimmed.to_owned())
+            } else {
+                None
+            };
+
+            ListItem {
+                pre_comment: pre_comment,
+                item: (self.get_item_string)(&item),
+                post_comment: post_comment,
+                new_lines: new_lines,
+            }
+        })
     }
 }
 
index 8f998e42035b59720d6332396d548bc35df3c578..70924b90d233fb5403d7d8e69f665e23168520bf 100644 (file)
@@ -159,15 +159,13 @@ 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()),
-                                                       mac.span.hi - BytePos(1)),
-                                                 context,
-                                                 shape));
+            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));
 
             Some(format!("{}{}", macro_name, rewrite))
         }
index 843556b9b38ef156207e4b620c52b6f56c463750..e83cc4d462d6048ce3c8481f640fc439a7a3c7c1 100644 (file)
@@ -158,13 +158,12 @@ fn replace_chars(string: &str) -> String {
                                                         self.config.max_width -
                                                         self.block_indent.width());
 
-                    self.buffer
-                        .push_str(&rewrite_comment(subslice,
-                                                   false,
-                                                   Shape::legacy(comment_width,
-                                                                 self.block_indent),
-                                                   self.config)
-                                          .unwrap());
+                    self.buffer.push_str(&rewrite_comment(subslice,
+                                                          false,
+                                                          Shape::legacy(comment_width,
+                                                                        self.block_indent),
+                                                          self.config)
+                                                 .unwrap());
 
                     last_wspace = None;
                     line_start = offset + subslice.len();
index 75175cc766722b2cb652562655380f8aba80e167..a16288886227348f1feab799d14d601485ca3b9a 100644 (file)
@@ -39,11 +39,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let sub_pat = match *sub_pat {
                     Some(ref p) => {
                         // 3 - ` @ `.
-                        let width = try_opt!(shape
-                                                 .width
-                                                 .checked_sub(prefix.len() + mut_infix.len() +
-                                                              id_str.len() +
-                                                              3));
+                        let width = try_opt!(shape.width.checked_sub(prefix.len() +
+                                                                     mut_infix.len() +
+                                                                     id_str.len() +
+                                                                     3));
                         format!(" @ {}",
                                 try_opt!(p.rewrite(context, Shape::legacy(width, shape.indent))))
                     }
@@ -328,13 +327,11 @@ 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,
-                            _ => false,
-                        }) {
+    for item in items.iter().rev().take_while(|i| match i.item {
+                                                  Some(ref internal_string) if internal_string ==
+                                                                               "_" => true,
+                                                  _ => false,
+                                              }) {
         suffix_len += 1;
 
         if item.pre_comment.is_some() || item.post_comment.is_some() {
index 754db6730133786bc92bf856beb2549817a2914c..5cb0fc3f49dbf16a24c0fc558788ca630e3823d0 100644 (file)
@@ -163,11 +163,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     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 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             // 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,
-                                                 Shape::legacy(max_path_width,
-                                                               shape.indent + extra_offset)));
+            let path_str = try_opt!(self.trait_ref.rewrite(context,
+                                                           Shape::legacy(max_path_width,
+                                                                         shape.indent +
+                                                                         extra_offset)));
 
             Some(if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
                      format!("for< {} > {}", lifetime_str, path_str)
@@ -613,22 +612,20 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                              format!("&{} {}{}",
                                      lt_str,
                                      mut_str,
-                                     try_opt!(mt.ty
-                                                  .rewrite(context,
-                                                           Shape::legacy(budget,
-                                                                         shape.indent + 2 +
-                                                                         mut_len +
-                                                                         lt_len))))
+                                     try_opt!(mt.ty.rewrite(context,
+                                                            Shape::legacy(budget,
+                                                                          shape.indent + 2 +
+                                                                          mut_len +
+                                                                          lt_len))))
                          }
                          None => {
                              let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
                              format!("&{}{}",
                                      mut_str,
-                                     try_opt!(mt.ty
-                                                  .rewrite(context,
-                                                           Shape::legacy(budget,
-                                                                         shape.indent + 1 +
-                                                                         mut_len))))
+                                     try_opt!(mt.ty.rewrite(context,
+                                                            Shape::legacy(budget,
+                                                                          shape.indent + 1 +
+                                                                          mut_len))))
                          }
                      })
             }
index 48531d321c83254fa2f1b66e2c398fc691dc31d2..66f7fa19cfb69d41d8501f339793faf7e579b038 100644 (file)
@@ -303,21 +303,18 @@ 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| {
-                    (capture
-                         .get(1)
-                         .expect("Couldn't unwrap capture")
-                         .as_str()
-                         .to_owned(),
-                     capture
-                         .get(2)
-                         .expect("Couldn't unwrap capture")
-                         .as_str()
-                         .to_owned())
-                })
+            regex.captures_iter(&line).next().map(|capture| {
+                (capture
+                     .get(1)
+                     .expect("Couldn't unwrap capture")
+                     .as_str()
+                     .to_owned(),
+                 capture
+                     .get(2)
+                     .expect("Couldn't unwrap capture")
+                     .as_str()
+                     .to_owned())
+            })
         })
         .collect()
 }
index b7cbf82f4119bdd3f9ab303fe85362ab8de985e3..2d3b52da72a4dfefc36ad838d5d9a500f366eb81 100644 (file)
@@ -29,14 +29,13 @@ fn b() {
 }
 
 fn issue550() {
-    self.visitor
-        .visit_volume(self.level.sector_id(sector),
-                      (floor_y,
-                       if is_sky_flat(ceil_tex) {
-                           from_wad_height(self.height_range.1)
-                       } else {
-                           ceil_y
-                       }));
+    self.visitor.visit_volume(self.level.sector_id(sector),
+                              (floor_y,
+                               if is_sky_flat(ceil_tex) {
+                                   from_wad_height(self.height_range.1)
+                               } else {
+                                   ceil_y
+                               }));
 }
 
 fn issue775() {