]> git.lizzy.rs Git - rust.git/blobdiff - src/lists.rs
Remove unnecessary references
[rust.git] / src / lists.rs
index a9d705b4c64938e50840cb103296b71f7b98b1df..42edc3d3d3c66cc62106739927827771e635647b 100644 (file)
@@ -20,7 +20,7 @@
 use utils::{first_line_width, last_line_width, mk_sp};
 
 /// 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 {
@@ -114,7 +114,7 @@ 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_different_group(&self) -> bool {
@@ -172,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,
@@ -265,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;
@@ -346,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.
@@ -405,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.
@@ -548,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()),
@@ -612,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 {
@@ -621,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];
 
@@ -640,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)
@@ -747,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
             }
         }