From fbd4b87329324e3d53064601cccf7c8e4ced5b31 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 1 Jun 2017 19:59:50 +0900 Subject: [PATCH] Use correct one line budget when using Rfc control style --- src/expr.rs | 28 ++++++++++++++--------- tests/target/configs-control_style-rfc.rs | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 tests/target/configs-control_style-rfc.rs diff --git a/src/expr.rs b/src/expr.rs index a8f1c3a01b5..b05c7ba6c97 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -326,23 +326,29 @@ pub fn rewrite_pair(lhs: &LHS, // Re-evaluate the rhs because we have more space now: let infix = infix.trim_right(); - let lhs_budget = try_opt!(context - .config - .max_width() - .checked_sub(shape.used_width() + prefix.len() + infix.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())), + Style::Rfc => { + shape + .block_indent(context.config.tab_spaces()) + .with_max_width(context.config) + } }; 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_shape = match context.config.control_style() { + Style::Default => { + let lhs_overhead = shape.used_width() + prefix.len() + infix.len(); + Shape { + width: try_opt!(context.config.max_width().checked_sub(lhs_overhead)), + ..shape + } + } + Style::Rfc => try_opt!(shape.sub_width(prefix.len() + infix.len())), + }; + let lhs_result = try_opt!(lhs.rewrite(context, lhs_shape)); Some(format!("{}{}{}\n{}{}{}", prefix, lhs_result, @@ -906,7 +912,7 @@ impl<'a> Rewrite for ControlFlow<'a> { Some(cond) => { let mut cond_shape = match context.config.control_style() { Style::Default => try_opt!(constr_shape.shrink_left(add_offset)), - Style::Rfc => constr_shape, + Style::Rfc => try_opt!(constr_shape.sub_width(add_offset)), }; if context.config.control_brace_style() != ControlBraceStyle::AlwaysNextLine { // 2 = " {".len() diff --git a/tests/target/configs-control_style-rfc.rs b/tests/target/configs-control_style-rfc.rs new file mode 100644 index 00000000000..a7213c34dfb --- /dev/null +++ b/tests/target/configs-control_style-rfc.rs @@ -0,0 +1,23 @@ +// rustfmt-control_style: Rfc + +// #1618 +fn main() { + loop { + if foo { + if ((right_paddle_speed < 0.) && + (right_paddle.position().y - paddle_size.y / 2. > 5.)) || + ((right_paddle_speed > 0.) && + (right_paddle.position().y + paddle_size.y / 2. < game_height as f32 - 5.)) + { + foo + } + if ai_timer.elapsed_time().as_microseconds() > ai_time.as_microseconds() { + if ball.position().y + ball_radius > + right_paddle.position().y + paddle_size.y / 2. + { + foo + } + } + } + } +}