]> git.lizzy.rs Git - rust.git/blobdiff - src/lists.rs
Remove unnecessary references
[rust.git] / src / lists.rs
index ea34cdfc4801f403cccfa791b15b7e99b6517d31..42edc3d3d3c66cc62106739927827771e635647b 100644 (file)
 use rewrite::RewriteContext;
 use utils::{first_line_width, last_line_width, mk_sp};
 
-#[derive(Eq, PartialEq, Debug, Copy, Clone)]
 /// Formatting tactic for lists. This will be cast down to a
-/// DefinitiveListTactic depending on the number and length of the items and
+/// `DefinitiveListTactic` depending on the number and length of the items and
 /// their comments.
+#[derive(Eq, PartialEq, Debug, Copy, Clone)]
 pub enum ListTactic {
     // One item per row.
     Vertical,
@@ -114,16 +114,26 @@ pub struct ListItem {
 
 impl ListItem {
     pub fn inner_as_ref(&self) -> &str {
-        self.item.as_ref().map_or("", |s| &*s)
+        self.item.as_ref().map_or("", |s| s)
     }
 
-    pub fn is_multiline(&self) -> bool {
+    pub fn is_different_group(&self) -> bool {
         self.inner_as_ref().contains('\n') || self.pre_comment.is_some() ||
             self.post_comment
                 .as_ref()
                 .map_or(false, |s| s.contains('\n'))
     }
 
+    pub fn is_multiline(&self) -> bool {
+        self.inner_as_ref().contains('\n') ||
+            self.pre_comment
+                .as_ref()
+                .map_or(false, |s| s.contains('\n')) ||
+            self.post_comment
+                .as_ref()
+                .map_or(false, |s| s.contains('\n'))
+    }
+
     pub fn has_comment(&self) -> bool {
         self.pre_comment
             .as_ref()
@@ -144,8 +154,8 @@ pub fn from_str<S: Into<String>>(s: S) -> ListItem {
     }
 }
 
-#[derive(Eq, PartialEq, Debug, Copy, Clone)]
 /// The definitive formatting tactic for lists.
+#[derive(Eq, PartialEq, Debug, Copy, Clone)]
 pub enum DefinitiveListTactic {
     Vertical,
     Horizontal,
@@ -162,7 +172,7 @@ pub fn ends_with_newline(&self, indent_style: IndentStyle) -> bool {
 }
 
 /// The type of separator for lists.
-#[derive(Eq, PartialEq, Debug)]
+#[derive(Copy, Clone, Eq, PartialEq, Debug)]
 pub enum Separator {
     Comma,
     VerticalBar,
@@ -255,7 +265,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
     // Now that we know how we will layout, we can decide for sure if there
     // will be a trailing separator.
     let mut trailing_separator = formatting.needs_trailing_separator();
-    let mut result = String::new();
+    let mut result = String::with_capacity(128);
     let cloned_items = items.clone();
     let mut iter = items.into_iter().enumerate().peekable();
     let mut item_max_width: Option<usize> = None;
@@ -336,9 +346,9 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
 
             if tactic == DefinitiveListTactic::Vertical {
                 // We cannot keep pre-comments on the same line if the comment if normalized.
-                let keep_comment = if formatting.config.normalize_comments() {
-                    false
-                } else if item.pre_comment_style == ListItemCommentStyle::DifferentLine {
+                let keep_comment = if formatting.config.normalize_comments() ||
+                    item.pre_comment_style == ListItemCommentStyle::DifferentLine
+                {
                     false
                 } else {
                     // We will try to keep the comment on the same line with the item here.
@@ -395,7 +405,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
                         formatting.config.max_width(),
                     ));
                 }
-                let overhead = if let &mut Some(max_width) = item_max_width {
+                let overhead = if let Some(max_width) = *item_max_width {
                     max_width + 2
                 } else {
                     // 1 = space between item and comment.
@@ -469,7 +479,7 @@ fn max_width_of_item_with_post_comment<I, T>(
         let item = item.as_ref();
         let inner_item_width = item.inner_as_ref().len();
         if !first &&
-            (item.is_multiline() || !item.post_comment.is_some() ||
+            (item.is_different_group() || !item.post_comment.is_some() ||
                 inner_item_width + overhead > max_budget)
         {
             return max_width;
@@ -538,8 +548,7 @@ fn next(&mut self) -> Option<Self::Item> {
                     .chars()
                     .rev()
                     .take(comment_end + 1)
-                    .find(|c| *c == '\n')
-                    .is_some()
+                    .any(|c| c == '\n')
                 {
                     (
                         Some(trimmed_pre_snippet.to_owned()),
@@ -602,7 +611,7 @@ fn next(&mut self) -> Option<Self::Item> {
                 }
                 None => post_snippet
                     .find_uncommented(self.terminator)
-                    .unwrap_or(post_snippet.len()),
+                    .unwrap_or_else(|| post_snippet.len()),
             };
 
             if !post_snippet.is_empty() && comment_end > 0 {
@@ -611,12 +620,14 @@ fn next(&mut self) -> Option<Self::Item> {
 
                 // 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());
+                let first_newline = test_snippet
+                    .find('\n')
+                    .unwrap_or_else(|| 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());
+                    .unwrap_or_else(|| test_snippet.len());
                 // From the end of the first line of comments to the next non-whitespace char.
                 let test_snippet = &test_snippet[..first];
 
@@ -630,7 +641,7 @@ fn next(&mut self) -> Option<Self::Item> {
             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(',') {
+            let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
                 post_snippet[1..].trim_matches(white_space)
             } else if post_snippet.ends_with(',') {
                 post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
@@ -737,7 +748,7 @@ pub fn struct_lit_shape(
         IndentStyle::Block => {
             let shape = shape.block_indent(context.config.tab_spaces());
             Shape {
-                width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
+                width: context.budget(shape.indent.width()),
                 ..shape
             }
         }