about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/expr.rs14
-rw-r--r--src/imports.rs11
-rw-r--r--src/items.rs12
-rw-r--r--src/lib.rs26
-rw-r--r--src/lists.rs2
-rw-r--r--src/macros.rs10
-rw-r--r--src/reorder.rs2
-rw-r--r--src/rustfmt_diff.rs2
-rw-r--r--src/types.rs2
9 files changed, 33 insertions, 48 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 84ecbb57184..cd8bfa8a72b 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1661,11 +1661,7 @@ fn rewrite_struct_lit<'a>(
         let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
 
         let ends_with_comma = span_ends_with_comma(context, span);
-        let force_no_trailing_comma = if context.inside_macro() && !ends_with_comma {
-            true
-        } else {
-            false
-        };
+        let force_no_trailing_comma = context.inside_macro() && !ends_with_comma;
 
         let fmt = struct_lit_formatting(
             nested_shape,
@@ -1846,12 +1842,10 @@ where
             } else {
                 Some(SeparatorTactic::Never)
             }
+        } else if items.len() == 1 {
+            Some(SeparatorTactic::Always)
         } else {
-            if items.len() == 1 {
-                Some(SeparatorTactic::Always)
-            } else {
-                None
-            }
+            None
         };
         overflow::rewrite_with_parens(
             context,
diff --git a/src/imports.rs b/src/imports.rs
index 85ea119af95..f7286fc3b43 100644
--- a/src/imports.rs
+++ b/src/imports.rs
@@ -381,7 +381,7 @@ impl UseTree {
 
         // Normalise foo::self -> foo.
         if let UseSegment::Slf(None) = last {
-            if self.path.len() > 0 {
+            if !self.path.is_empty() {
                 return self;
             }
         }
@@ -494,8 +494,8 @@ impl UseTree {
             UseSegment::List(list) => {
                 let prefix = &self.path[..self.path.len() - 1];
                 let mut result = vec![];
-                for nested_use_tree in list.into_iter() {
-                    for mut flattend in nested_use_tree.clone().flatten().iter_mut() {
+                for nested_use_tree in list {
+                    for mut flattend in &mut nested_use_tree.clone().flatten() {
                         let mut new_path = prefix.to_vec();
                         new_path.append(&mut flattend.path);
                         result.push(UseTree {
@@ -672,7 +672,7 @@ fn rewrite_nested_use_tree(
             list_items.push(ListItem::from_str(use_tree.rewrite(context, nested_shape)?));
         }
     }
-    let (tactic, remaining_width) = if use_tree_list.iter().any(|use_segment| {
+    let has_nested_list = use_tree_list.iter().any(|use_segment| {
         use_segment
             .path
             .last()
@@ -680,7 +680,8 @@ fn rewrite_nested_use_tree(
                 UseSegment::List(..) => true,
                 _ => false,
             })
-    }) {
+    });
+    let (tactic, remaining_width) = if has_nested_list {
         (DefinitiveListTactic::Vertical, 0)
     } else {
         let remaining_width = shape.width.checked_sub(2).unwrap_or(0);
diff --git a/src/items.rs b/src/items.rs
index 91db82cd9ed..80e837ed904 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -2722,17 +2722,7 @@ impl Rewrite for ast::ForeignItem {
                 let mut_str = if is_mutable { "mut " } else { "" };
                 let prefix = format!("{}static {}{}:", vis, mut_str, self.ident);
                 // 1 = ;
-                let shape = shape.sub_width(1)?;
-                ty.rewrite(context, shape).map(|ty_str| {
-                    // 1 = space between prefix and type.
-                    let sep = if prefix.len() + ty_str.len() + 1 <= shape.width {
-                        Cow::from(" ")
-                    } else {
-                        let nested_indent = shape.indent.block_indent(context.config);
-                        nested_indent.to_string_with_newline(context.config)
-                    };
-                    format!("{}{}{};", prefix, sep, ty_str)
-                })
+                rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
             }
             ast::ForeignItemKind::Ty => {
                 let vis = format_visibility(&self.vis);
diff --git a/src/lib.rs b/src/lib.rs
index 9386c72f077..0a5c7307e4f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -220,12 +220,12 @@ impl FormatReport {
                 write!(t, "{} ", error.msg_prefix())?;
                 t.reset()?;
                 t.attr(term::Attr::Bold)?;
-                write!(t, "{}\n", error.kind)?;
+                writeln!(t, "{}", error.kind)?;
 
                 // Second line: file info
                 write!(t, "{}--> ", &prefix_spaces[1..])?;
                 t.reset()?;
-                write!(t, "{}:{}\n", file, error.line)?;
+                writeln!(t, "{}:{}", file, error.line)?;
 
                 // Third to fifth lines: show the line which triggered error, if available.
                 if !error.line_buffer.is_empty() {
@@ -233,11 +233,11 @@ impl FormatReport {
                     t.attr(term::Attr::Bold)?;
                     write!(t, "{}|\n{} | ", prefix_spaces, error.line)?;
                     t.reset()?;
-                    write!(t, "{}\n", error.line_buffer)?;
+                    writeln!(t, "{}", error.line_buffer)?;
                     t.attr(term::Attr::Bold)?;
                     write!(t, "{}| ", prefix_spaces)?;
                     t.fg(term::color::RED)?;
-                    write!(t, "{}\n", target_str(space_len, target_len))?;
+                    writeln!(t, "{}", target_str(space_len, target_len))?;
                     t.reset()?;
                 }
 
@@ -247,9 +247,9 @@ impl FormatReport {
                     t.attr(term::Attr::Bold)?;
                     write!(t, "{}= note: ", prefix_spaces)?;
                     t.reset()?;
-                    write!(t, "{}\n", error.msg_suffix())?;
+                    writeln!(t, "{}", error.msg_suffix())?;
                 } else {
-                    write!(t, "\n")?;
+                    writeln!(t)?;
                 }
                 t.reset()?;
             }
@@ -307,9 +307,9 @@ impl fmt::Display for FormatReport {
                     format!("{}note= ", prefix_spaces)
                 };
 
-                write!(
+                writeln!(
                     fmt,
-                    "{}\n{}\n{}\n{}{}\n",
+                    "{}\n{}\n{}\n{}{}",
                     error_info,
                     file_info,
                     error_line_buffer,
@@ -319,9 +319,9 @@ impl fmt::Display for FormatReport {
             }
         }
         if !self.file_error_map.is_empty() {
-            write!(
+            writeln!(
                 fmt,
-                "warning: rustfmt may have failed to format. See previous {} errors.\n",
+                "warning: rustfmt may have failed to format. See previous {} errors.",
                 self.warning_count(),
             )?;
         }
@@ -384,7 +384,7 @@ where
 
         debug_assert_eq!(
             visitor.line_number,
-            ::utils::count_newlines(&format!("{}", visitor.buffer))
+            ::utils::count_newlines(&visitor.buffer)
         );
 
         let filename = path.clone();
@@ -627,7 +627,7 @@ fn enclose_in_main_block(s: &str, config: &Config) -> String {
         }
         result.push_str(&line);
         result.push('\n');
-        need_indent = !(kind.is_string() && !line.ends_with('\\'));
+        need_indent = !kind.is_string() || line.ends_with('\\');
     }
     result.push('}');
     result
@@ -680,7 +680,7 @@ pub fn format_code_block(code_snippet: &str, config: &Config) -> Option<String>
             line
         };
         result.push_str(trimmed_line);
-        is_indented = !(kind.is_string() && !line.ends_with('\\'));
+        is_indented = !kind.is_string() || line.ends_with('\\');
     }
     Some(result)
 }
diff --git a/src/lists.rs b/src/lists.rs
index 8b75e68f6f5..9e5ea33d296 100644
--- a/src/lists.rs
+++ b/src/lists.rs
@@ -458,7 +458,7 @@ where
         let item = item.as_ref();
         let inner_item_width = item.inner_as_ref().len();
         if !first
-            && (item.is_different_group() || !item.post_comment.is_some()
+            && (item.is_different_group() || item.post_comment.is_none()
                 || inner_item_width + overhead > max_budget)
         {
             return max_width;
diff --git a/src/macros.rs b/src/macros.rs
index 9cf033e9693..a76520c8fe6 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -580,7 +580,7 @@ impl MacroArgKind {
                 let another = another
                     .as_ref()
                     .and_then(|a| a.rewrite(context, shape, use_multiple_lines))
-                    .unwrap_or("".to_owned());
+                    .unwrap_or_else(|| "".to_owned());
                 let repeat_tok = pprust::token_to_string(tok);
 
                 Some(format!("${}{}{}{}{}", lhs, inner, rhs, another, repeat_tok))
@@ -685,7 +685,7 @@ impl MacroArgParser {
         match iter.next() {
             Some(TokenTree::Token(sp, Token::Ident(ref ident, _))) => {
                 self.result.push(ParsedMacroArg {
-                    kind: MacroArgKind::MetaVariable(ident.clone(), self.buf.clone()),
+                    kind: MacroArgKind::MetaVariable(*ident, self.buf.clone()),
                     span: mk_sp(self.lo, sp.hi()),
                 });
 
@@ -718,7 +718,7 @@ impl MacroArgParser {
         let mut hi = span.hi();
 
         // Parse '*', '+' or '?.
-        while let Some(ref tok) = iter.next() {
+        for ref tok in iter {
             self.set_last_tok(tok);
             if first {
                 first = false;
@@ -1080,7 +1080,7 @@ fn indent_macro_snippet(
         .min()?;
 
     Some(
-        String::from(first_line) + "\n"
+        first_line + "\n"
             + &trimmed_lines
                 .iter()
                 .map(
@@ -1250,7 +1250,7 @@ impl MacroBranch {
                     if !l.is_empty() && need_indent {
                         s += &indent_str;
                     }
-                    (s + l + "\n", !(kind.is_string() && !l.ends_with('\\')))
+                    (s + l + "\n", !kind.is_string() || l.ends_with('\\'))
                 },
             )
             .0;
diff --git a/src/reorder.rs b/src/reorder.rs
index fb1716379b1..7d7c1ff8d32 100644
--- a/src/reorder.rs
+++ b/src/reorder.rs
@@ -149,7 +149,7 @@ fn rewrite_reorderable_items(
                 .into_iter()
                 .map(|use_tree| ListItem {
                     item: use_tree.rewrite_top_level(context, nested_shape),
-                    ..use_tree.list_item.unwrap_or_else(|| ListItem::empty())
+                    ..use_tree.list_item.unwrap_or_else(ListItem::empty)
                 })
                 .collect();
 
diff --git a/src/rustfmt_diff.rs b/src/rustfmt_diff.rs
index 426f7da96a2..178ed99f696 100644
--- a/src/rustfmt_diff.rs
+++ b/src/rustfmt_diff.rs
@@ -160,7 +160,7 @@ where
 
     for mismatch in diff {
         let title = get_section_title(mismatch.line_number);
-        writer.writeln(&format!("{}", title), None);
+        writer.writeln(&title, None);
 
         for line in mismatch.lines {
             match line {
diff --git a/src/types.rs b/src/types.rs
index 0c0cad8244b..4bde91f67f1 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -482,7 +482,7 @@ fn rewrite_bounded_lifetime(
 ) -> Option<String> {
     let result = lt.rewrite(context, shape)?;
 
-    if bounds.len() == 0 {
+    if bounds.is_empty() {
         Some(result)
     } else {
         let colon = type_bound_colon(context);