From 79ba34c60758f02b3aded90bae3fb9983f550234 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida <topecongiro@localhost.localdomain> Date: Mon, 15 May 2017 22:55:01 +0900 Subject: [PATCH] Use offset_left for rewrite_unary_prefix --- src/expr.rs | 36 ++++++++---------------------------- src/patterns.rs | 6 ++---- tests/source/macros.rs | 6 ++++++ tests/target/macros.rs | 6 ++++++ 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 585073c8f0c..729a98170af 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -156,11 +156,7 @@ fn format_expr(expr: &ast::Expr, }; if let Some(ref expr) = *opt_expr { - rewrite_unary_prefix(context, - &format!("break{} ", id_str), - &**expr, - shape, - expr.span) + rewrite_unary_prefix(context, &format!("break{} ", id_str), &**expr, shape) } else { wrap_str(format!("break{}", id_str), context.config.max_width, shape) } @@ -180,11 +176,9 @@ fn format_expr(expr: &ast::Expr, } ast::ExprKind::Ret(None) => wrap_str("return".to_owned(), context.config.max_width, shape), ast::ExprKind::Ret(Some(ref expr)) => { - rewrite_unary_prefix(context, "return ", &**expr, shape, expr.span) - } - ast::ExprKind::Box(ref expr) => { - rewrite_unary_prefix(context, "box ", &**expr, shape, expr.span) + rewrite_unary_prefix(context, "return ", &**expr, shape) } + ast::ExprKind::Box(ref expr) => rewrite_unary_prefix(context, "box ", &**expr, shape), ast::ExprKind::AddrOf(mutability, ref expr) => { rewrite_expr_addrof(context, mutability, expr, shape) } @@ -226,7 +220,7 @@ fn format_expr(expr: &ast::Expr, } else { delim.into() }; - rewrite_unary_prefix(context, &sp_delim, &**rhs, shape, expr.span) + rewrite_unary_prefix(context, &sp_delim, &**rhs, shape) } (Some(ref lhs), None) => { let sp_delim = if context.config.spaces_around_ranges { @@ -1999,24 +1993,10 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext, pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext, prefix: &str, rewrite: &R, - mut shape: Shape, - span: Span) + shape: Shape) -> Option<String> { - // Heuristic: if unary is `&` and `rewrite` contains `{`, - // it is likely that block indent is preferred to visual indent. - if prefix == "&" { - let snippet = String::from(context.snippet(span).trim_left_matches('&')); - let first_line = try_opt!(snippet.lines().nth(0)); - if first_line.contains("{") { - shape = try_opt!(shape.sub_width(prefix.len())).block_indent(0); - } else { - shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0); - } - } else { - shape = try_opt!(shape.shrink_left(prefix.len())).visual_indent(0); - } rewrite - .rewrite(context, shape) + .rewrite(context, try_opt!(shape.offset_left(prefix.len()))) .map(|r| format!("{}{}", prefix, r)) } @@ -2046,7 +2026,7 @@ fn rewrite_unary_op(context: &RewriteContext, ast::UnOp::Not => "!", ast::UnOp::Neg => "-", }; - rewrite_unary_prefix(context, operator_str, expr, shape, expr.span) + rewrite_unary_prefix(context, operator_str, expr, shape) } fn rewrite_assignment(context: &RewriteContext, @@ -2143,5 +2123,5 @@ fn rewrite_expr_addrof(context: &RewriteContext, ast::Mutability::Immutable => "&", ast::Mutability::Mutable => "&mut ", }; - rewrite_unary_prefix(context, operator_str, expr, shape, expr.span) + rewrite_unary_prefix(context, operator_str, expr, shape) } diff --git a/src/patterns.rs b/src/patterns.rs index 8edfc1deea0..75175cc7667 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -28,9 +28,7 @@ use syntax::codemap::{self, BytePos, Span}; impl Rewrite for Pat { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> { match self.node { - PatKind::Box(ref pat) => { - rewrite_unary_prefix(context, "box ", &**pat, shape, self.span) - } + PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape), PatKind::Ident(binding_mode, ident, ref sub_pat) => { let (prefix, mutability) = match binding_mode { BindingMode::ByRef(mutability) => ("ref ", mutability), @@ -74,7 +72,7 @@ impl Rewrite for Pat { } PatKind::Ref(ref pat, mutability) => { let prefix = format!("&{}", format_mutability(mutability)); - rewrite_unary_prefix(context, &prefix, &**pat, shape, self.span) + rewrite_unary_prefix(context, &prefix, &**pat, shape) } PatKind::Tuple(ref items, dotdot_pos) => { rewrite_tuple_pat(items, dotdot_pos, None, self.span, context, shape) diff --git a/tests/source/macros.rs b/tests/source/macros.rs index c8f625b935b..3eee8c543ba 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -79,3 +79,9 @@ gfx_pipeline!(pipe { fn issue_1279() { println!("dsfs"); // a comment } + +fn issue_1555() { + let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}", + "65454654654654654654654655464", + "4"); +} diff --git a/tests/target/macros.rs b/tests/target/macros.rs index c4f40cfe55b..1fd8130f824 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -80,3 +80,9 @@ gfx_pipeline!(pipe { fn issue_1279() { println!("dsfs"); // a comment } + +fn issue_1555() { + let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}", + "65454654654654654654654655464", + "4"); +}