]> git.lizzy.rs Git - rust.git/blobdiff - src/expr.rs
Merge pull request #3129 from otavio/issue-3104
[rust.git] / src / expr.rs
index 06c9d3559064f9953186452099f77a6904b04e7b..fbbc592e547228c2b1175bd008a7013ff8ea5017 100644 (file)
@@ -39,9 +39,9 @@
 use string::{rewrite_string, StringFormat};
 use types::{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,
+    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,
 };
 use vertical::rewrite_with_alignment;
 use visitor::FmtVisitor;
@@ -273,11 +273,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))),
                 )
             };
 
@@ -803,6 +801,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,
@@ -887,7 +899,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
@@ -900,10 +913,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));
                 }
@@ -1256,57 +1270,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(callee_str: &str, args: &[OverflowableItem]) -> 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) {
@@ -1374,8 +1341,7 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
             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
+            context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
         }
         ast::ExprKind::Array(..)
         | ast::ExprKind::Call(..)
@@ -1436,13 +1402,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)?;
 
@@ -1458,20 +1426,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,
@@ -1628,7 +1624,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)?
@@ -1835,12 +1831,7 @@ fn rewrite_unary_op(
     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(
@@ -1958,7 +1949,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))
@@ -1999,3 +1992,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);
+    }
+}