]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Format the last expression-statement as expression (#3631)
[rust.git] / src / expr.rs
index 15ffb842ee495e773a3fb16325db18f48b89d0f6..d7640bf98caf451340819bcb43fa0d6cf5a34db4 100644 (file)
@@ -1,67 +1,59 @@
-// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
 use std::borrow::Cow;
 use std::cmp::min;
+use std::iter;
 
-use config::lists::*;
+use itertools::Itertools;
 use syntax::parse::token::DelimToken;
 use syntax::source_map::{BytePos, SourceMap, Span};
 use syntax::{ast, ptr};
 
-use chains::rewrite_chain;
-use closures;
-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, MacroPosition};
-use matches::rewrite_match;
-use overflow::{self, IntoOverflowableItem, OverflowableItem};
-use pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
-use patterns::is_short_pattern;
-use rewrite::{Rewrite, RewriteContext};
-use shape::{Indent, Shape};
-use source_map::{LineRangeUtils, SpanUtils};
-use spanned::Spanned;
-use string::{rewrite_string, StringFormat};
-use types::{rewrite_path, PathContext};
-use utils::{
+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_stmt, wrap_str,
+    semicolon_for_expr, unicode_str_width, 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> {
+    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
         format_expr(self, ExprType::SubExpression, context, shape)
     }
 }
 
 #[derive(Copy, Clone, PartialEq)]
-pub enum ExprType {
+pub(crate) enum ExprType {
     Statement,
     SubExpression,
 }
 
-pub fn format_expr(
+pub(crate) fn format_expr(
     expr: &ast::Expr,
     expr_type: ExprType,
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     shape: Shape,
 ) -> Option<String> {
     skip_out_of_file_lines_range!(context, expr.span);
@@ -69,6 +61,11 @@ 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(
@@ -106,6 +103,7 @@ pub fn format_expr(
             path,
             fields,
             base.as_ref().map(|e| &**e),
+            &expr.attrs,
             expr.span,
             shape,
         ),
@@ -186,9 +184,9 @@ pub fn format_expr(
                 Some("yield".to_string())
             }
         }
-        ast::ExprKind::Closure(capture, asyncness, movability, ref fn_decl, ref body, _) => {
+        ast::ExprKind::Closure(capture, ref is_async, movability, ref fn_decl, ref body, _) => {
             closures::rewrite_closure(
-                capture, asyncness, movability, fn_decl, body, expr.span, context, shape,
+                capture, is_async, movability, fn_decl, body, expr.span, context, shape,
             )
         }
         ast::ExprKind::Try(..) | ast::ExprKind::Field(..) | ast::ExprKind::MethodCall(..) => {
@@ -244,7 +242,7 @@ pub fn format_expr(
                 ast::RangeLimits::Closed => "..=",
             };
 
-            fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {
+            fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
                 match lhs.node {
                     ast::ExprKind::Lit(ref lit) => match lit.node {
                         ast::LitKind::FloatUnsuffixed(..) => {
@@ -338,10 +336,6 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
                 ))
             }
         }
-        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 "
@@ -374,6 +368,19 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
                 ))
             }
         }
+        ast::ExprKind::Await(ast::AwaitOrigin::FieldLike, _) => rewrite_chain(expr, context, shape),
+        ast::ExprKind::Await(ast::AwaitOrigin::MacroLike, ref nested) => {
+            overflow::rewrite_with_parens(
+                context,
+                "await!",
+                iter::once(nested),
+                shape,
+                expr.span,
+                context.config.max_width(),
+                None,
+            )
+        }
+        ast::ExprKind::Err => None,
     };
 
     expr_rw
@@ -389,11 +396,11 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
         })
 }
 
-pub fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
+pub(crate) fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
     name: &'a str,
     exprs: impl Iterator<Item = &'a T>,
     span: Span,
-    context: &'a RewriteContext,
+    context: &'a RewriteContext<'_>,
     shape: Shape,
     force_separator_tactic: Option<SeparatorTactic>,
     delim_token: Option<DelimToken>,
@@ -410,22 +417,23 @@ pub fn rewrite_array<'a, T: 'a + IntoOverflowableItem<'a>>(
 }
 
 fn rewrite_empty_block(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
     label: Option<ast::Label>,
     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.source_map)
-        && shape.width >= 2
-    {
+    if !block_contains_comment(block, context.source_map) && shape.width >= 2 {
         return Some(format!("{}{}{{}}", prefix, label_str));
     }
 
@@ -446,7 +454,7 @@ fn rewrite_empty_block(
     None
 }
 
-fn block_prefix(context: &RewriteContext, block: &ast::Block, shape: Shape) -> Option<String> {
+fn block_prefix(context: &RewriteContext<'_>, block: &ast::Block, shape: Shape) -> Option<String> {
     Some(match block.rules {
         ast::BlockCheckMode::Unsafe(..) => {
             let snippet = context.snippet(block.span);
@@ -475,7 +483,7 @@ fn block_prefix(context: &RewriteContext, block: &ast::Block, shape: Shape) -> O
 }
 
 fn rewrite_single_line_block(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     prefix: &str,
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
@@ -494,8 +502,8 @@ fn rewrite_single_line_block(
     None
 }
 
-pub fn rewrite_block_with_visitor(
-    context: &RewriteContext,
+pub(crate) fn rewrite_block_with_visitor(
+    context: &RewriteContext<'_>,
     prefix: &str,
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
@@ -510,13 +518,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);
@@ -526,7 +534,7 @@ pub fn rewrite_block_with_visitor(
 }
 
 impl Rewrite for ast::Block {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
+    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
         rewrite_block(self, None, None, context, shape)
     }
 }
@@ -535,7 +543,7 @@ fn rewrite_block(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
     label: Option<ast::Label>,
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     shape: Shape,
 ) -> Option<String> {
     let prefix = block_prefix(context, block, shape)?;
@@ -560,30 +568,12 @@ fn rewrite_block(
     result
 }
 
-impl Rewrite for ast::Stmt {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        skip_out_of_file_lines_range!(context, self.span());
-
-        let result = match self.node {
-            ast::StmtKind::Local(ref local) => local.rewrite(context, shape),
-            ast::StmtKind::Expr(ref ex) | ast::StmtKind::Semi(ref ex) => {
-                let suffix = if semicolon_for_stmt(context, self) {
-                    ";"
-                } else {
-                    ""
-                };
-
-                let shape = shape.sub_width(suffix.len())?;
-                format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix)
-            }
-            ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
-        };
-        result.and_then(|res| recover_comment_removed(res, self.span(), context))
-    }
-}
-
 // Rewrite condition if the given expression has one.
-pub fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Option<String> {
+pub(crate) fn rewrite_cond(
+    context: &RewriteContext<'_>,
+    expr: &ast::Expr,
+    shape: Shape,
+) -> Option<String> {
     match expr.node {
         ast::ExprKind::Match(ref cond, _) => {
             // `match `cond` {`
@@ -615,12 +605,12 @@ struct ControlFlow<'a> {
     matcher: &'a str,
     connector: &'a str,
     allow_single_line: bool,
-    // True if this is an `if` expression in an `else if` :-( hacky
+    // HACK: `true` if this is an `if` expression in an `else if`.
     nested_if: bool,
     span: Span,
 }
 
-fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow> {
+fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<'_>> {
     match expr.node {
         ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if(
             cond,
@@ -663,11 +653,7 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow>
 }
 
 fn choose_matcher(pats: &[&ast::Pat]) -> &'static str {
-    if pats.is_empty() {
-        ""
-    } else {
-        "let"
-    }
+    if pats.is_empty() { "" } else { "let" }
 }
 
 impl<'a> ControlFlow<'a> {
@@ -760,7 +746,7 @@ fn new_for(
     fn rewrite_single_line(
         &self,
         pat_expr_str: &str,
-        context: &RewriteContext,
+        context: &RewriteContext<'_>,
         width: usize,
     ) -> Option<String> {
         assert!(self.allow_single_line);
@@ -801,10 +787,24 @@ 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,
-        context: &RewriteContext,
+        context: &RewriteContext<'_>,
         expr: &ast::Expr,
         shape: Shape,
         offset: usize,
@@ -844,7 +844,7 @@ fn rewrite_pat_expr(
 
     fn rewrite_cond(
         &self,
-        context: &RewriteContext,
+        context: &RewriteContext<'_>,
         shape: Shape,
         alt_block_sep: &str,
     ) -> Option<(String, usize)> {
@@ -885,7 +885,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
@@ -979,7 +980,7 @@ fn rewrite_cond(
 }
 
 impl<'a> Rewrite for ControlFlow<'a> {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
+    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
         debug!("ControlFlow::rewrite {:?} {:?}", self, shape);
 
         let alt_block_sep = &shape.indent.to_string_with_newline(context.config);
@@ -1104,7 +1105,7 @@ fn rewrite_label(opt_label: Option<ast::Label>) -> Cow<'static, str> {
     }
 }
 
-fn extract_comment(span: Span, context: &RewriteContext, shape: Shape) -> Option<String> {
+fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
     match rewrite_missing_comment(span, shape, context) {
         Some(ref comment) if !comment.is_empty() => Some(format!(
             "{indent}{}{indent}",
@@ -1115,7 +1116,7 @@ fn extract_comment(span: Span, context: &RewriteContext, shape: Shape) -> Option
     }
 }
 
-pub fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) -> bool {
+pub(crate) fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) -> bool {
     let snippet = source_map.span_to_snippet(block.span).unwrap();
     contains_comment(&snippet)
 }
@@ -1124,7 +1125,7 @@ pub fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) -> boo
 // attributes.
 // FIXME: incorrectly returns false when comment is contained completely within
 // the expression.
-pub fn is_simple_block(
+pub(crate) fn is_simple_block(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
     source_map: &SourceMap,
@@ -1137,7 +1138,7 @@ pub fn is_simple_block(
 
 /// Checks whether a block contains at most one statement or expression, and no
 /// comments or attributes.
-pub fn is_simple_block_stmt(
+pub(crate) fn is_simple_block_stmt(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
     source_map: &SourceMap,
@@ -1149,7 +1150,7 @@ pub fn is_simple_block_stmt(
 
 /// Checks whether a block contains no statements, expressions, comments, or
 /// inner attributes.
-pub fn is_empty_block(
+pub(crate) fn is_empty_block(
     block: &ast::Block,
     attrs: Option<&[ast::Attribute]>,
     source_map: &SourceMap,
@@ -1159,14 +1160,14 @@ pub fn is_empty_block(
         && attrs.map_or(true, |a| inner_attributes(a).is_empty())
 }
 
-pub fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
+pub(crate) fn stmt_is_expr(stmt: &ast::Stmt) -> bool {
     match stmt.node {
         ast::StmtKind::Expr(..) => true,
         _ => false,
     }
 }
 
-pub fn is_unsafe_block(block: &ast::Block) -> bool {
+pub(crate) fn is_unsafe_block(block: &ast::Block) -> bool {
     if let ast::BlockCheckMode::Unsafe(..) = block.rules {
         true
     } else {
@@ -1174,8 +1175,8 @@ pub fn is_unsafe_block(block: &ast::Block) -> bool {
     }
 }
 
-pub fn rewrite_multiple_patterns(
-    context: &RewriteContext,
+pub(crate) fn rewrite_multiple_patterns(
+    context: &RewriteContext<'_>,
     pats: &[&ast::Pat],
     shape: Shape,
 ) -> Option<String> {
@@ -1207,7 +1208,11 @@ pub fn rewrite_multiple_patterns(
     write_list(&items, &fmt)
 }
 
-pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option<String> {
+pub(crate) fn rewrite_literal(
+    context: &RewriteContext<'_>,
+    l: &ast::Lit,
+    shape: Shape,
+) -> Option<String> {
     match l.node {
         ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
         _ => wrap_str(
@@ -1218,14 +1223,13 @@ pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) ->
     }
 }
 
-fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Option<String> {
+fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) -> Option<String> {
     let string_lit = context.snippet(span);
 
     if !context.config.format_strings() {
         if string_lit
             .lines()
-            .rev()
-            .skip(1)
+            .dropping_back(1)
             .all(|line| line.ends_with('\\'))
         {
             let new_indent = shape.visual_indent(1).indent;
@@ -1236,14 +1240,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<_>>()
                     .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);
         }
@@ -1259,7 +1267,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
     )
 }
 
-fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option<SeparatorTactic> {
+fn choose_separator_tactic(context: &RewriteContext<'_>, span: Span) -> Option<SeparatorTactic> {
     if context.inside_macro() {
         if span_ends_with_comma(context, span) {
             Some(SeparatorTactic::Always)
@@ -1271,8 +1279,8 @@ fn choose_separator_tactic(context: &RewriteContext, span: Span) -> Option<Separ
     }
 }
 
-pub fn rewrite_call(
-    context: &RewriteContext,
+pub(crate) fn rewrite_call(
+    context: &RewriteContext<'_>,
     callee: &str,
     args: &[ptr::P<ast::Expr>],
     span: Span,
@@ -1289,7 +1297,7 @@ pub fn rewrite_call(
     )
 }
 
-pub fn is_simple_expr(expr: &ast::Expr) -> bool {
+pub(crate) 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,
@@ -1307,15 +1315,21 @@ pub fn is_simple_expr(expr: &ast::Expr) -> bool {
     }
 }
 
-pub fn is_every_expr_simple(lists: &[OverflowableItem]) -> bool {
+pub(crate) 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 {
+pub(crate) fn can_be_overflowed_expr(
+    context: &RewriteContext<'_>,
+    expr: &ast::Expr,
+    args_len: usize,
+) -> bool {
     match expr.node {
+        _ if !expr.attrs.is_empty() => false,
         ast::ExprKind::Match(..) => {
             (context.use_block_indent() && args_len == 1)
                 || (context.config.indent_style() == IndentStyle::Visual && args_len > 1)
+                || context.config.overflow_delimited_expr()
         }
         ast::ExprKind::If(..)
         | ast::ExprKind::IfLet(..)
@@ -1325,15 +1339,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
+
+        // Handle always block-like expressions
+        ast::ExprKind::Async(..) | 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::Array(..)
-        | ast::ExprKind::Call(..)
-        | ast::ExprKind::Mac(..)
-        | ast::ExprKind::MethodCall(..)
-        | ast::ExprKind::Struct(..)
-        | ast::ExprKind::Tup(..) => 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
+        }
+
+        // Handle unary-like expressions
         ast::ExprKind::AddrOf(_, ref expr)
         | ast::ExprKind::Box(ref expr)
         | ast::ExprKind::Try(ref expr)
@@ -1343,7 +1370,7 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
     }
 }
 
-pub fn is_nested_call(expr: &ast::Expr) -> bool {
+pub(crate) fn is_nested_call(expr: &ast::Expr) -> bool {
     match expr.node {
         ast::ExprKind::Call(..) | ast::ExprKind::Mac(..) => true,
         ast::ExprKind::AddrOf(_, ref expr)
@@ -1355,10 +1382,10 @@ pub fn is_nested_call(expr: &ast::Expr) -> bool {
     }
 }
 
-/// Return true if a function call or a method call represented by the given span ends with a
+/// Returns `true` if a function call or a method call represented by the given span ends with a
 /// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
 /// comma from macro can potentially break the code.
-pub fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
+pub(crate) fn span_ends_with_comma(context: &RewriteContext<'_>, span: Span) -> bool {
     let mut result: bool = Default::default();
     let mut prev_char: char = Default::default();
     let closing_delimiters = &[')', '}', ']'];
@@ -1379,7 +1406,7 @@ pub fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
 }
 
 fn rewrite_paren(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     mut subexpr: &ast::Expr,
     shape: Shape,
     mut span: Span,
@@ -1416,14 +1443,14 @@ fn rewrite_paren(
     let subexpr_str = subexpr.rewrite(context, sub_shape)?;
     let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
     if fits_single_line {
-        Some(format!("({}{}{})", pre_comment, &subexpr_str, post_comment))
+        Some(format!("({}{}{})", pre_comment, subexpr_str, post_comment))
     } else {
         rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
     }
 }
 
 fn rewrite_paren_in_multi_line(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     subexpr: &ast::Expr,
     shape: Shape,
     pre_span: Span,
@@ -1456,7 +1483,7 @@ fn rewrite_paren_in_multi_line(
 fn rewrite_index(
     expr: &ast::Expr,
     index: &ast::Expr,
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     shape: Shape,
 ) -> Option<String> {
     let expr_str = expr.rewrite(context, shape)?;
@@ -1517,10 +1544,11 @@ fn struct_lit_can_be_aligned(fields: &[ast::Field], base: Option<&ast::Expr>) ->
 }
 
 fn rewrite_struct_lit<'a>(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     path: &ast::Path,
     fields: &'a [ast::Field],
     base: Option<&'a ast::Expr>,
+    attrs: &[ast::Attribute],
     span: Span,
     shape: Shape,
 ) -> Option<String> {
@@ -1550,17 +1578,17 @@ enum StructLitField<'a> {
         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));
 
-        let span_lo = |item: &StructLitField| match *item {
+        let span_lo = |item: &StructLitField<'_>| match *item {
             StructLitField::Regular(field) => field.span().lo(),
             StructLitField::Base(expr) => {
                 let last_field_hi = fields.last().map_or(span.lo(), |field| field.span.hi());
@@ -1569,11 +1597,11 @@ enum StructLitField<'a> {
                 last_field_hi + BytePos(pos as u32)
             }
         };
-        let span_hi = |item: &StructLitField| match *item {
+        let span_hi = |item: &StructLitField<'_>| match *item {
             StructLitField::Regular(field) => field.span().hi(),
             StructLitField::Base(expr) => expr.span.hi(),
         };
-        let rewrite = |item: &StructLitField| match *item {
+        let rewrite = |item: &StructLitField<'_>| match *item {
             StructLitField::Regular(field) => {
                 // The 1 taken from the v_budget is for the comma.
                 rewrite_field(context, field, v_shape.sub_width(1)?, 0)
@@ -1615,43 +1643,58 @@ enum StructLitField<'a> {
         write_list(&item_vec, &fmt)?
     };
 
-    let fields_str = wrap_struct_field(context, &fields_str, shape, v_shape, one_line_width);
+    let fields_str =
+        wrap_struct_field(context, &attrs, &fields_str, shape, v_shape, one_line_width)?;
     Some(format!("{} {{{}}}", path_str, fields_str))
 
     // FIXME if context.config.indent_style() == Visual, but we run out
     // of space, we should fall back to BlockIndent.
 }
 
-pub fn wrap_struct_field(
-    context: &RewriteContext,
+pub(crate) fn wrap_struct_field(
+    context: &RewriteContext<'_>,
+    attrs: &[ast::Attribute],
     fields_str: &str,
     shape: Shape,
     nested_shape: Shape,
     one_line_width: usize,
-) -> String {
-    if context.config.indent_style() == IndentStyle::Block
+) -> Option<String> {
+    let should_vertical = context.config.indent_style() == IndentStyle::Block
         && (fields_str.contains('\n')
             || !context.config.struct_lit_single_line()
-            || fields_str.len() > one_line_width)
-    {
-        format!(
-            "{}{}{}",
+            || fields_str.len() > one_line_width);
+
+    let inner_attrs = &inner_attributes(attrs);
+    if inner_attrs.is_empty() {
+        if should_vertical {
+            Some(format!(
+                "{}{}{}",
+                nested_shape.indent.to_string_with_newline(context.config),
+                fields_str,
+                shape.indent.to_string_with_newline(context.config)
+            ))
+        } else {
+            // One liner or visual indent.
+            Some(format!(" {} ", fields_str))
+        }
+    } else {
+        Some(format!(
+            "{}{}{}{}{}",
+            nested_shape.indent.to_string_with_newline(context.config),
+            inner_attrs.rewrite(context, shape)?,
             nested_shape.indent.to_string_with_newline(context.config),
             fields_str,
             shape.indent.to_string_with_newline(context.config)
-        )
-    } else {
-        // One liner or visual indent.
-        format!(" {} ", fields_str)
+        ))
     }
 }
 
-pub fn struct_lit_field_separator(config: &Config) -> &str {
-    colon_spaces(config.space_before_colon(), config.space_after_colon())
+pub(crate) fn struct_lit_field_separator(config: &Config) -> &str {
+    colon_spaces(config)
 }
 
-pub fn rewrite_field(
-    context: &RewriteContext,
+pub(crate) fn rewrite_field(
+    context: &RewriteContext<'_>,
     field: &ast::Field,
     shape: Shape,
     prefix_max_width: usize,
@@ -1700,7 +1743,7 @@ pub fn rewrite_field(
 }
 
 fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     mut items: impl Iterator<Item = &'a T>,
     span: Span,
     shape: Shape,
@@ -1747,8 +1790,8 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
     Some(format!("({})", list_str))
 }
 
-pub fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
-    context: &'a RewriteContext,
+pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
+    context: &'a RewriteContext<'_>,
     items: impl Iterator<Item = &'a T>,
     span: Span,
     shape: Shape,
@@ -1782,8 +1825,8 @@ pub fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
     }
 }
 
-pub fn rewrite_unary_prefix<R: Rewrite>(
-    context: &RewriteContext,
+pub(crate) fn rewrite_unary_prefix<R: Rewrite>(
+    context: &RewriteContext<'_>,
     prefix: &str,
     rewrite: &R,
     shape: Shape,
@@ -1795,8 +1838,8 @@ pub fn rewrite_unary_prefix<R: Rewrite>(
 
 // FIXME: this is probably not correct for multi-line Rewrites. we should
 // subtract suffix.len() from the last line budget, not the first!
-pub fn rewrite_unary_suffix<R: Rewrite>(
-    context: &RewriteContext,
+pub(crate) fn rewrite_unary_suffix<R: Rewrite>(
+    context: &RewriteContext<'_>,
     suffix: &str,
     rewrite: &R,
     shape: Shape,
@@ -1810,7 +1853,7 @@ pub fn rewrite_unary_suffix<R: Rewrite>(
 }
 
 fn rewrite_unary_op(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     op: ast::UnOp,
     expr: &ast::Expr,
     shape: Shape,
@@ -1820,7 +1863,7 @@ fn rewrite_unary_op(
 }
 
 fn rewrite_assignment(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     lhs: &ast::Expr,
     rhs: &ast::Expr,
     op: Option<&ast::BinOp>,
@@ -1840,17 +1883,20 @@ fn rewrite_assignment(
 
 /// Controls where to put the rhs.
 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
-pub enum RhsTactics {
+pub(crate) enum RhsTactics {
     /// Use heuristics.
     Default,
     /// Put the rhs on the next line if it uses multiple line, without extra indentation.
     ForceNextLineWithoutIndent,
+    /// Allow overflowing max width if neither `Default` nor `ForceNextLineWithoutIndent`
+    /// did not work.
+    AllowOverflow,
 }
 
 // The left hand side must contain everything up to, and including, the
 // assignment operator.
-pub fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
-    context: &RewriteContext,
+pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
+    context: &RewriteContext<'_>,
     lhs: S,
     ex: &R,
     shape: Shape,
@@ -1858,8 +1904,8 @@ pub fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
     rewrite_assign_rhs_with(context, lhs, ex, shape, RhsTactics::Default)
 }
 
-pub fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
-    context: &RewriteContext,
+pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
+    context: &RewriteContext<'_>,
     lhs: S,
     ex: &R,
     shape: Shape,
@@ -1888,14 +1934,16 @@ pub fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
 }
 
 fn choose_rhs<R: Rewrite>(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     expr: &R,
     shape: Shape,
     orig_rhs: Option<String>,
     rhs_tactics: RhsTactics,
 ) -> Option<String> {
     match orig_rhs {
-        Some(ref new_str) if !new_str.contains('\n') && new_str.len() <= shape.width => {
+        Some(ref new_str)
+            if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
+        {
             Some(format!(" {}", new_str))
         }
         _ => {
@@ -1921,6 +1969,10 @@ fn choose_rhs<R: Rewrite>(
                     Some(format!("{}{}", new_indent_str, new_rhs))
                 }
                 (None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
+                (None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
+                    let shape = shape.infinite_width();
+                    expr.rewrite(context, shape).map(|s| format!(" {}", s))
+                }
                 (None, None) => None,
                 (Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
             }
@@ -1929,7 +1981,7 @@ fn choose_rhs<R: Rewrite>(
 }
 
 fn shape_from_rhs_tactic(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     shape: Shape,
     rhs_tactic: RhsTactics,
 ) -> Option<Shape> {
@@ -1937,14 +1989,27 @@ fn shape_from_rhs_tactic(
         RhsTactics::ForceNextLineWithoutIndent => shape
             .with_max_width(context.config)
             .sub_width(shape.indent.width()),
-        RhsTactics::Default => {
+        RhsTactics::Default | RhsTactics::AllowOverflow => {
             Shape::indented(shape.indent.block_indent(context.config), context.config)
                 .sub_width(shape.rhs_overhead(context.config))
         }
     }
 }
 
-pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTactics) -> bool {
+/// Returns true if formatting next_line_rhs is better on a new line when compared to the
+/// original's line formatting.
+///
+/// It is considered better if:
+/// 1. the tactic is ForceNextLineWithoutIndent
+/// 2. next_line_rhs doesn't have newlines
+/// 3. the original line has more newlines than next_line_rhs
+/// 4. the original formatting of the first line ends with `(`, `{`, or `[` and next_line_rhs
+///    doesn't
+pub(crate) fn prefer_next_line(
+    orig_rhs: &str,
+    next_line_rhs: &str,
+    rhs_tactics: RhsTactics,
+) -> bool {
     rhs_tactics == RhsTactics::ForceNextLineWithoutIndent
         || !next_line_rhs.contains('\n')
         || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
@@ -1954,7 +2019,7 @@ pub fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str, rhs_tactics: RhsTac
 }
 
 fn rewrite_expr_addrof(
-    context: &RewriteContext,
+    context: &RewriteContext<'_>,
     mutability: ast::Mutability,
     expr: &ast::Expr,
     shape: Shape,
@@ -1966,7 +2031,7 @@ fn rewrite_expr_addrof(
     rewrite_unary_prefix(context, operator_str, expr, shape)
 }
 
-pub fn is_method_call(expr: &ast::Expr) -> bool {
+pub(crate) fn is_method_call(expr: &ast::Expr) -> bool {
     match expr.node {
         ast::ExprKind::MethodCall(..) => true,
         ast::ExprKind::AddrOf(_, ref expr)
@@ -1977,3 +2042,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);
+    }
+}