]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
transition to Rust 2018
[rust.git] / src / expr.rs
index 8118e3bccfd6f11525dc47b65b5e0eb3a85d5805..0e22134680f6e5f0356d3ab32af9ff339956e805 100644 (file)
 use std::borrow::Cow;
 use std::cmp::min;
 
-use config::lists::*;
-use syntax::codemap::{BytePos, CodeMap, Span};
 use syntax::parse::token::DelimToken;
+use syntax::source_map::{BytePos, SourceMap, Span};
 use syntax::{ast, ptr};
 
-use chains::rewrite_chain;
-use closures;
-use codemap::{LineRangeUtils, SpanUtils};
-use comment::{
+use crate::chains::rewrite_chain;
+use crate::closures;
+use crate::comment::{
     combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment,
     rewrite_missing_comment, CharClasses, FindUncommented,
 };
-use config::{Config, ControlBraceStyle, IndentStyle};
-use lists::{
+use crate::config::lists::*;
+use crate::config::{Config, ControlBraceStyle, IndentStyle, Version};
+use crate::lists::{
     definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
     struct_lit_tactic, write_list, ListFormatting, ListItem, Separator,
 };
-use macros::{rewrite_macro, MacroArg, MacroPosition};
-use matches::rewrite_match;
-use overflow;
-use pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
-use patterns::{can_be_overflowed_pat, is_short_pattern, TuplePatField};
-use rewrite::{Rewrite, RewriteContext};
-use shape::{Indent, Shape};
-use spanned::Spanned;
-use string::{rewrite_string, StringFormat};
-use types::{can_be_overflowed_type, rewrite_path, PathContext};
-use utils::{
-    colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width,
-    inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
-    ptr_vec_to_ref_vec, semicolon_for_stmt, wrap_str,
+use crate::macros::{rewrite_macro, MacroPosition};
+use crate::matches::rewrite_match;
+use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
+use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
+use crate::patterns::is_short_pattern;
+use crate::rewrite::{Rewrite, RewriteContext};
+use crate::shape::{Indent, Shape};
+use crate::source_map::{LineRangeUtils, SpanUtils};
+use crate::spanned::Spanned;
+use crate::string::{rewrite_string, StringFormat};
+use crate::types::{rewrite_path, PathContext};
+use crate::utils::{
+    colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
+    last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
+    semicolon_for_expr, semicolon_for_stmt, wrap_str,
 };
-use vertical::rewrite_with_alignment;
-use visitor::FmtVisitor;
+use crate::vertical::rewrite_with_alignment;
+use crate::visitor::FmtVisitor;
 
 impl Rewrite for ast::Expr {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
@@ -69,11 +69,16 @@ pub fn format_expr(
     if contains_skip(&*expr.attrs) {
         return Some(context.snippet(expr.span()).to_owned());
     }
+    let shape = if expr_type == ExprType::Statement && semicolon_for_expr(context, expr) {
+        shape.sub_width(1)?
+    } else {
+        shape
+    };
 
     let expr_rw = match expr.node {
         ast::ExprKind::Array(ref expr_vec) => rewrite_array(
             "",
-            &ptr_vec_to_ref_vec(expr_vec),
+            expr_vec.iter(),
             expr.span,
             context,
             shape,
@@ -100,7 +105,7 @@ pub fn format_expr(
                 )
             })
         }
-        ast::ExprKind::Unary(ref op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
+        ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
         ast::ExprKind::Struct(ref path, ref fields, ref base) => rewrite_struct_lit(
             context,
             path,
@@ -110,7 +115,7 @@ pub fn format_expr(
             shape,
         ),
         ast::ExprKind::Tup(ref items) => {
-            rewrite_tuple(context, &ptr_vec_to_ref_vec(items), expr.span, shape)
+            rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
         }
         ast::ExprKind::If(..)
         | ast::ExprKind::IfLet(..)
@@ -179,11 +184,13 @@ pub fn format_expr(
                 Some(format!("break{}", id_str))
             }
         }
-        ast::ExprKind::Yield(ref opt_expr) => if let Some(ref expr) = *opt_expr {
-            rewrite_unary_prefix(context, "yield ", &**expr, shape)
-        } else {
-            Some("yield".to_string())
-        },
+        ast::ExprKind::Yield(ref opt_expr) => {
+            if let Some(ref expr) = *opt_expr {
+                rewrite_unary_prefix(context, "yield ", &**expr, shape)
+            } else {
+                Some("yield".to_string())
+            }
+        }
         ast::ExprKind::Closure(capture, asyncness, movability, ref fn_decl, ref body, _) => {
             closures::rewrite_closure(
                 capture, asyncness, movability, fn_decl, body, expr.span, context, shape,
@@ -271,11 +278,9 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
 
                 format!(
                     "{}{}{}",
-                    lhs.map(|lhs| space_if(needs_space_before_range(context, lhs)))
-                        .unwrap_or(""),
+                    lhs.map_or("", |lhs| space_if(needs_space_before_range(context, lhs))),
                     delim,
-                    rhs.map(|rhs| space_if(needs_space_after_range(rhs)))
-                        .unwrap_or(""),
+                    rhs.map_or("", |rhs| space_if(needs_space_after_range(rhs))),
                 )
             };
 
@@ -317,10 +322,40 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
         // We do not format these expressions yet, but they should still
         // satisfy our width restrictions.
         ast::ExprKind::InlineAsm(..) => Some(context.snippet(expr.span).to_owned()),
-        ast::ExprKind::Catch(ref block) => {
+        ast::ExprKind::TryBlock(ref block) => {
+            if let rw @ Some(_) =
+                rewrite_single_line_block(context, "try ", block, Some(&expr.attrs), None, shape)
+            {
+                rw
+            } else {
+                // 9 = `try `
+                let budget = shape.width.saturating_sub(9);
+                Some(format!(
+                    "{}{}",
+                    "try ",
+                    rewrite_block(
+                        block,
+                        Some(&expr.attrs),
+                        None,
+                        context,
+                        Shape::legacy(budget, shape.indent)
+                    )?
+                ))
+            }
+        }
+        ast::ExprKind::ObsoleteInPlace(ref lhs, ref rhs) => lhs
+            .rewrite(context, shape)
+            .map(|s| s + " <-")
+            .and_then(|lhs| rewrite_assign_rhs(context, lhs, &**rhs, shape)),
+        ast::ExprKind::Async(capture_by, _node_id, ref block) => {
+            let mover = if capture_by == ast::CaptureBy::Value {
+                "move "
+            } else {
+                ""
+            };
             if let rw @ Some(_) = rewrite_single_line_block(
                 context,
-                "do catch ",
+                format!("{}{}", "async ", mover).as_str(),
                 block,
                 Some(&expr.attrs),
                 None,
@@ -328,11 +363,12 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
             ) {
                 rw
             } else {
-                // 9 = `do catch `
-                let budget = shape.width.saturating_sub(9);
+                // 6 = `async `
+                let budget = shape.width.saturating_sub(6);
                 Some(format!(
-                    "{}{}",
-                    "do catch ",
+                    "{}{}{}",
+                    "async ",
+                    mover,
                     rewrite_block(
                         block,
                         Some(&expr.attrs),
@@ -343,10 +379,6 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
                 ))
             }
         }
-        // FIXME(#2743)
-        ast::ExprKind::ObsoleteInPlace(..) => unimplemented!(),
-        // FIXME(topecongiro) Format async block.
-        ast::ExprKind::Async(..) => None,
     };
 
     expr_rw
@@ -362,11 +394,11 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
         })
 }
 
-pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
-    name: &str,
-    exprs: &[&T],
+pub fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
+    name: &'a str,
+    exprs: impl Iterator<Item = &'a T>,
     span: Span,
-    context: &RewriteContext,
+    context: &'a RewriteContext,
     shape: Shape,
     force_separator_tactic: Option<SeparatorTactic>,
     delim_token: Option<DelimToken>,
@@ -390,13 +422,16 @@ fn rewrite_empty_block(
     prefix: &str,
     shape: Shape,
 ) -> Option<String> {
+    if !block.stmts.is_empty() {
+        return None;
+    }
+
     let label_str = rewrite_label(label);
     if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
         return None;
     }
 
-    if block.stmts.is_empty() && !block_contains_comment(block, context.codemap) && shape.width >= 2
-    {
+    if !block_contains_comment(block, context.source_map) && shape.width >= 2 {
         return Some(format!("{}{}{{}}", prefix, label_str));
     }
 
@@ -453,7 +488,7 @@ fn rewrite_single_line_block(
     label: Option<ast::Label>,
     shape: Shape,
 ) -> Option<String> {
-    if is_simple_block(block, attrs, context.codemap) {
+    if is_simple_block(block, attrs, context.source_map) {
         let expr_shape = shape.offset_left(last_line_width(prefix))?;
         let expr_str = block.stmts[0].rewrite(context, expr_shape)?;
         let label_str = rewrite_label(label);
@@ -481,13 +516,13 @@ pub fn rewrite_block_with_visitor(
     let mut visitor = FmtVisitor::from_context(context);
     visitor.block_indent = shape.indent;
     visitor.is_if_else_block = context.is_if_else_block();
-    match block.rules {
-        ast::BlockCheckMode::Unsafe(..) => {
+    match (block.rules, label) {
+        (ast::BlockCheckMode::Unsafe(..), _) | (ast::BlockCheckMode::Default, Some(_)) => {
             let snippet = context.snippet(block.span);
             let open_pos = snippet.find_uncommented("{")?;
             visitor.last_pos = block.span.lo() + BytePos(open_pos as u32)
         }
-        ast::BlockCheckMode::Default => visitor.last_pos = block.span.lo(),
+        (ast::BlockCheckMode::Default, None) => visitor.last_pos = block.span.lo(),
     }
 
     let inner_attrs = attrs.map(inner_attributes);
@@ -739,8 +774,8 @@ fn rewrite_single_line(
         let fixed_cost = self.keyword.len() + "  {  } else {  }".len();
 
         if let ast::ExprKind::Block(ref else_node, _) = else_block.node {
-            if !is_simple_block(self.block, None, context.codemap)
-                || !is_simple_block(else_node, None, context.codemap)
+            if !is_simple_block(self.block, None, context.source_map)
+                || !is_simple_block(else_node, None, context.source_map)
                 || pat_expr_str.contains('\n')
             {
                 return None;
@@ -772,6 +807,20 @@ fn rewrite_single_line(
     }
 }
 
+/// Returns true if the last line of pat_str has leading whitespace and it is wider than the
+/// shape's indent.
+fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool {
+    let mut leading_whitespaces = 0;
+    for c in pat_str.chars().rev() {
+        match c {
+            '\n' => break,
+            _ if c.is_whitespace() => leading_whitespaces += 1,
+            _ => leading_whitespaces = 0,
+        }
+    }
+    leading_whitespaces > start_column
+}
+
 impl<'a> ControlFlow<'a> {
     fn rewrite_pat_expr(
         &self,
@@ -856,7 +905,8 @@ fn rewrite_cond(
             .saturating_sub(constr_shape.used_width() + offset + brace_overhead);
         let force_newline_brace = (pat_expr_string.contains('\n')
             || pat_expr_string.len() > one_line_budget)
-            && !last_line_extendable(&pat_expr_string);
+            && (!last_line_extendable(&pat_expr_string)
+                || last_line_offsetted(shape.used_width(), &pat_expr_string));
 
         // Try to format if-else on single line.
         if self.allow_single_line
@@ -869,10 +919,11 @@ fn rewrite_cond(
             let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
 
             if let Some(cond_str) = trial {
-                if cond_str.len() <= context
-                    .config
-                    .width_heuristics()
-                    .single_line_if_else_max_width
+                if cond_str.len()
+                    <= context
+                        .config
+                        .width_heuristics()
+                        .single_line_if_else_max_width
                 {
                     return Some((cond_str, 0));
                 }
@@ -998,7 +1049,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                         false,
                         true,
                         mk_sp(else_block.span.lo(), self.span.hi()),
-                    ).rewrite(context, shape)
+                    )
+                    .rewrite(context, shape)
                 }
                 ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
                     ControlFlow::new_if(
@@ -1009,7 +1061,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                         false,
                         true,
                         mk_sp(else_block.span.lo(), self.span.hi()),
-                    ).rewrite(context, shape)
+                    )
+                    .rewrite(context, shape)
                 }
                 _ => {
                     last_in_chain = true;
@@ -1083,8 +1136,8 @@ fn extract_comment(span: Span, context: &RewriteContext, shape: Shape) -> Option
     }
 }
 
-pub fn block_contains_comment(block: &ast::Block, codemap: &CodeMap) -> bool {
-    let snippet = codemap.span_to_snippet(block.span).unwrap();
+pub fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) -> bool {
+    let snippet = source_map.span_to_snippet(block.span).unwrap();
     contains_comment(&snippet)
 }
 
@@ -1095,11 +1148,11 @@ pub fn block_contains_comment(block: &ast::Block, codemap: &CodeMap) -> bool {
 pub fn is_simple_block(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
-    codemap: &CodeMap,
+    source_map: &SourceMap,
 ) -> bool {
     (block.stmts.len() == 1
         && stmt_is_expr(&block.stmts[0])
-        && !block_contains_comment(block, codemap)
+        && !block_contains_comment(block, source_map)
         && attrs.map_or(true, |a| a.is_empty()))
 }
 
@@ -1108,10 +1161,10 @@ pub fn is_simple_block(
 pub fn is_simple_block_stmt(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
-    codemap: &CodeMap,
+    source_map: &SourceMap,
 ) -> bool {
     block.stmts.len() <= 1
-        && !block_contains_comment(block, codemap)
+        && !block_contains_comment(block, source_map)
         && attrs.map_or(true, |a| a.is_empty())
 }
 
@@ -1120,10 +1173,10 @@ pub fn is_simple_block_stmt(
 pub fn is_empty_block(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
-    codemap: &CodeMap,
+    source_map: &SourceMap,
 ) -> bool {
     block.stmts.is_empty()
-        && !block_contains_comment(block, codemap)
+        && !block_contains_comment(block, source_map)
         && attrs.map_or(true, |a| inner_attributes(a).is_empty())
 }
 
@@ -1167,17 +1220,11 @@ pub fn rewrite_multiple_patterns(
             shape.width,
         )
     };
-    let fmt = ListFormatting {
-        tactic,
-        separator: " |",
-        trailing_separator: SeparatorTactic::Never,
-        separator_place: context.config.binop_separator(),
-        shape,
-        ends_with_newline: false,
-        preserve_newline: false,
-        nested: false,
-        config: context.config,
-    };
+    let fmt = ListFormatting::new(shape, context.config)
+        .tactic(tactic)
+        .separator(" |")
+        .separator_place(context.config.binop_separator())
+        .ends_with_newline(false);
     write_list(&items, &fmt)
 }
 
@@ -1210,13 +1257,18 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
                         format!(
                             "{}{}",
                             new_indent.to_string(context.config),
-                            line.trim_left()
+                            line.trim_start()
                         )
-                    }).collect::<Vec<_>>()
+                    })
+                    .collect::<Vec<_>>()
                     .join("\n")
-                    .trim_left(),
+                    .trim_start(),
             );
-            return wrap_str(indented_string_lit, context.config.max_width(), shape);
+            return if context.config.version() == Version::Two {
+                Some(indented_string_lit)
+            } else {
+                wrap_str(indented_string_lit, context.config.max_width(), shape)
+            };
         } else {
             return wrap_str(string_lit.to_owned(), context.config.max_width(), shape);
         }
@@ -1228,57 +1280,10 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
     rewrite_string(
         str_lit,
         &StringFormat::new(shape.visual_indent(0), context.config),
+        shape.width.saturating_sub(2),
     )
 }
 
-/// In case special-case style is required, returns an offset from which we start horizontal layout.
-pub fn maybe_get_args_offset<T: ToExpr>(callee_str: &str, args: &[&T]) -> Option<(bool, usize)> {
-    if let Some(&(_, num_args_before)) = SPECIAL_MACRO_WHITELIST
-        .iter()
-        .find(|&&(s, _)| s == callee_str)
-    {
-        let all_simple = args.len() > num_args_before && is_every_expr_simple(args);
-
-        Some((all_simple, num_args_before))
-    } else {
-        None
-    }
-}
-
-/// A list of `format!`-like macros, that take a long format string and a list of arguments to
-/// format.
-///
-/// Organized as a list of `(&str, usize)` tuples, giving the name of the macro and the number of
-/// arguments before the format string (none for `format!("format", ...)`, one for `assert!(result,
-/// "format", ...)`, two for `assert_eq!(left, right, "format", ...)`).
-const SPECIAL_MACRO_WHITELIST: &[(&str, usize)] = &[
-    // format! like macros
-    // From the Rust Standard Library.
-    ("eprint!", 0),
-    ("eprintln!", 0),
-    ("format!", 0),
-    ("format_args!", 0),
-    ("print!", 0),
-    ("println!", 0),
-    ("panic!", 0),
-    ("unreachable!", 0),
-    // From the `log` crate.
-    ("debug!", 0),
-    ("error!", 0),
-    ("info!", 0),
-    ("warn!", 0),
-    // write! like macros
-    ("assert!", 1),
-    ("debug_assert!", 1),
-    ("write!", 1),
-    ("writeln!", 1),
-    // assert_eq! like macros
-    ("assert_eq!", 2),
-    ("assert_ne!", 2),
-    ("debug_assert_eq!", 2),
-    ("debug_assert_ne!", 2),
-];
-
 fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option<SeparatorTactic> {
     if context.inside_macro() {
         if span_ends_with_comma(context, span) {
@@ -1301,7 +1306,7 @@ pub fn rewrite_call(
     overflow::rewrite_with_parens(
         context,
         callee,
-        &ptr_vec_to_ref_vec(args),
+        args.iter(),
         shape,
         span,
         context.config.width_heuristics().fn_call_width,
@@ -1309,7 +1314,7 @@ pub fn rewrite_call(
     )
 }
 
-fn is_simple_expr(expr: &ast::Expr) -> bool {
+pub fn is_simple_expr(expr: &ast::Expr) -> bool {
     match expr.node {
         ast::ExprKind::Lit(..) => true,
         ast::ExprKind::Path(ref qself, ref path) => qself.is_none() && path.segments.len() <= 1,
@@ -1327,10 +1332,8 @@ fn is_simple_expr(expr: &ast::Expr) -> bool {
     }
 }
 
-pub fn is_every_expr_simple<T: ToExpr>(lists: &[&T]) -> bool {
-    lists
-        .iter()
-        .all(|arg| arg.to_expr().map_or(false, is_simple_expr))
+pub fn is_every_expr_simple(lists: &[OverflowableItem]) -> bool {
+    lists.iter().all(OverflowableItem::is_simple)
 }
 
 pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: usize) -> bool {
@@ -1347,16 +1350,28 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
         | ast::ExprKind::WhileLet(..) => {
             context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
         }
-        ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => {
-            context.use_block_indent()
-                || context.config.indent_style() == IndentStyle::Visual && args_len > 1
+
+        // Handle always block-like expressions
+        ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => true,
+
+        // Handle `[]` and `{}`-like expressions
+        ast::ExprKind::Array(..) | ast::ExprKind::Struct(..) => {
+            context.config.overflow_delimited_expr()
+                || (context.use_block_indent() && args_len == 1)
+        }
+        ast::ExprKind::Mac(ref macro_) => {
+            match (macro_.node.delim, context.config.overflow_delimited_expr()) {
+                (ast::MacDelimiter::Bracket, true) | (ast::MacDelimiter::Brace, true) => true,
+                _ => context.use_block_indent() && args_len == 1,
+            }
+        }
+
+        // Handle parenthetical expressions
+        ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Tup(..) => {
+            context.use_block_indent() && args_len == 1
         }
-        ast::ExprKind::Array(..)
-        | ast::ExprKind::Call(..)
-        | ast::ExprKind::Mac(..)
-        | ast::ExprKind::MethodCall(..)
-        | ast::ExprKind::Struct(..)
-        | ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1,
+
+        // Handle unary-like expressions
         ast::ExprKind::AddrOf(_, ref expr)
         | ast::ExprKind::Box(ref expr)
         | ast::ExprKind::Try(ref expr)
@@ -1410,13 +1425,15 @@ fn rewrite_paren(
     debug!("rewrite_paren, shape: {:?}", shape);
 
     // Extract comments within parens.
+    let mut pre_span;
+    let mut post_span;
     let mut pre_comment;
     let mut post_comment;
     let remove_nested_parens = context.config.remove_nested_parens();
     loop {
         // 1 = "(" or ")"
-        let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
-        let post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
+        pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
+        post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
         pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
         post_comment = rewrite_missing_comment(post_span, shape, context)?;
 
@@ -1432,20 +1449,48 @@ fn rewrite_paren(
         break;
     }
 
-    // 1 `(`
-    let sub_shape = shape.offset_left(1).and_then(|s| s.sub_width(1))?;
-
+    // 1 = `(` and `)`
+    let sub_shape = shape.offset_left(1)?.sub_width(1)?;
     let subexpr_str = subexpr.rewrite(context, sub_shape)?;
-    debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
-
-    // 2 = `()`
-    if subexpr_str.contains('\n') || first_line_width(&subexpr_str) + 2 <= shape.width {
+    let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
+    if fits_single_line {
         Some(format!("({}{}{})", pre_comment, &subexpr_str, post_comment))
     } else {
-        None
+        rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
     }
 }
 
+fn rewrite_paren_in_multi_line(
+    context: &RewriteContext,
+    subexpr: &ast::Expr,
+    shape: Shape,
+    pre_span: Span,
+    post_span: Span,
+) -> Option<String> {
+    let nested_indent = shape.indent.block_indent(context.config);
+    let nested_shape = Shape::indented(nested_indent, context.config);
+    let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
+    let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
+    let subexpr_str = subexpr.rewrite(context, nested_shape)?;
+
+    let mut result = String::with_capacity(subexpr_str.len() * 2);
+    result.push('(');
+    if !pre_comment.is_empty() {
+        result.push_str(&nested_indent.to_string_with_newline(context.config));
+        result.push_str(&pre_comment);
+    }
+    result.push_str(&nested_indent.to_string_with_newline(context.config));
+    result.push_str(&subexpr_str);
+    if !post_comment.is_empty() {
+        result.push_str(&nested_indent.to_string_with_newline(context.config));
+        result.push_str(&post_comment);
+    }
+    result.push_str(&shape.indent.to_string_with_newline(context.config));
+    result.push(')');
+
+    Some(result)
+}
+
 fn rewrite_index(
     expr: &ast::Expr,
     index: &ast::Expr,
@@ -1461,7 +1506,12 @@ fn rewrite_index(
             .offset_left(offset)
             .and_then(|shape| shape.sub_width(1 + rhs_overhead))
     } else {
-        shape.visual_indent(offset).sub_width(offset + 1)
+        match context.config.indent_style() {
+            IndentStyle::Block => shape
+                .offset_left(offset)
+                .and_then(|shape| shape.sub_width(1)),
+            IndentStyle::Visual => shape.visual_indent(offset).sub_width(offset + 1),
+        }
     };
     let orig_index_rw = index_shape.and_then(|s| index.rewrite(context, s));
 
@@ -1496,7 +1546,7 @@ fn rewrite_index(
     }
 }
 
-fn struct_lit_can_be_aligned(fields: &[ast::Field], base: &Option<&ast::Expr>) -> bool {
+fn struct_lit_can_be_aligned(fields: &[ast::Field], base: Option<&ast::Expr>) -> bool {
     if base.is_some() {
         return false;
     }
@@ -1532,19 +1582,19 @@ enum StructLitField<'a> {
 
     let one_line_width = h_shape.map_or(0, |shape| shape.width);
     let body_lo = context.snippet_provider.span_after(span, "{");
-    let fields_str = if struct_lit_can_be_aligned(fields, &base)
+    let fields_str = if struct_lit_can_be_aligned(fields, base)
         && context.config.struct_field_align_threshold() > 0
     {
         rewrite_with_alignment(
             fields,
             context,
-            shape,
+            v_shape,
             mk_sp(body_lo, span.hi()),
             one_line_width,
         )?
     } else {
         let field_iter = fields
-            .into_iter()
+            .iter()
             .map(StructLitField::Regular)
             .chain(base.into_iter().map(StructLitField::Base));
 
@@ -1597,7 +1647,7 @@ enum StructLitField<'a> {
             nested_shape,
             tactic,
             context,
-            force_no_trailing_comma || base.is_some(),
+            force_no_trailing_comma || base.is_some() || !context.use_block_indent(),
         );
 
         write_list(&item_vec, &fmt)?
@@ -1653,7 +1703,7 @@ pub fn rewrite_field(
     };
     let name = context.snippet(field.ident.span);
     if field.is_shorthand {
-        Some(attrs_str + &name)
+        Some(attrs_str + name)
     } else {
         let mut separator = String::from(struct_lit_field_separator(context.config));
         for _ in 0..prefix_max_width.saturating_sub(name.len()) {
@@ -1665,7 +1715,7 @@ pub fn rewrite_field(
 
         match expr {
             Some(ref e) if e.as_str() == name && context.config.use_field_init_shorthand() => {
-                Some(attrs_str + &name)
+                Some(attrs_str + name)
             }
             Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)),
             None => {
@@ -1687,19 +1737,16 @@ pub fn rewrite_field(
     }
 }
 
-fn rewrite_tuple_in_visual_indent_style<'a, T>(
+fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
     context: &RewriteContext,
-    items: &[&T],
+    mut items: impl Iterator<Item = &'a T>,
     span: Span,
     shape: Shape,
-) -> Option<String>
-where
-    T: Rewrite + Spanned + ToExpr + 'a,
-{
-    let mut items = items.iter();
+    is_singleton_tuple: bool,
+) -> Option<String> {
     // In case of length 1, need a trailing comma
     debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
-    if items.len() == 1 {
+    if is_singleton_tuple {
         // 3 = "(" + ",)"
         let nested_shape = shape.sub_width(3)?.visual_indent(1);
         return items
@@ -1730,31 +1777,21 @@ fn rewrite_tuple_in_visual_indent_style<'a, T>(
         Separator::Comma,
         nested_shape.width,
     );
-    let fmt = ListFormatting {
-        tactic,
-        separator: ",",
-        trailing_separator: SeparatorTactic::Never,
-        separator_place: SeparatorPlace::Back,
-        shape,
-        ends_with_newline: false,
-        preserve_newline: false,
-        nested: false,
-        config: context.config,
-    };
+    let fmt = ListFormatting::new(nested_shape, context.config)
+        .tactic(tactic)
+        .ends_with_newline(false);
     let list_str = write_list(&item_vec, &fmt)?;
 
     Some(format!("({})", list_str))
 }
 
-pub fn rewrite_tuple<'a, T>(
-    context: &RewriteContext,
-    items: &[&T],
+pub fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
+    context: &'a RewriteContext,
+    items: impl Iterator<Item = &'a T>,
     span: Span,
     shape: Shape,
-) -> Option<String>
-where
-    T: Rewrite + Spanned + ToExpr + 'a,
-{
+    is_singleton_tuple: bool,
+) -> Option<String> {
     debug!("rewrite_tuple {:?}", shape);
     if context.use_block_indent() {
         // We use the same rule as function calls for rewriting tuples.
@@ -1764,7 +1801,7 @@ pub fn rewrite_tuple<'a, T>(
             } else {
                 Some(SeparatorTactic::Never)
             }
-        } else if items.len() == 1 {
+        } else if is_singleton_tuple {
             Some(SeparatorTactic::Always)
         } else {
             None
@@ -1779,7 +1816,7 @@ pub fn rewrite_tuple<'a, T>(
             force_tactic,
         )
     } else {
-        rewrite_tuple_in_visual_indent_style(context, items, span, shape)
+        rewrite_tuple_in_visual_indent_style(context, items, span, shape, is_singleton_tuple)
     }
 }
 
@@ -1812,17 +1849,12 @@ pub fn rewrite_unary_suffix<R: Rewrite>(
 
 fn rewrite_unary_op(
     context: &RewriteContext,
-    op: &ast::UnOp,
+    op: ast::UnOp,
     expr: &ast::Expr,
     shape: Shape,
 ) -> Option<String> {
     // For some reason, an UnOp is not spanned like BinOp!
-    let operator_str = match *op {
-        ast::UnOp::Deref => "*",
-        ast::UnOp::Not => "!",
-        ast::UnOp::Neg => "-",
-    };
-    rewrite_unary_prefix(context, operator_str, expr, shape)
+    rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
 }
 
 fn rewrite_assignment(
@@ -1940,7 +1972,9 @@ fn shape_from_rhs_tactic(
     rhs_tactic: RhsTactics,
 ) -> Option<Shape> {
     match rhs_tactic {
-        RhsTactics::ForceNextLineWithoutIndent => Some(shape.with_max_width(context.config)),
+        RhsTactics::ForceNextLineWithoutIndent => shape
+            .with_max_width(context.config)
+            .sub_width(shape.indent.width()),
         RhsTactics::Default => {
             Shape::indented(shape.indent.block_indent(context.config), context.config)
                 .sub_width(shape.rhs_overhead(context.config))
@@ -1970,79 +2004,6 @@ fn rewrite_expr_addrof(
     rewrite_unary_prefix(context, operator_str, expr, shape)
 }
 
-pub trait ToExpr {
-    fn to_expr(&self) -> Option<&ast::Expr>;
-    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool;
-}
-
-impl ToExpr for ast::Expr {
-    fn to_expr(&self) -> Option<&ast::Expr> {
-        Some(self)
-    }
-
-    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
-        can_be_overflowed_expr(context, self, len)
-    }
-}
-
-impl ToExpr for ast::Ty {
-    fn to_expr(&self) -> Option<&ast::Expr> {
-        None
-    }
-
-    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
-        can_be_overflowed_type(context, self, len)
-    }
-}
-
-impl<'a> ToExpr for TuplePatField<'a> {
-    fn to_expr(&self) -> Option<&ast::Expr> {
-        None
-    }
-
-    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
-        can_be_overflowed_pat(context, self, len)
-    }
-}
-
-impl<'a> ToExpr for ast::StructField {
-    fn to_expr(&self) -> Option<&ast::Expr> {
-        None
-    }
-
-    fn can_be_overflowed(&self, _: &RewriteContext, _: usize) -> bool {
-        false
-    }
-}
-
-impl<'a> ToExpr for MacroArg {
-    fn to_expr(&self) -> Option<&ast::Expr> {
-        match *self {
-            MacroArg::Expr(ref expr) => Some(expr),
-            _ => None,
-        }
-    }
-
-    fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
-        match *self {
-            MacroArg::Expr(ref expr) => can_be_overflowed_expr(context, expr, len),
-            MacroArg::Ty(ref ty) => can_be_overflowed_type(context, ty, len),
-            MacroArg::Pat(..) => false,
-            MacroArg::Item(..) => len == 1,
-        }
-    }
-}
-
-impl ToExpr for ast::GenericParam {
-    fn to_expr(&self) -> Option<&ast::Expr> {
-        None
-    }
-
-    fn can_be_overflowed(&self, _: &RewriteContext, _: usize) -> bool {
-        false
-    }
-}
-
 pub fn is_method_call(expr: &ast::Expr) -> bool {
     match expr.node {
         ast::ExprKind::MethodCall(..) => true,
@@ -2054,3 +2015,29 @@ pub fn is_method_call(expr: &ast::Expr) -> bool {
         _ => false,
     }
 }
+
+#[cfg(test)]
+mod test {
+    use super::last_line_offsetted;
+
+    #[test]
+    fn test_last_line_offsetted() {
+        let lines = "one\n    two";
+        assert_eq!(last_line_offsetted(2, lines), true);
+        assert_eq!(last_line_offsetted(4, lines), false);
+        assert_eq!(last_line_offsetted(6, lines), false);
+
+        let lines = "one    two";
+        assert_eq!(last_line_offsetted(2, lines), false);
+        assert_eq!(last_line_offsetted(0, lines), false);
+
+        let lines = "\ntwo";
+        assert_eq!(last_line_offsetted(2, lines), false);
+        assert_eq!(last_line_offsetted(0, lines), false);
+
+        let lines = "one\n    two      three";
+        assert_eq!(last_line_offsetted(2, lines), true);
+        let lines = "one\n two      three";
+        assert_eq!(last_line_offsetted(2, lines), false);
+    }
+}