From 18ccd0190ee6ef4a6e1bb9d737e515b872009711 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 13 Jun 2017 14:49:47 +1200 Subject: [PATCH] Rebasing --- src/chains.rs | 3 +- src/expr.rs | 144 ++++---- src/items.rs | 321 +++++++++++------- src/lib.rs | 6 +- src/lists.rs | 26 +- src/patterns.rs | 3 +- src/string.rs | 10 +- src/types.rs | 76 +++-- tests/target/big-impl.rs | 76 +++-- tests/target/configs-generics_indent-block.rs | 3 +- tests/target/fn-simple.rs | 22 +- tests/target/fn.rs | 7 +- tests/target/hard-tabs.rs | 16 +- tests/target/issue-510.rs | 35 +- tests/target/long-match-arms-brace-newline.rs | 8 +- tests/target/match.rs | 3 +- tests/target/multiple.rs | 12 +- tests/target/paths.rs | 24 +- tests/target/string-lit.rs | 7 +- tests/target/trailing_commas.rs | 50 ++- tests/target/tuple.rs | 4 +- tests/target/type_alias.rs | 14 +- 22 files changed, 503 insertions(+), 367 deletions(-) diff --git a/src/chains.rs b/src/chains.rs index eba79505e20..890c39ebabb 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -574,7 +574,8 @@ fn rewrite_method_call( let (lo, type_str) = if types.is_empty() { (args[0].span.hi, String::new()) } else { - let type_list: Vec<_> = try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect()); + let type_list: Vec<_> = + try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect()); let type_str = if context.config.spaces_within_angle_brackets() && type_list.len() > 0 { format!("::< {} >", type_list.join(", ")) diff --git a/src/expr.rs b/src/expr.rs index 24432fb1dff..80c89a3a91b 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -281,7 +281,9 @@ fn format_expr( Some(format!( "{}{}", "do catch ", - try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent))) + try_opt!( + block.rewrite(&context, Shape::legacy(budget, shape.indent)) + ) )) } }; @@ -1428,9 +1430,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { if contains_skip(attrs) { return None; } - format!("{}\n{}", - try_opt!(attrs.rewrite(context, shape)), - shape.indent.to_string(context.config)) + format!( + "{}\n{}", + try_opt!(attrs.rewrite(context, shape)), + shape.indent.to_string(context.config) + ) } else { String::new() }; @@ -1515,11 +1519,11 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { }; match rewrite { - Some(ref body_str) if (!body_str.contains('\n') && - body_str.len() <= arm_shape.width) || - !context.config.wrap_match_arms() || - (extend && first_line_width(body_str) <= arm_shape.width) || - is_block => { + Some(ref body_str) + if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) || + !context.config.wrap_match_arms() || + (extend && first_line_width(body_str) <= arm_shape.width) || + is_block => { let block_sep = match context.config.control_brace_style() { ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(), _ => " ", @@ -1610,28 +1614,33 @@ fn rewrite_guard( if let Some(ref guard) = *guard { // First try to fit the guard string on the same line as the pattern. // 4 = ` if `, 5 = ` => {` - if let Some(cond_shape) = shape - .shrink_left(pattern_width + 4) - .and_then(|s| s.sub_width(5)) { - if let Some(cond_str) = guard - .rewrite(context, cond_shape) - .and_then(|s| s.rewrite(context, cond_shape)) { + if let Some(cond_shape) = shape.shrink_left(pattern_width + 4).and_then( + |s| s.sub_width(5), + ) + { + if let Some(cond_str) = guard.rewrite(context, cond_shape).and_then(|s| { + s.rewrite(context, cond_shape) + }) + { return Some(format!(" if {}", cond_str)); } } // Not enough space to put the guard after the pattern, try a newline. // 3 == `if ` - if let Some(cond_shape) = Shape::indented(shape.indent.block_indent(context.config) + 3, - context.config) - .sub_width(3) { + if let Some(cond_shape) = Shape::indented( + shape.indent.block_indent(context.config) + 3, + context.config, + ).sub_width(3) + { if let Some(cond_str) = guard.rewrite(context, cond_shape) { - return Some(format!("\n{}if {}", - shape - .indent - .block_indent(context.config) - .to_string(context.config), - cond_str)); + return Some(format!( + "\n{}if {}", + shape.indent.block_indent(context.config).to_string( + context.config, + ), + cond_str + )); } } @@ -1826,26 +1835,28 @@ fn rewrite_call_inner( let span_lo = context.codemap.span_after(span, "("); let args_span = mk_sp(span_lo, span.hi); - let (extendable, list_str) = rewrite_call_args(context, - args, - args_span, - nested_shape, - one_line_width, - force_trailing_comma) - .or_else(|| if context.use_block_indent() { - rewrite_call_args(context, - args, - args_span, - Shape::indented(shape - .block() - .indent - .block_indent(context.config), - context.config), - 0, - force_trailing_comma) - } else { - None - }) + let (extendable, list_str) = rewrite_call_args( + context, + args, + args_span, + nested_shape, + one_line_width, + force_trailing_comma, + ).or_else(|| if context.use_block_indent() { + rewrite_call_args( + context, + args, + args_span, + Shape::indented( + shape.block().indent.block_indent(context.config), + context.config, + ), + 0, + force_trailing_comma, + ) + } else { + None + }) .ok_or(Ordering::Less)?; if !context.use_block_indent() && need_block_indent(&list_str, nested_shape) && !extendable { @@ -1861,12 +1872,20 @@ fn rewrite_call_inner( ); } - let args_shape = shape - .sub_width(last_line_width(&callee_str)) - .ok_or(Ordering::Less)?; - Ok(format!("{}{}", - callee_str, - wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape))) + let args_shape = shape.sub_width(last_line_width(&callee_str)).ok_or( + Ordering::Less, + )?; + Ok(format!( + "{}{}", + callee_str, + wrap_args_with_parens( + context, + &list_str, + extendable, + args_shape, + nested_shape, + ) + )) } fn need_block_indent(s: &str, shape: Shape) -> bool { @@ -2056,15 +2075,17 @@ fn paren_overhead(context: &RewriteContext) -> usize { } } -fn wrap_args_with_parens(context: &RewriteContext, - args_str: &str, - is_extendable: bool, - shape: Shape, - nested_shape: Shape) - -> String { +fn wrap_args_with_parens( + context: &RewriteContext, + args_str: &str, + is_extendable: bool, + shape: Shape, + nested_shape: Shape, +) -> String { if !context.use_block_indent() || - (context.inside_macro && !args_str.contains('\n') && - args_str.len() + paren_overhead(context) <= shape.width) || is_extendable { + (context.inside_macro && !args_str.contains('\n') && + args_str.len() + paren_overhead(context) <= shape.width) || is_extendable + { if context.config.spaces_within_parens() && args_str.len() > 0 { format!("( {} )", args_str) } else { @@ -2166,10 +2187,9 @@ enum StructLitField<'a> { return Some(format!("{} {{}}", path_str)); } - let field_iter = fields - .into_iter() - .map(StructLitField::Regular) - .chain(base.into_iter().map(StructLitField::Base)); + let field_iter = fields.into_iter().map(StructLitField::Regular).chain( + base.into_iter().map(StructLitField::Base), + ); // Foo { a: Foo } - indent is +3, width is -5. let (h_shape, v_shape) = try_opt!(struct_lit_shape(shape, context, path_str.len() + 3, 2)); diff --git a/src/items.rs b/src/items.rs index 01aa7b389d5..156528301be 100644 --- a/src/items.rs +++ b/src/items.rs @@ -550,11 +550,12 @@ fn format_variant(&self, field: &ast::Variant) -> Option { } } -pub fn format_impl(context: &RewriteContext, - item: &ast::Item, - offset: Indent, - where_span_end: Option) - -> Option { +pub fn format_impl( + context: &RewriteContext, + item: &ast::Item, + offset: Indent, + where_span_end: Option, +) -> Option { if let ast::ItemKind::Impl(_, _, _, ref generics, _, _, ref items) = item.node { let mut result = String::new(); let ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset)); @@ -569,18 +570,26 @@ pub fn format_impl(context: &RewriteContext, .checked_sub(last_line_width(&result)) .unwrap_or(0) }; - let where_clause_str = try_opt!(rewrite_where_clause(context, - &generics.where_clause, - context.config.item_brace_style(), - Shape::legacy(where_budget, - offset.block_only()), - context.config.where_density(), - "{", - false, - last_line_width(&ref_and_type) == 1, - where_span_end)); + let where_clause_str = try_opt!(rewrite_where_clause( + context, + &generics.where_clause, + context.config.item_brace_style(), + Shape::legacy(where_budget, offset.block_only()), + context.config.where_density(), + "{", + false, + last_line_width(&ref_and_type) == 1, + where_span_end, + )); - if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) { + if try_opt!(is_impl_single_line( + context, + &items, + &result, + &where_clause_str, + &item, + )) + { result.push_str(&where_clause_str); if where_clause_str.contains('\n') { let white_space = offset.to_string(context.config); @@ -670,10 +679,11 @@ fn is_impl_single_line( ) } -fn format_impl_ref_and_type(context: &RewriteContext, - item: &ast::Item, - offset: Indent) - -> Option { +fn format_impl_ref_and_type( + context: &RewriteContext, + item: &ast::Item, + offset: Indent, +) -> Option { if let ast::ItemKind::Impl(unsafety, polarity, _, @@ -693,12 +703,18 @@ fn format_impl_ref_and_type(context: &RewriteContext, Some(ref tr) => tr.path.span.lo, None => self_ty.span.lo, }; - let shape = generics_shape_from_config(context.config, - Shape::indented(offset + last_line_width(&result), - context.config), - 0); - let generics_str = - try_opt!(rewrite_generics_inner(context, generics, shape, shape.width, mk_sp(lo, hi))); + let shape = generics_shape_from_config( + context.config, + Shape::indented(offset + last_line_width(&result), context.config), + 0, + ); + let generics_str = try_opt!(rewrite_generics_inner( + context, + generics, + shape, + shape.width, + mk_sp(lo, hi), + )); let polarity_str = if polarity == ast::ImplPolarity::Negative { "!" @@ -709,24 +725,34 @@ fn format_impl_ref_and_type(context: &RewriteContext, if let Some(ref trait_ref) = *trait_ref { let result_len = result.len(); if let Some(trait_ref_str) = - rewrite_trait_ref(context, - &trait_ref, - offset, - &generics_str, - true, - polarity_str, - result_len) { + rewrite_trait_ref( + context, + &trait_ref, + offset, + &generics_str, + true, + polarity_str, + result_len, + ) + { result.push_str(&trait_ref_str); } else { - let generics_str = - try_opt!(rewrite_generics_inner(context, generics, shape, 0, mk_sp(lo, hi))); - result.push_str(&try_opt!(rewrite_trait_ref(context, - &trait_ref, - offset, - &generics_str, - false, - polarity_str, - result_len))); + let generics_str = try_opt!(rewrite_generics_inner( + context, + generics, + shape, + 0, + mk_sp(lo, hi), + )); + result.push_str(&try_opt!(rewrite_trait_ref( + context, + &trait_ref, + offset, + &generics_str, + false, + polarity_str, + result_len, + ))); } } else { result.push_str(&generics_str); @@ -777,32 +803,41 @@ fn format_impl_ref_and_type(context: &RewriteContext, Style::Legacy => new_line_offset + trait_ref_overhead, Style::Rfc => new_line_offset, }; - result.push_str(&*try_opt!(self_ty.rewrite(context, Shape::legacy(budget, type_offset)))); + result.push_str(&*try_opt!(self_ty.rewrite( + context, + Shape::legacy(budget, type_offset), + ))); Some(result) } else { unreachable!(); } } -fn rewrite_trait_ref(context: &RewriteContext, - trait_ref: &ast::TraitRef, - offset: Indent, - generics_str: &str, - retry: bool, - polarity_str: &str, - result_len: usize) - -> Option { +fn rewrite_trait_ref( + context: &RewriteContext, + trait_ref: &ast::TraitRef, + offset: Indent, + generics_str: &str, + retry: bool, + polarity_str: &str, + result_len: usize, +) -> Option { // 1 = space between generics and trait_ref let used_space = 1 + polarity_str.len() + - if generics_str.contains('\n') { - last_line_width(&generics_str) - } else { - result_len + generics_str.len() - }; + if generics_str.contains('\n') { + last_line_width(&generics_str) + } else { + result_len + generics_str.len() + }; let shape = Shape::indented(offset + used_space, context.config); if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) { if !(retry && trait_ref_str.contains('\n')) { - return Some(format!("{} {}{}", generics_str, polarity_str, &trait_ref_str)); + return Some(format!( + "{} {}{}", + generics_str, + polarity_str, + &trait_ref_str + )); } } // We could not make enough space for trait_ref, so put it on new line. @@ -810,26 +845,29 @@ fn rewrite_trait_ref(context: &RewriteContext, let offset = offset.block_indent(context.config); let shape = Shape::indented(offset, context.config); let trait_ref_str = try_opt!(trait_ref.rewrite(context, shape)); - Some(format!("{}\n{}{}{}", - generics_str, - &offset.to_string(context.config), - polarity_str, - &trait_ref_str)) + Some(format!( + "{}\n{}{}{}", + generics_str, + &offset.to_string(context.config), + polarity_str, + &trait_ref_str + )) } else { None } } -pub fn format_struct(context: &RewriteContext, - item_name: &str, - ident: ast::Ident, - vis: &ast::Visibility, - struct_def: &ast::VariantData, - generics: Option<&ast::Generics>, - span: Span, - offset: Indent, - one_line_width: Option) - -> Option { +pub fn format_struct( + context: &RewriteContext, + item_name: &str, + ident: ast::Ident, + vis: &ast::Visibility, + struct_def: &ast::VariantData, + generics: Option<&ast::Generics>, + span: Span, + offset: Indent, + one_line_width: Option, +) -> Option { match *struct_def { ast::VariantData::Unit(..) => Some(format_unit_struct(item_name, ident, vis)), ast::VariantData::Tuple(ref fields, _) => { @@ -877,8 +915,12 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) let body_lo = context.codemap.span_after(item.span, "{"); let shape = Shape::indented(offset + last_line_width(&result), context.config); - let generics_str = - try_opt!(rewrite_generics(context, generics, shape, mk_sp(item.span.lo, body_lo))); + let generics_str = try_opt!(rewrite_generics( + context, + generics, + shape, + mk_sp(item.span.lo, body_lo), + )); result.push_str(&generics_str); let trait_bound_str = try_opt!(rewrite_trait_bounds( @@ -1534,10 +1576,12 @@ pub fn rewrite_associated_type( let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt { let shape = Shape::legacy(context.config.max_width(), indent); let bounds: &[_] = ty_param_bounds; - let bound_str = try_opt!(bounds - .iter() - .map(|ty_bound| ty_bound.rewrite(context, shape)) - .collect::>>()); + let bound_str = try_opt!( + bounds + .iter() + .map(|ty_bound| ty_bound.rewrite(context, shape)) + .collect::>>() + ); if bounds.len() > 0 { format!(": {}", join_bounds(context, shape, &bound_str)) } else { @@ -2269,7 +2313,8 @@ fn compute_budgets_for_args( // 4 = "() {".len() let multi_line_overhead = indent.width() + result.len() + if newline_brace { 2 } else { 4 }; - let multi_line_budget = try_opt!(context.config.max_width().checked_sub(multi_line_overhead)); + let multi_line_budget = + try_opt!(context.config.max_width().checked_sub(multi_line_overhead)); return Some(( one_line_budget, @@ -2295,22 +2340,24 @@ fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> bool { } } -fn rewrite_generics(context: &RewriteContext, - generics: &ast::Generics, - shape: Shape, - span: Span) - -> Option { +fn rewrite_generics( + context: &RewriteContext, + generics: &ast::Generics, + shape: Shape, + span: Span, +) -> Option { let shape = generics_shape_from_config(context.config, shape, 0); rewrite_generics_inner(context, generics, shape, shape.width, span) .or_else(|| rewrite_generics_inner(context, generics, shape, 0, span)) } -fn rewrite_generics_inner(context: &RewriteContext, - generics: &ast::Generics, - shape: Shape, - one_line_width: usize, - span: Span) - -> Option { +fn rewrite_generics_inner( + context: &RewriteContext, + generics: &ast::Generics, + shape: Shape, + one_line_width: usize, + span: Span, +) -> Option { // FIXME: convert bounds to where clauses where they get too big or if // there is a where clause at all. let lifetimes: &[_] = &generics.lifetimes; @@ -2334,15 +2381,17 @@ fn rewrite_generics_inner(context: &RewriteContext, }); let ty_spans = tys.iter().map(span_for_ty_param); - let items = itemize_list(context.codemap, - lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)), - ">", - |&(sp, _)| sp.lo, - |&(sp, _)| sp.hi, - // FIXME: don't clone - |&(_, ref str)| str.clone(), - context.codemap.span_after(span, "<"), - span.hi); + let items = itemize_list( + context.codemap, + lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)), + ">", + |&(sp, _)| sp.lo, + |&(sp, _)| sp.hi, + // FIXME: don't clone + |&(_, ref str)| str.clone(), + context.codemap.span_after(span, "<"), + span.hi, + ); format_generics_item_list(context, items, shape, one_line_width) } @@ -2357,12 +2406,14 @@ pub fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) } } -pub fn format_generics_item_list(context: &RewriteContext, - items: I, - shape: Shape, - one_line_budget: usize) - -> Option - where I: Iterator +pub fn format_generics_item_list( + context: &RewriteContext, + items: I, + shape: Shape, + one_line_budget: usize, +) -> Option +where + I: Iterator, { let item_vec = items.collect::>(); @@ -2381,21 +2432,29 @@ pub fn format_generics_item_list(context: &RewriteContext, let list_str = try_opt!(write_list(&item_vec, &fmt)); - Some(wrap_generics_with_angle_brackets(context, &list_str, shape.indent)) + Some(wrap_generics_with_angle_brackets( + context, + &list_str, + shape.indent, + )) } -pub fn wrap_generics_with_angle_brackets(context: &RewriteContext, - list_str: &str, - list_offset: Indent) - -> String { +pub fn wrap_generics_with_angle_brackets( + context: &RewriteContext, + list_str: &str, + list_offset: Indent, +) -> String { if context.config.generics_indent() == IndentStyle::Block && - (list_str.contains('\n') || list_str.ends_with(',')) { - format!("<\n{}{}\n{}>", - list_offset.to_string(context.config), - list_str, - list_offset - .block_unindent(context.config) - .to_string(context.config)) + (list_str.contains('\n') || list_str.ends_with(',')) + { + format!( + "<\n{}{}\n{}>", + list_offset.to_string(context.config), + list_str, + list_offset.block_unindent(context.config).to_string( + context.config, + ) + ) } else if context.config.spaces_within_angle_brackets() { format!("< {} >", list_str) } else { @@ -2413,10 +2472,12 @@ fn rewrite_trait_bounds( if bounds.is_empty() { return Some(String::new()); } - let bound_str = try_opt!(bounds - .iter() - .map(|ty_bound| ty_bound.rewrite(&context, shape)) - .collect::>>()); + let bound_str = try_opt!( + bounds + .iter() + .map(|ty_bound| ty_bound.rewrite(&context, shape)) + .collect::>>() + ); Some(format!(": {}", join_bounds(context, shape, &bound_str))) } @@ -2446,14 +2507,16 @@ fn rewrite_where_clause_rfc_style( let len = where_clause.predicates.len(); let end_of_preds = span_for_where_pred(&where_clause.predicates[len - 1]).hi; let span_end = span_end.unwrap_or(end_of_preds); - let items = itemize_list(context.codemap, - where_clause.predicates.iter(), - terminator, - |pred| span_for_where_pred(pred).lo, - |pred| span_for_where_pred(pred).hi, - |pred| pred.rewrite(context, shape), - span_start, - span_end); + let items = itemize_list( + context.codemap, + where_clause.predicates.iter(), + terminator, + |pred| span_for_where_pred(pred).lo, + |pred| span_for_where_pred(pred).hi, + |pred| pred.rewrite(context, shape), + span_start, + span_end, + ); let comma_tactic = if suppress_comma { SeparatorTactic::Never } else { diff --git a/src/lib.rs b/src/lib.rs index 2ede6d9a2f0..2aa477ccafa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -468,8 +468,10 @@ fn format_ast( } // Reset the error count. if parse_session.span_diagnostic.has_errors() { - let silent_emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), - Some(codemap.clone()))); + let silent_emitter = Box::new(EmitterWriter::new( + Box::new(Vec::new()), + Some(codemap.clone()), + )); parse_session.span_diagnostic = Handler::with_emitter(true, false, silent_emitter); } } diff --git a/src/lists.rs b/src/lists.rs index 37acff692a4..24bd10e24a0 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -555,20 +555,20 @@ pub fn struct_lit_shape( prefix_width: usize, suffix_width: usize, ) -> Option<(Option, Shape)> { - let v_shape = - match context.config.struct_lit_style() { - IndentStyle::Visual => { - try_opt!(try_opt!(shape.visual_indent(0).shrink_left(prefix_width)) - .sub_width(suffix_width)) + let v_shape = match context.config.struct_lit_style() { + IndentStyle::Visual => { + try_opt!( + try_opt!(shape.visual_indent(0).shrink_left(prefix_width)).sub_width(suffix_width) + ) + } + IndentStyle::Block => { + let shape = shape.block_indent(context.config.tab_spaces()); + Shape { + width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())), + ..shape } - IndentStyle::Block => { - let shape = shape.block_indent(context.config.tab_spaces()); - Shape { - width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())), - ..shape - } - } - }; + } + }; let h_shape = shape.sub_width(prefix_width + suffix_width); Some((h_shape, v_shape)) } diff --git a/src/patterns.rs b/src/patterns.rs index a52f42157b8..25290ceaf0e 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -81,7 +81,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape) } PatKind::TupleStruct(ref path, ref pat_vec, dotdot_pos) => { - let path_str = try_opt!(rewrite_path(context, PathContext::Expr, None, path, shape)); + let path_str = + try_opt!(rewrite_path(context, PathContext::Expr, None, path, shape)); rewrite_tuple_pat( pat_vec, dotdot_pos, diff --git a/src/string.rs b/src/string.rs index 904260e8182..6c20c18ca47 100644 --- a/src/string.rs +++ b/src/string.rs @@ -42,12 +42,10 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option // `cur_start` is the position in `orig` of the start of the current line. let mut cur_start = 0; - let mut result = String::with_capacity( - stripped_str - .len() - .checked_next_power_of_two() - .unwrap_or(usize::max_value()), - ); + let mut result = + String::with_capacity(stripped_str.len().checked_next_power_of_two().unwrap_or( + usize::max_value(), + )); result.push_str(fmt.opener); let ender_length = fmt.line_end.len(); diff --git a/src/types.rs b/src/types.rs index b1cea7df864..5e57ce790a7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -226,18 +226,22 @@ fn rewrite_segment( let generics_shape = generics_shape_from_config(context.config, shape, separator.len()); - let items = itemize_list(context.codemap, - param_list.into_iter(), - ">", - |param| param.get_span().lo, - |param| param.get_span().hi, - |seg| seg.rewrite(context, generics_shape), - list_lo, - span_hi); - let generics_str = try_opt!(format_generics_item_list(context, - items, - generics_shape, - generics_shape.width)); + let items = itemize_list( + context.codemap, + param_list.into_iter(), + ">", + |param| param.get_span().lo, + |param| param.get_span().hi, + |seg| seg.rewrite(context, generics_shape), + list_lo, + span_hi, + ); + let generics_str = try_opt!(format_generics_item_list( + context, + items, + generics_shape, + generics_shape.width, + )); // Update position of last bracket. *span_lo = next_span_lo; @@ -307,9 +311,9 @@ enum ArgumentKind context.codemap, // FIXME Would be nice to avoid this allocation, // but I couldn't get the types to work out. - inputs - .map(|i| ArgumentKind::Regular(Box::new(i))) - .chain(variadic_arg), + inputs.map(|i| ArgumentKind::Regular(Box::new(i))).chain( + variadic_arg, + ), ")", |arg| match *arg { ArgumentKind::Regular(ref ty) => ty.span().lo, @@ -387,11 +391,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { // 6 = "for<> ".len() let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6; let ty_shape = try_opt!(shape.block_left(used_width)); - let bounds: Vec<_> = - try_opt!(bounds - .iter() - .map(|ty_bound| ty_bound.rewrite(context, ty_shape)) - .collect()); + let bounds: Vec<_> = try_opt!( + bounds + .iter() + .map(|ty_bound| ty_bound.rewrite(context, ty_shape)) + .collect() + ); let bounds_str = join_bounds(context, ty_shape, &bounds); if context.config.spaces_within_angle_brackets() && lifetime_str.len() > 0 { @@ -411,11 +416,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { Style::Legacy => try_opt!(shape.block_left(used_width)), Style::Rfc => shape.block_indent(context.config.tab_spaces()), }; - let bounds: Vec<_> = - try_opt!(bounds - .iter() - .map(|ty_bound| ty_bound.rewrite(context, ty_shape)) - .collect()); + let bounds: Vec<_> = try_opt!( + bounds + .iter() + .map(|ty_bound| ty_bound.rewrite(context, ty_shape)) + .collect() + ); let bounds_str = join_bounds(context, ty_shape, &bounds); format!("{}{}{}", type_str, colon, bounds_str) @@ -482,10 +488,12 @@ fn rewrite_bounded_lifetime<'b, I>( ); let colon = type_bound_colon(context); let overhead = last_line_width(&result) + colon.len(); - let result = format!("{}{}{}", - result, - colon, - join_bounds(context, try_opt!(shape.sub_width(overhead)), &appendix)); + let result = format!( + "{}{}{}", + result, + colon, + join_bounds(context, try_opt!(shape.sub_width(overhead)), &appendix) + ); wrap_str(result, context.config.max_width(), shape) } } @@ -540,10 +548,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { result.push_str(&self.ident.to_string()); if !self.bounds.is_empty() { result.push_str(type_bound_colon(context)); - let strs: Vec<_> = try_opt!(self.bounds - .iter() - .map(|ty_bound| ty_bound.rewrite(context, shape)) - .collect()); + let strs: Vec<_> = try_opt!( + self.bounds + .iter() + .map(|ty_bound| ty_bound.rewrite(context, shape)) + .collect() + ); result.push_str(&join_bounds(context, shape, &strs)); } if let Some(ref def) = self.default { diff --git a/tests/target/big-impl.rs b/tests/target/big-impl.rs index afe2571ec68..056ded71daa 100644 --- a/tests/target/big-impl.rs +++ b/tests/target/big-impl.rs @@ -1,24 +1,27 @@ // #1357 impl<'a, Select, From, Distinct, Where, Order, Limit, Offset, Groupby, DB> InternalBoxedDsl<'a, DB> for SelectStatement - where DB: Backend, - Select: QueryFragment + SelectableExpression + 'a, - Distinct: QueryFragment + 'a, - Where: Into + 'a>>>, - Order: QueryFragment + 'a, - Limit: QueryFragment + 'a, - Offset: QueryFragment + 'a +where + DB: Backend, + Select: QueryFragment + SelectableExpression + 'a, + Distinct: QueryFragment + 'a, + Where: Into + 'a>>>, + Order: QueryFragment + 'a, + Limit: QueryFragment + 'a, + Offset: QueryFragment + 'a, { type Output = BoxedSelectStatement<'a, Select::SqlTypeForSelect, From, DB>; fn internal_into_boxed(self) -> Self::Output { - BoxedSelectStatement::new(Box::new(self.select), - self.from, - Box::new(self.distinct), - self.where_clause.into(), - Box::new(self.order), - Box::new(self.limit), - Box::new(self.offset)) + BoxedSelectStatement::new( + Box::new(self.select), + self.from, + Box::new(self.distinct), + self.where_clause.into(), + Box::new(self.order), + Box::new(self.limit), + Box::new(self.offset), + ) } } @@ -31,32 +34,39 @@ impl Foo Foo for Bar { +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo + for Bar { fn foo() {} } impl Foo - for Bar { + for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, + > { fn foo() {} } impl Foo - for Bar { + for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, + > { fn foo() {} } -impl Foo - for Bar { +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo + for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, + > { fn foo() {} } diff --git a/tests/target/configs-generics_indent-block.rs b/tests/target/configs-generics_indent-block.rs index 53175fa362f..0950a716fde 100644 --- a/tests/target/configs-generics_indent-block.rs +++ b/tests/target/configs-generics_indent-block.rs @@ -9,7 +9,8 @@ fn lorem< Adipiscing: Eq = usize, Consectetur: Eq = usize, Elit: Eq = usize, ->(ipsum: Ipsum, +>( + ipsum: Ipsum, dolor: Dolor, sit: Sit, amet: Amet, diff --git a/tests/target/fn-simple.rs b/tests/target/fn-simple.rs index d0c15c643f5..102c1449627 100644 --- a/tests/target/fn-simple.rs +++ b/tests/target/fn-simple.rs @@ -8,8 +8,10 @@ fn simple( fn op( x: Typ, key: &[u8], - upd: Box) - -> (memcache::Status, Result>)>, + upd: Box< + Fn(Option<&memcache::Item>) + -> (memcache::Status, Result>), + >, ) -> MapResult { } @@ -33,14 +35,14 @@ fn weird_comment( fn generic(arg: T) -> &SomeType where T: Fn(// First arg - A, - // Second argument - B, - C, - D, - // pre comment - E /* last comment */) - -> &SomeType, + A, + // Second argument + B, + C, + D, + // pre comment + E /* last comment */) + -> &SomeType, { arg(a, b, c, d, e) } diff --git a/tests/target/fn.rs b/tests/target/fn.rs index 7a09ec6e83c..c8102fe3b54 100644 --- a/tests/target/fn.rs +++ b/tests/target/fn.rs @@ -55,7 +55,7 @@ pub fn render< N: Clone + 'a, E: Clone + 'a, G: Labeller<'a, N, E> + GraphWalk<'a, N, E>, - W: Write + W: Write, >( g: &'a G, w: &mut W, @@ -101,7 +101,10 @@ fn foo(a: i32) -> i32 { fn ______________________baz( a: i32, -) -> *mut ::std::option::Option ()> { +) -> *mut ::std::option::Option< + extern "C" fn(arg1: i32, _____________________a: i32, arg3: i32) + -> (), +> { } pub fn check_path<'a, 'tcx>( diff --git a/tests/target/hard-tabs.rs b/tests/target/hard-tabs.rs index 2781d54af88..42d3875ad19 100644 --- a/tests/target/hard-tabs.rs +++ b/tests/target/hard-tabs.rs @@ -58,14 +58,14 @@ fn foo(a: i32, a: i32, a: i32, a: i32, a: i32, a: i32, a: i32, a: i32, a: i32, a fn generic(arg: T) -> &SomeType where T: Fn(// First arg - A, - // Second argument - B, - C, - D, - // pre comment - E /* last comment */) - -> &SomeType, + A, + // Second argument + B, + C, + D, + // pre comment + E /* last comment */) + -> &SomeType, { arg(a, b, c, d, e) } diff --git a/tests/target/issue-510.rs b/tests/target/issue-510.rs index 27eeacaa08a..54fe361bb4e 100644 --- a/tests/target/issue-510.rs +++ b/tests/target/issue-510.rs @@ -5,24 +5,23 @@ fn solve_inline_size_constraints( input: &ISizeConstraintInput, ) -> ISizeConstraintSolution { - let (inline_start, inline_size, margin_inline_start, margin_inline_end) = - match ( - inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx, - inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx, - ) { - (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Auto) => { - let margin_start = inline_start_margin.specified_or_zero(); - let margin_end = inline_end_margin.specified_or_zero(); - // Now it is the same situation as inline-start Specified and inline-end - // and inline-size Auto. - // - // Set inline-end to zero to calculate inline-size. - let inline_size = block.get_shrink_to_fit_inline_size( - available_inline_size - (margin_start + margin_end), - ); - (Au(0), inline_size, margin_start, margin_end) - } - }; + let (inline_start, inline_size, margin_inline_start, margin_inline_end) = match ( + inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx, + inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx, + ) { + (MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Auto) => { + let margin_start = inline_start_margin.specified_or_zero(); + let margin_end = inline_end_margin.specified_or_zero(); + // Now it is the same situation as inline-start Specified and inline-end + // and inline-size Auto. + // + // Set inline-end to zero to calculate inline-size. + let inline_size = block.get_shrink_to_fit_inline_size( + available_inline_size - (margin_start + margin_end), + ); + (Au(0), inline_size, margin_start, margin_end) + } + }; let (inline_start, inline_size, margin_inline_start, margin_inline_end) = match (inline_start, inline_end, computed_inline_size) { diff --git a/tests/target/long-match-arms-brace-newline.rs b/tests/target/long-match-arms-brace-newline.rs index 95e6d3be3ed..6f4e3a8de21 100644 --- a/tests/target/long-match-arms-brace-newline.rs +++ b/tests/target/long-match-arms-brace-newline.rs @@ -5,12 +5,8 @@ fn main() { match x { - aaaaaaaa::Bbbbb::Ccccccccccccc(_, Some(ref x)) if x == - "aaaaaaaaaaa \ - aaaaaaa aaaaaa" => - { - Ok(()) - } + aaaaaaaa::Bbbbb::Ccccccccccccc(_, Some(ref x)) + if x == "aaaaaaaaaaa aaaaaaa aaaaaa" => Ok(()), _ => Err(x), } } diff --git a/tests/target/match.rs b/tests/target/match.rs index f21fdb37dc5..35b97094535 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -323,7 +323,8 @@ fn guards() { barrrrrrrrrrrr => {} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if fooooooooooooooooooooo && - (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => { + (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => { + {} } } } diff --git a/tests/target/multiple.rs b/tests/target/multiple.rs index 8c1e2d9695d..49d0520a5d1 100644 --- a/tests/target/multiple.rs +++ b/tests/target/multiple.rs @@ -40,7 +40,7 @@ fn foo() -> Box fn baz< 'a: 'b, // comment on 'a - T: SomsssssssssssssssssssssssssssssssssssssssssssssssssssssseType /* comment on T */ + T: SomsssssssssssssssssssssssssssssssssssssssssssssssssssssseType, /* comment on T */ >( a: A, b: B, // comment on b @@ -164,11 +164,11 @@ fn deconstruct() fn deconstruct( foo: Bar, ) -> (SocketAddr, - Method, - Headers, - RequestUri, - HttpVersion, - AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) { + Method, + Headers, + RequestUri, + HttpVersion, + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) { } #[rustfmt_skip] diff --git a/tests/target/paths.rs b/tests/target/paths.rs index da266389b6b..9b48dd9a470 100644 --- a/tests/target/paths.rs +++ b/tests/target/paths.rs @@ -1,18 +1,18 @@ // rustfmt-normalize_comments: true fn main() { - let constellation_chan = Constellation::::start( - compositor_proxy, - resource_task, - image_cache_task, - font_cache_task, - time_profiler_chan, - mem_profiler_chan, - devtools_chan, - storage_task, - supports_clipboard, - ); + let constellation_chan = + Constellation::::start( + compositor_proxy, + resource_task, + image_cache_task, + font_cache_task, + time_profiler_chan, + mem_profiler_chan, + devtools_chan, + storage_task, + supports_clipboard, + ); Quux::::some_func(); diff --git a/tests/target/string-lit.rs b/tests/target/string-lit.rs index 7ac75290f2e..fc92d379a52 100644 --- a/tests/target/string-lit.rs +++ b/tests/target/string-lit.rs @@ -25,10 +25,9 @@ fn main() -> &'static str { filename.replace(" ", "\\"); - let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = - funktion( - "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", - ); + let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = funktion( + "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", + ); let unicode = "a̐éö̲\r\n"; let unicode2 = "Löwe 老虎 Léopard"; diff --git a/tests/target/trailing_commas.rs b/tests/target/trailing_commas.rs index 9ef4eecbcc5..c5e6b9525b4 100644 --- a/tests/target/trailing_commas.rs +++ b/tests/target/trailing_commas.rs @@ -11,7 +11,12 @@ fn main() { } } -fn f(x: T, y: S) -> T +fn f< + S, T, +>( + x: T, + y: S, +) -> T where T: P, S: Q, @@ -31,8 +36,9 @@ fn f(x: T) -> T } } -struct Pair -where +struct Pair< + S, T, +> where T: P, S: P + Q, { @@ -40,36 +46,56 @@ struct Pair b: S, } -struct TupPair(S, T) +struct TupPair< + S, T, +>(S, T) where T: P, S: P + Q; -enum E -where +enum E< + S, T, +> where S: P, T: P, { A { a: T, }, } -type Double where +type Double< + T, +> where T: P, - T: Q = Pair; + T: Q = Pair< + T, T, +>; extern "C" { - fn f(x: T, y: S) -> T + fn f< + S, T, + >( + x: T, + y: S, + ) -> T where T: P, S: Q; } -trait Q -where +trait Q< + S, T, +> where T: P, S: R, { - fn f(self, x: T, y: S, z: U) -> Self + fn f< + U, V, + >( + self, + x: T, + y: S, + z: U, + ) -> Self where U: P, V: P; diff --git a/tests/target/tuple.rs b/tests/target/tuple.rs index f398b7dffb7..772cb251f57 100644 --- a/tests/target/tuple.rs +++ b/tests/target/tuple.rs @@ -65,7 +65,9 @@ fn issue775() { ( "b".to_string(), Array(vec![ - mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), + mk_object( + &[("c".to_string(), String("\x0c\r".to_string()))] + ), mk_object(&[("d".to_string(), String("".to_string()))]), ]), ), diff --git a/tests/target/type_alias.rs b/tests/target/type_alias.rs index 86180149aac..cd40f65d6e1 100644 --- a/tests/target/type_alias.rs +++ b/tests/target/type_alias.rs @@ -3,9 +3,11 @@ type PrivateTest<'a, I> = (Box + 'a>, Box + 'a>); -pub type PublicTest<'a, I, O> = Result, - Box + 'a>, - Box + 'a>>; +pub type PublicTest<'a, I, O> = Result< + Vec, + Box + 'a>, + Box + 'a>, +>; pub type LongGenericListTest< 'a, @@ -17,7 +19,7 @@ LONGPARAMETERNAME, A, B, - C + C, > = Option>; pub type Exactly100CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec; @@ -37,13 +39,13 @@ LONGPARAMETERNAME, A1, B, - C + C, > = Vec; pub type CommentTest< // Lifetime 'a, // Type - T + T, > = ();