]> git.lizzy.rs Git - rust.git/blobdiff - src/patterns.rs
discard trailing blank comments
[rust.git] / src / patterns.rs
index cfff023bafc4bad922318290acfa43b4dc90349a..465d64627e8989aab428530aa43745c332a5e2c5 100644 (file)
 
 use codemap::SpanUtils;
 use comment::FindUncommented;
-use expr::{can_be_overflowed_expr, rewrite_pair, rewrite_unary_prefix, wrap_struct_field,
-           PairParts};
-use lists::{itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
-            struct_lit_tactic, write_list};
+use expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field};
+use lists::{
+    itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, struct_lit_tactic,
+    write_list,
+};
 use macros::{rewrite_macro, MacroPosition};
 use overflow;
+use pairs::{rewrite_pair, PairParts};
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
 use spanned::Spanned;
 use types::{rewrite_path, PathContext};
-use utils::{format_mutability, mk_sp};
+use utils::{format_mutability, mk_sp, rewrite_ident};
 
 /// Returns true if the given pattern is short. A short pattern is defined by the following grammer:
 ///
@@ -71,7 +73,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                     BindingMode::ByValue(mutability) => ("", mutability),
                 };
                 let mut_infix = format_mutability(mutability);
-                let id_str = ident.node.to_string();
+                let id_str = rewrite_ident(context, ident);
                 let sub_pat = match *sub_pat {
                     Some(ref p) => {
                         // 3 - ` @ `.
@@ -96,7 +98,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 }
             }
             PatKind::Range(ref lhs, ref rhs, ref end_kind) => {
-                let infix = match *end_kind {
+                let infix = match end_kind.node {
                     RangeEnd::Included(RangeSyntax::DotDotDot) => "...",
                     RangeEnd::Included(RangeSyntax::DotDotEq) => "..=",
                     RangeEnd::Excluded => "..",
@@ -109,7 +111,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 rewrite_pair(
                     &**lhs,
                     &**rhs,
-                    PairParts::new("", &infix, ""),
+                    PairParts::infix(&infix),
                     context,
                     shape,
                     SeparatorPlace::Front,
@@ -142,7 +144,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let prefix = prefix.iter().map(|p| p.rewrite(context, shape));
                 let slice_pat = slice_pat
                     .as_ref()
-                    .map(|p| Some(format!("{}..", p.rewrite(context, shape)?)));
+                    .and_then(|p| p.rewrite(context, shape))
+                    .map(|rw| Some(format!("{}..", if rw == "_" { "" } else { &rw })));
                 let suffix = suffix.iter().map(|p| p.rewrite(context, shape));
 
                 // Munge them together.
@@ -153,18 +156,14 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let pats = pats?;
 
                 // Unwrap all the sub-strings and join them with commas.
-                let result = if context.config.spaces_within_parens_and_brackets() {
-                    format!("[ {} ]", pats.join(", "))
-                } else {
-                    format!("[{}]", pats.join(", "))
-                };
-                Some(result)
+                Some(format!("[{}]", pats.join(", ")))
             }
             PatKind::Struct(ref path, ref fields, ellipsis) => {
                 rewrite_struct_pat(path, fields, ellipsis, self.span, context, shape)
             }
             PatKind::Mac(ref mac) => rewrite_macro(mac, None, context, shape, MacroPosition::Pat),
-            PatKind::Paren(ref pat) => pat.rewrite(context, shape.offset_left(1)?.sub_width(1)?)
+            PatKind::Paren(ref pat) => pat
+                .rewrite(context, shape.offset_left(1)?.sub_width(1)?)
                 .map(|inner_pat| format!("({})", inner_pat)),
         }
     }
@@ -216,7 +215,7 @@ fn rewrite_struct_pat(
     if ellipsis {
         if fields_str.contains('\n') || fields_str.len() > one_line_width {
             // Add a missing trailing comma.
-            if fmt.trailing_separator == SeparatorTactic::Never {
+            if context.config.trailing_comma() == SeparatorTactic::Never {
                 fields_str.push_str(",");
             }
             fields_str.push_str("\n");
@@ -225,7 +224,7 @@ fn rewrite_struct_pat(
         } else {
             if !fields_str.is_empty() {
                 // there are preceding struct fields being matched on
-                if fmt.tactic == DefinitiveListTactic::Vertical {
+                if tactic == DefinitiveListTactic::Vertical {
                     // if the tactic is Vertical, write_list already added a trailing ,
                     fields_str.push_str(" ");
                 } else {
@@ -247,7 +246,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             pat
         } else {
             let pat_str = pat?;
-            let id_str = self.ident.to_string();
+            let id_str = rewrite_ident(context, self.ident);
             let one_line_width = id_str.len() + 2 + pat_str.len();
             if one_line_width <= shape.width {
                 Some(format!("{}: {}", id_str, pat_str))
@@ -360,20 +359,13 @@ fn rewrite_tuple_pat(
 
     // add comma if `(x,)`
     let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
-    let mut context = context.clone();
-    if let Some(&TuplePatField::Dotdot(..)) = pat_vec.last() {
-        context.inside_macro = true;
-    }
     let path_str = path_str.unwrap_or_default();
-    let mut pat_ref_vec = Vec::with_capacity(pat_vec.len());
-    for pat in pat_vec {
-        pat_ref_vec.push(pat);
-    }
+    let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
 
     overflow::rewrite_with_parens(
         &context,
         &path_str,
-        &pat_ref_vec[..],
+        &pat_ref_vec,
         shape,
         span,
         context.config.max_width(),
@@ -412,7 +404,7 @@ fn count_wildcard_suffix_len(
     }) {
         suffix_len += 1;
 
-        if item.pre_comment.is_some() || item.post_comment.is_some() {
+        if item.has_comment() {
             break;
         }
     }