From fa6892eb6389dbcdf8037670bd480767a61b5a8d Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Mon, 22 Jan 2018 13:04:20 +0900 Subject: [PATCH] Use field initialization shorthand if possible Also this PR fixes a bug that attributes on a shorthand field get removed. --- src/expr.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 5965f3007b40..09917ca088b5 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2565,9 +2565,13 @@ pub fn rewrite_field( if contains_skip(&field.attrs) { return Some(context.snippet(field.span()).to_owned()); } - let name = &field.ident.node.to_string(); + let mut attrs_str = field.attrs.rewrite(context, shape)?; + if !attrs_str.is_empty() { + attrs_str.push_str(&format!("\n{}", shape.indent.to_string(context.config))); + }; + let name = field.ident.node.to_string(); if field.is_shorthand { - Some(name.to_string()) + Some(attrs_str + &name) } else { let mut separator = String::from(struct_lit_field_separator(context.config)); for _ in 0..prefix_max_width.checked_sub(name.len()).unwrap_or(0) { @@ -2577,12 +2581,8 @@ pub fn rewrite_field( let expr_shape = shape.offset_left(overhead)?; let expr = field.expr.rewrite(context, expr_shape); - let mut attrs_str = field.attrs.rewrite(context, shape)?; - if !attrs_str.is_empty() { - attrs_str.push_str(&format!("\n{}", shape.indent.to_string(context.config))); - }; - match expr { + Some(ref e) if e.as_str() == name => Some(attrs_str + &name), Some(e) => Some(format!("{}{}{}{}", attrs_str, name, separator, e)), None => { let expr_offset = shape.indent.block_indent(context.config);