]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #1442 from topecongiro/poor-formatting/trailing-comma
authorNick Cameron <nrc@ncameron.org>
Sun, 7 May 2017 22:45:03 +0000 (10:45 +1200)
committerGitHub <noreply@github.com>
Sun, 7 May 2017 22:45:03 +0000 (10:45 +1200)
Remove comma from function definition with a single argument

1  2 
src/expr.rs
src/items.rs
tests/target/expr-block.rs

diff --combined src/expr.rs
index ed5547034109ef76fd02505b74009b4ab9863b1f,f50b383b943f58e66073f3191f94cf0975ddfdd8..ab831a37431323d723066c758b219a262b52cde4
@@@ -26,7 -26,7 +26,7 @@@ use utils::{extra_offset, last_line_wid
              semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
              colon_spaces};
  use visitor::FmtVisitor;
 -use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle};
 +use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
  use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
  use types::{rewrite_path, PathContext};
  use items::{span_lo_for_arg, span_hi_for_arg};
@@@ -314,12 -314,8 +314,12 @@@ pub fn rewrite_pair<LHS, RHS>(lhs: &LHS
                                    .max_width
                                    .checked_sub(shape.used_width() + prefix.len() +
                                                 infix.len()));
 -    let rhs_shape = try_opt!(shape.sub_width(suffix.len() + prefix.len()))
 -        .visual_indent(prefix.len());
 +    let rhs_shape = match context.config.control_style {
 +        Style::Default => {
 +            try_opt!(shape.sub_width(suffix.len() + prefix.len())).visual_indent(prefix.len())
 +        }
 +        Style::Rfc => try_opt!(shape.block_left(context.config.tab_spaces)),
 +    };
  
      let rhs_result = try_opt!(rhs.rewrite(context, rhs_shape));
      let lhs_result = try_opt!(lhs.rewrite(context,
@@@ -530,11 -526,13 +530,11 @@@ fn rewrite_closure(capture: ast::Captur
              // We need braces, but we might still prefer a one-liner.
              let stmt = &block.stmts[0];
              // 4 = braces and spaces.
 -            let mut rewrite = stmt.rewrite(context, try_opt!(body_shape.sub_width(4)));
 -
 -            // Checks if rewrite succeeded and fits on a single line.
 -            rewrite = and_one_line(rewrite);
 -
 -            if let Some(rewrite) = rewrite {
 -                return Some(format!("{} {{ {} }}", prefix, rewrite));
 +            if let Some(body_shape) = body_shape.sub_width(4) {
 +                // Checks if rewrite succeeded and fits on a single line.
 +                if let Some(rewrite) = and_one_line(stmt.rewrite(context, body_shape)) {
 +                    return Some(format!("{} {{ {} }}", prefix, rewrite));
 +                }
              }
          }
  
  
          // The body of the closure is big enough to be block indented, that
          // means we must re-format.
 -        let block_shape = Shape {
 -            width: context.config.max_width - shape.block().indent.width(),
 -            ..shape.block()
 -        };
 +        let block_shape = shape.block().with_max_width(context.config);
          let block_str = try_opt!(block.rewrite(&context, block_shape));
          Some(format!("{} {}",
                       prefix,
@@@ -883,10 -884,7 +883,10 @@@ impl<'a> Rewrite for ControlFlow<'a> 
  
          let pat_expr_string = match self.cond {
              Some(cond) => {
 -                let mut cond_shape = try_opt!(constr_shape.shrink_left(add_offset));
 +                let mut cond_shape = match context.config.control_style {
 +                    Style::Default => try_opt!(constr_shape.shrink_left(add_offset)),
 +                    Style::Rfc => constr_shape,
 +                };
                  if context.config.control_brace_style != ControlBraceStyle::AlwaysNextLine {
                      // 2 = " {".len()
                      cond_shape = try_opt!(cond_shape.sub_width(2));
              None => String::new(),
          };
  
 +        let force_newline_brace = context.config.control_style == Style::Rfc &&
 +                                  pat_expr_string.contains('\n');
 +
          // Try to format if-else on single line.
          if self.allow_single_line && context.config.single_line_if_else_max_width > 0 {
              let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
                              &shape.indent.block_only().to_string(context.config);
          let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
              ""
 -        } else if context.config.control_brace_style ==
 -                  ControlBraceStyle::AlwaysNextLine {
 +        } else if context.config.control_brace_style == ControlBraceStyle::AlwaysNextLine ||
 +                  force_newline_brace {
              alt_block_sep.as_str()
          } else {
              " "
@@@ -1207,7 -1202,11 +1207,7 @@@ fn rewrite_match(context: &RewriteConte
          result.push('\n');
          result.push_str(&arm_indent_str);
  
 -        let arm_str = arm.rewrite(&context,
 -                                  Shape {
 -                                      width: context.config.max_width - arm_shape.indent.width(),
 -                                      ..arm_shape
 -                                  });
 +        let arm_str = arm.rewrite(&context, arm_shape.with_max_width(context.config));
          if let Some(ref arm_str) = arm_str {
              result.push_str(arm_str);
          } else {
@@@ -1314,7 -1313,10 +1314,7 @@@ impl Rewrite for ast::Arm 
          let pats_str = try_opt!(write_list(items, &fmt));
  
          let guard_shape = if pats_str.contains('\n') {
 -            Shape {
 -                width: context.config.max_width - shape.indent.width(),
 -                ..shape
 -            }
 +            shape.with_max_width(context.config)
          } else {
              shape
          };
@@@ -1492,7 -1494,7 +1492,7 @@@ fn rewrite_pat_expr(context: &RewriteCo
                      connector: &str,
                      shape: Shape)
                      -> Option<String> {
 -    debug!("rewrite_pat_expr {:?} {:?}", shape, pat);
 +    debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, pat, expr);
      let mut result = match pat {
          Some(pat) => {
              let matcher = if matcher.is_empty() {
@@@ -1750,7 -1752,8 +1750,8 @@@ fn rewrite_call_inner<R>(context: &Rewr
          tactic: tactic,
          separator: ",",
          trailing_separator: if force_no_trailing_comma ||
-                                context.config.fn_call_style == IndentStyle::Visual {
+                                context.config.fn_call_style == IndentStyle::Visual ||
+                                args.len() <= 1 {
              SeparatorTactic::Never
          } else {
              context.config.trailing_comma
diff --combined src/items.rs
index 4b49d0a93f996d24f2ad0c878885de3cf9602e7e,6e5f4b0802a74ba179514ee9ead681c95dba1cd3..a99797579c366247e61fff8dc64085a694847377
@@@ -208,8 -208,8 +208,8 @@@ impl<'a> FmtVisitor<'a> 
                  let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident);
                  let offset = self.block_indent + prefix.len();
                  // 1 = ;
 -                let width = self.config.max_width - offset.width() - 1;
 -                let rewrite = ty.rewrite(&self.get_context(), Shape::legacy(width, offset));
 +                let shape = Shape::indented(offset, self.config).sub_width(1).unwrap();
 +                let rewrite = ty.rewrite(&self.get_context(), shape);
  
                  match rewrite {
                      Some(result) => {
                              let suffix = if semicolon_for_expr(e) { ";" } else { "" };
  
                              e.rewrite(&self.get_context(),
 -                                         Shape::legacy(self.config.max_width -
 -                                                       self.block_indent.width(),
 -                                                       self.block_indent))
 +                                         Shape::indented(self.block_indent, self.config))
                                  .map(|s| s + suffix)
                                  .or_else(|| Some(self.snippet(e.span)))
                          }
                          None => {
                              stmt.rewrite(&self.get_context(),
 -                                         Shape::legacy(self.config.max_width -
 -                                                       self.block_indent.width(),
 -                                                       self.block_indent))
 +                                         Shape::indented(self.block_indent, self.config))
                          }
                      }
                  } else {
                                   body_lo,
                                   body_hi);
  
 -        let budget = self.config.max_width - self.block_indent.width() - 2;
 +        let shape = Shape::indented(self.block_indent, self.config)
 +            .sub_width(2)
 +            .unwrap();
          let fmt = ListFormatting {
              tactic: DefinitiveListTactic::Vertical,
              separator: ",",
              trailing_separator: self.config.trailing_comma,
 -            shape: Shape::legacy(budget, self.block_indent),
 +            shape: shape,
              ends_with_newline: true,
              config: self.config,
          };
                                        .node
                                        .attrs
                                        .rewrite(&self.get_context(),
 -                                               Shape::legacy(self.config.max_width -
 -                                                             indent.width(),
 -                                                             indent)));
 +                                               Shape::indented(indent, self.config)));
          if !result.is_empty() {
              result.push('\n');
              result.push_str(&indent.to_string(self.config));
  
                  wrap_str(tag,
                           self.config.max_width,
 -                         Shape::legacy(self.config.max_width - indent.width(), indent))
 +                         Shape::indented(indent, self.config))
              }
          };
  
@@@ -639,7 -643,7 +639,7 @@@ fn format_impl_ref_and_type(context: &R
              None => self_ty.span.lo,
          };
          let generics_indent = offset + last_line_width(&result);
 -        let shape = try_opt!(generics_shape(context.config, generics_indent));
 +        let shape = Shape::indented(generics_indent, context.config);
          let generics_str = try_opt!(rewrite_generics(context, generics, shape, mk_sp(lo, hi)));
          result.push_str(&generics_str);
  
          // Can't fit the self type on what's left of the line, so start a new one.
          let indent = offset.block_indent(context.config);
          result.push_str(&format!("\n{}", indent.to_string(context.config)));
 -        let budget = try_opt!(context.config.max_width.checked_sub(indent.width()));
 -        result.push_str(&*try_opt!(self_ty.rewrite(context, Shape::legacy(budget, indent))));
 +        result.push_str(&*try_opt!(self_ty.rewrite(context,
 +                                                   Shape::indented(indent, context.config))));
          Some(result)
      } else {
          unreachable!();
@@@ -751,7 -755,7 +751,7 @@@ pub fn format_trait(context: &RewriteCo
          let body_lo = context.codemap.span_after(item.span, "{");
  
          let generics_indent = offset + last_line_width(&result);
 -        let shape = try_opt!(generics_shape(context.config, generics_indent));
 +        let shape = Shape::indented(generics_indent, context.config);
          let generics_str =
              try_opt!(rewrite_generics(context, generics, shape, mk_sp(item.span.lo, body_lo)));
          result.push_str(&generics_str);
@@@ -996,7 -1000,7 +996,7 @@@ fn format_tuple_struct(context: &Rewrit
      let where_clause_str = match generics {
          Some(generics) => {
              let generics_indent = offset + last_line_width(&header_str);
 -            let shape = try_opt!(generics_shape(context.config, generics_indent));
 +            let shape = Shape::indented(generics_indent, context.config);
              let generics_str =
                  try_opt!(rewrite_generics(context, generics, shape, mk_sp(span.lo, body_lo)));
              result.push_str(&generics_str);
@@@ -1127,7 -1131,7 +1127,7 @@@ pub fn rewrite_type_alias(context: &Rew
  
      let generics_indent = indent + result.len();
      let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
 -    let shape = try_opt!(try_opt!(generics_shape(context.config, generics_indent))
 +    let shape = try_opt!(Shape::indented(generics_indent, context.config)
                               .sub_width(" =".len()));
      let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
  
@@@ -1201,9 -1205,11 +1201,9 @@@ impl Rewrite for ast::StructField 
  
          let name = self.ident;
          let vis = format_visibility(&self.vis);
 -        let mut attr_str = try_opt!(self.attrs
 -                                        .rewrite(context,
 -                                                 Shape::legacy(context.config.max_width -
 -                                                               shape.indent.width(),
 -                                                               shape.indent)));
 +        let mut attr_str =
 +            try_opt!(self.attrs
 +                         .rewrite(context, Shape::indented(shape.indent, context.config)));
          if !attr_str.is_empty() {
              attr_str.push('\n');
              attr_str.push_str(&shape.indent.to_string(context.config));
  
          let type_offset = shape.indent.block_indent(context.config);
          let rewrite_type_in_next_line = || {
 -            let budget = try_opt!(context.config.max_width.checked_sub(type_offset.width()));
 -            self.ty.rewrite(context, Shape::legacy(budget, type_offset))
 +            self.ty
 +                .rewrite(context, Shape::indented(type_offset, context.config))
          };
  
          let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
@@@ -1549,7 -1555,7 +1549,7 @@@ fn rewrite_fn_base(context: &RewriteCon
      // Generics.
      let generics_indent = indent + last_line_width(&result);
      let generics_span = mk_sp(span.lo, span_for_return(&fd.output).lo);
 -    let shape = try_opt!(generics_shape(context.config, generics_indent));
 +    let shape = Shape::indented(generics_indent, context.config);
      let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
      result.push_str(&generics_str);
  
      // Note that the width and indent don't really matter, we'll re-layout the
      // return type later anyway.
      let ret_str = try_opt!(fd.output
 -                               .rewrite(&context,
 -                                        Shape::legacy(context.config.max_width -
 -                                                      indent.width(),
 -                                                      indent)));
 +                               .rewrite(&context, Shape::indented(indent, context.config)));
  
      let multi_line_ret_str = ret_str.contains('\n');
      let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
                                          indent,
                                          arg_indent,
                                          args_span,
-                                         fd.variadic));
+                                         fd.variadic,
+                                         generics_str.contains('\n')));
  
      let multi_line_arg_str = arg_str.contains('\n');
  
          if multi_line_ret_str || ret_should_indent {
              // Now that we know the proper indent and width, we need to
              // re-layout the return type.
 -            let budget = try_opt!(context.config.max_width.checked_sub(ret_indent.width()));
              let ret_str = try_opt!(fd.output
 -                                       .rewrite(context, Shape::legacy(budget, ret_indent)));
 +                                       .rewrite(context,
 +                                                Shape::indented(ret_indent, context.config)));
              result.push_str(&ret_str);
          } else {
              result.push_str(&ret_str);
          }
      }
  
 -    let budget = try_opt!(context.config.max_width.checked_sub(indent.block_indent));
      let where_clause_str = try_opt!(rewrite_where_clause(context,
                                                           where_clause,
                                                           context.config.fn_brace_style,
 -                                                         Shape::legacy(budget, indent),
 +                                                         Shape::indented(indent, context.config),
                                                           Density::Tall,
                                                           "{",
                                                           !has_braces,
@@@ -1773,7 -1784,8 +1774,8 @@@ fn rewrite_args(context: &RewriteContex
                  indent: Indent,
                  arg_indent: Indent,
                  span: Span,
-                 variadic: bool)
+                 variadic: bool,
+                 generics_str_contains_newline: bool)
                  -> Option<String> {
      let mut arg_item_strs =
          try_opt!(args.iter()
      }
  
      let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout {
+         IndentStyle::Block if !generics_str_contains_newline && arg_items.len() <= 1 => {
+             (indent.block_indent(context.config), SeparatorTactic::Never, true)
+         }
          IndentStyle::Block => {
              (indent.block_indent(context.config), SeparatorTactic::Vertical, true)
          }
@@@ -2216,7 -2231,7 +2221,7 @@@ fn format_generics(context: &RewriteCon
                     offset: Indent,
                     span: Span)
                     -> Option<String> {
 -    let shape = try_opt!(generics_shape(context.config, offset));
 +    let shape = Shape::indented(offset, context.config);
      let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
  
      if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
  
      Some(result)
  }
 -
 -fn generics_shape(config: &Config, indent: Indent) -> Option<Shape> {
 -    Some(Shape::legacy(try_opt!(config.max_width.checked_sub(indent.width())),
 -                       indent))
 -}
index 349f2a00c8484bf51db4bee4f95aa6e1bab93226,9c1108ff507e2fdf0efb0fda257eafa5be601429..b6f35f65aec82cfcb6d4f6d932f68f2a95a6f8f9
@@@ -1,6 -1,5 +1,6 @@@
  // rustfmt-array_layout: Block
  // rustfmt-fn_call_style: Block
 +// rustfmt-control_style: Rfc
  // Test expressions with block formatting.
  
  fn arrays() {
@@@ -102,7 -101,7 +102,7 @@@ fn arrays() 
              Weighted { weight: 1, item: 1 },
              Weighted { weight: x, item: 2 },
              Weighted { weight: 1, item: 3 },
-         ],
+         ]
      );
  
      let z = [
@@@ -189,27 -188,3 +189,27 @@@ fn macros() 
          Some(p) => baz!(one_item_macro_as_expression_which_is_also_loooooooooooooooong),
      };
  }
 +
 +fn issue_1450() {
 +    if selfstate
 +        .compare_exchandsfasdsdfgsdgsdfgsdfgsdfgsdfgsdfgfsfdsage_weak(
 +            STATE_PARKED,
 +            STATE_UNPARKED,
 +            Release,
 +            Relaxed,
 +            Release,
 +            Relaxed,
 +        )
 +        .is_ok()
 +    {
 +        return;
 +    }
 +}
 +
 +fn foo() {
 +    if real_total <= limit && !pre_line_comments &&
 +        !items.into_iter().any(|item| item.as_ref().is_multiline())
 +    {
 +        DefinitiveListTactic::Horizontal
 +    }
 +}