From 488c0b9546ff32234ae5b4d3463ec579e337020b Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 22 Mar 2017 09:05:50 +1300 Subject: [PATCH] Test and source fallout --- src/bin/rustfmt.rs | 5 +++- src/chains.rs | 5 +++- src/expr.rs | 47 ++++++++++++++++++++++++++++---------- src/imports.rs | 14 ++++++------ src/issues.rs | 5 +++- src/items.rs | 47 ++++++++++++++++++-------------------- src/types.rs | 40 +++++++++++++++++++------------- tests/source/expr.rs | 8 +++---- tests/target/expr-block.rs | 20 ++++------------ tests/target/expr.rs | 8 +++---- tests/target/pattern.rs | 25 ++++++++++++++------ 11 files changed, 130 insertions(+), 94 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 69a18d14cb5..e97d0219aad 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -219,7 +219,10 @@ fn execute(opts: &Options) -> FmtResult { Ok(run(Input::Text(input), &config)) } - Operation::Format { mut files, config_path } => { + Operation::Format { + mut files, + config_path, + } => { let options = try!(CliOptions::from_matches(&matches)); // Add any additional files that were specified via `--file-lines`. diff --git a/src/chains.rs b/src/chains.rs index 3a3ce74bbb6..4eda33d9647 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -130,7 +130,10 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) - .checked_sub(nested_shape.indent.width() + nested_shape.offset)); - let other_child_shape = Shape { width: max_width, ..nested_shape }; + let other_child_shape = Shape { + width: max_width, + ..nested_shape + }; let first_child_shape = if extend { let mut shape = try_opt!(parent_shape.shrink_left(last_line_width(&parent_rewrite))); match context.config.chain_indent { diff --git a/src/expr.rs b/src/expr.rs index aa4988740dc..1df0f2a96d0 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -19,8 +19,8 @@ use {Indent, Shape, Spanned}; use codemap::SpanUtils; use rewrite::{Rewrite, RewriteContext}; use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic, - DefinitiveListTactic, definitive_tactic, ListItem, format_item_list, - struct_lit_shape, struct_lit_tactic, shape_for_tactic, struct_lit_formatting}; + DefinitiveListTactic, definitive_tactic, ListItem, format_item_list, struct_lit_shape, + struct_lit_tactic, shape_for_tactic, struct_lit_formatting}; use string::{StringFormat, rewrite_string}; use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width, semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr}; @@ -308,7 +308,11 @@ pub fn rewrite_pair(lhs: &LHS, .visual_indent(prefix.len()); let rhs_result = try_opt!(rhs.rewrite(context, rhs_shape)); - let lhs_result = try_opt!(lhs.rewrite(context, Shape { width: lhs_budget, ..shape })); + let lhs_result = try_opt!(lhs.rewrite(context, + Shape { + width: lhs_budget, + ..shape + })); Some(format!("{}{}{}\n{}{}{}", prefix, lhs_result, @@ -658,9 +662,7 @@ impl Rewrite for ast::Block { impl Rewrite for ast::Stmt { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { let result = match self.node { - ast::StmtKind::Local(ref local) => { - local.rewrite(context, shape) - } + 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(self) { ";" } else { "" }; @@ -893,7 +895,10 @@ impl<'a> Rewrite for ControlFlow<'a> { block_width }; - let block_shape = Shape { width: block_width, ..shape }; + let block_shape = Shape { + width: block_width, + ..shape + }; let block_str = try_opt!(self.block.rewrite(context, block_shape)); let cond_span = if let Some(cond) = self.cond { @@ -975,7 +980,10 @@ impl<'a> Rewrite for ControlFlow<'a> { last_in_chain = true; // When rewriting a block, the width is only used for single line // blocks, passing 1 lets us avoid that. - let else_shape = Shape { width: min(1, shape.width), ..shape }; + let else_shape = Shape { + width: min(1, shape.width), + ..shape + }; else_block.rewrite(context, else_shape) } }; @@ -1175,7 +1183,11 @@ fn rewrite_match(context: &RewriteContext, } fn arm_start_pos(arm: &ast::Arm) -> BytePos { - let &ast::Arm { ref attrs, ref pats, .. } = arm; + let &ast::Arm { + ref attrs, + ref pats, + .. + } = arm; if !attrs.is_empty() { return attrs[0].span.lo; } @@ -1205,7 +1217,12 @@ fn arm_comma(config: &Config, body: &ast::Expr) -> &'static str { impl Rewrite for ast::Arm { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { debug!("Arm::rewrite {:?} {:?}", self, shape); - let &ast::Arm { ref attrs, ref pats, ref guard, ref body } = self; + let &ast::Arm { + ref attrs, + ref pats, + ref guard, + ref body, + } = self; // FIXME this is all a bit grotty, would be nice to abstract out the // treatment of attributes. @@ -1250,7 +1267,10 @@ 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 { + width: context.config.max_width - shape.indent.width(), + ..shape + } } else { shape }; @@ -1598,7 +1618,10 @@ fn rewrite_call_inner(context: &RewriteContext, |item| item.span.hi, |item| { item.rewrite(context, - Shape { width: remaining_width, ..nested_shape }) + Shape { + width: remaining_width, + ..nested_shape + }) }, span.lo, span.hi); diff --git a/src/imports.rs b/src/imports.rs index cafba0bb2c5..4b8f4e9762a 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -193,13 +193,13 @@ impl<'a> FmtVisitor<'a> { // before the current `self.last_pos` let pos_before_first_use_item = use_items.first() .map(|p_i| { - cmp::max(self.last_pos, - p_i.attrs - .iter() - .map(|attr| attr.span.lo) - .min() - .unwrap_or(p_i.span.lo)) - }) + cmp::max(self.last_pos, + p_i.attrs + .iter() + .map(|attr| attr.span.lo) + .min() + .unwrap_or(p_i.span.lo)) + }) .unwrap_or(self.last_pos); // Construct a list of pairs, each containing a `use` item and the start of span before // that `use` item. diff --git a/src/issues.rs b/src/issues.rs index e247aad67a0..4819816c4eb 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -98,7 +98,10 @@ impl BadIssueSeeker { // unnumbered TO-DO or FIX-ME. pub fn inspect(&mut self, c: char) -> Option { match self.state { - Seeking::Issue { todo_idx, fixme_idx } => { + Seeking::Issue { + todo_idx, + fixme_idx, + } => { self.state = self.inspect_issue(c, todo_idx, fixme_idx); } Seeking::Number { issue, part } => { diff --git a/src/items.rs b/src/items.rs index 06c5552121f..f5118625822 100644 --- a/src/items.rs +++ b/src/items.rs @@ -73,10 +73,7 @@ impl Rewrite for ast::Local { //let budget = try_opt!(shape.width.checked_sub(shape.indent.block_only().width() + 1)); let nested_shape = try_opt!(shape.sub_width(1)); - result = try_opt!(rewrite_assign_rhs(&context, - result, - ex, - nested_shape)); + result = try_opt!(rewrite_assign_rhs(&context, result, ex, nested_shape)); } result.push(';'); @@ -932,13 +929,13 @@ fn format_struct_struct(context: &RewriteContext, fields.iter(), "}", |field| { - // Include attributes and doc comments, if present - if !field.attrs.is_empty() { - field.attrs[0].span.lo - } else { - field.span.lo - } - }, + // Include attributes and doc comments, if present + if !field.attrs.is_empty() { + field.attrs[0].span.lo + } else { + field.span.lo + } + }, |field| field.ty.span.hi, |field| field.rewrite(context, Shape::legacy(item_budget, item_indent)), context.codemap.span_after(span, "{"), @@ -1035,13 +1032,13 @@ fn format_tuple_struct(context: &RewriteContext, fields.iter(), ")", |field| { - // Include attributes and doc comments, if present - if !field.attrs.is_empty() { - field.attrs[0].span.lo - } else { - field.span.lo - } - }, + // Include attributes and doc comments, if present + if !field.attrs.is_empty() { + field.attrs[0].span.lo + } else { + field.span.lo + } + }, |field| field.ty.span.hi, |field| field.rewrite(context, Shape::legacy(item_budget, item_indent)), context.codemap.span_after(span, "("), @@ -1920,13 +1917,13 @@ fn rewrite_generics(context: &RewriteContext, // Extract comments between generics. let lt_spans = lifetimes.iter().map(|l| { - let hi = if l.bounds.is_empty() { - l.lifetime.span.hi - } else { - l.bounds[l.bounds.len() - 1].span.hi - }; - mk_sp(l.lifetime.span.lo, hi) - }); + let hi = if l.bounds.is_empty() { + l.lifetime.span.hi + } else { + l.bounds[l.bounds.len() - 1].span.hi + }; + mk_sp(l.lifetime.span.lo, hi) + }); let ty_spans = tys.iter().map(span_for_ty_param); let items = itemize_list(context.codemap, diff --git a/src/types.rs b/src/types.rs index 0162ac851f3..ba6852dbe84 100644 --- a/src/types.rs +++ b/src/types.rs @@ -357,21 +357,21 @@ impl Rewrite for ast::WherePredicate { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { // TODO: dead spans? let result = match *self { - ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { ref bound_lifetimes, - ref bounded_ty, - ref bounds, - .. }) => { + ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { + ref bound_lifetimes, + ref bounded_ty, + ref bounds, + .. + }) => { let type_str = try_opt!(bounded_ty.rewrite(context, shape)); let colon = type_bound_colon(context); if !bound_lifetimes.is_empty() { let lifetime_str: String = try_opt!(bound_lifetimes.iter() - .map(|lt| { - lt.rewrite(context, shape) - }) - .intersperse(Some(", ".to_string())) - .collect()); + .map(|lt| lt.rewrite(context, shape)) + .intersperse(Some(", ".to_string())) + .collect()); // 6 = "for<> ".len() let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6; @@ -386,7 +386,11 @@ impl Rewrite for ast::WherePredicate { .collect()); if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 { - format!("for< {} > {}{}{}", lifetime_str, type_str, colon, bounds_str) + format!("for< {} > {}{}{}", + lifetime_str, + type_str, + colon, + bounds_str) } else { format!("for<{}> {}{}{}", lifetime_str, type_str, colon, bounds_str) } @@ -405,14 +409,18 @@ impl Rewrite for ast::WherePredicate { format!("{}{}{}", type_str, colon, bounds_str) } } - ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { ref lifetime, - ref bounds, - .. }) => { + ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { + ref lifetime, + ref bounds, + .. + }) => { try_opt!(rewrite_bounded_lifetime(lifetime, bounds.iter(), context, shape)) } - ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { ref lhs_ty, - ref rhs_ty, - .. }) => { + ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { + ref lhs_ty, + ref rhs_ty, + .. + }) => { let lhs_ty_str = try_opt!(lhs_ty.rewrite(context, shape)); // 3 = " = ".len() let used_width = 3 + lhs_ty_str.len(); diff --git a/tests/source/expr.rs b/tests/source/expr.rs index c0d1e3477e0..11d3fa98f9d 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -174,19 +174,19 @@ fn arrays() { let xy = [ strukt { test123: value_one_two_three_four, turbo: coolio(), } , /* comment */ 1 ]; let a =WeightedChoice::new(&mut [Weighted { - weight: x, + weightweight: x, item: 0, }, Weighted { - weight: 1, + weightweight: 1, item: 1, }, Weighted { - weight: x, + weightweight: x, item: 2, }, Weighted { - weight: 1, + weightweight: 1, item: 3, }]); diff --git a/tests/target/expr-block.rs b/tests/target/expr-block.rs index 0daa8ff91d6..6f0bbeb3212 100644 --- a/tests/target/expr-block.rs +++ b/tests/target/expr-block.rs @@ -95,22 +95,10 @@ fn arrays() { ]; let a = WeightedChoice::new(&mut [ - Weighted { - weight: x, - item: 0, - }, - Weighted { - weight: 1, - item: 1, - }, - Weighted { - weight: x, - item: 2, - }, - Weighted { - weight: 1, - item: 3, - }, + Weighted { weight: x, item: 0 }, + Weighted { weight: 1, item: 1 }, + Weighted { weight: x, item: 2 }, + Weighted { weight: 1, item: 3 }, ]); let z = diff --git a/tests/target/expr.rs b/tests/target/expr.rs index d05870086e3..f0788628538 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -174,19 +174,19 @@ fn arrays() { 1]; let a = WeightedChoice::new(&mut [Weighted { - weight: x, + weightweight: x, item: 0, }, Weighted { - weight: 1, + weightweight: 1, item: 1, }, Weighted { - weight: x, + weightweight: x, item: 2, }, Weighted { - weight: 1, + weightweight: 1, item: 3, }]); diff --git a/tests/target/pattern.rs b/tests/target/pattern.rs index 36d24170cee..f0cc1c16e14 100644 --- a/tests/target/pattern.rs +++ b/tests/target/pattern.rs @@ -24,13 +24,24 @@ fn main() { let foo {} = 42; let foo { .. } = 42; let foo { x, y: ref foo, .. } = 42; - let foo { x, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo, .. } = 42; - let foo { x, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo } = 42; - let foo { x, - yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo, - .. }; - let foo { x, - yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo }; + let foo { + x, + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo, + .. + } = 42; + let foo { + x, + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo, + } = 42; + let foo { + x, + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo, + .. + }; + let foo { + x, + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: ref foo, + }; } impl<'a, 'b> ResolveGeneratedContentFragmentMutator<'a, 'b> {