From a09ca681def98dcde966f5fe1f6c0af3bd8956ba Mon Sep 17 00:00:00 2001 From: rChaser53 Date: Wed, 28 Aug 2019 20:50:02 +0900 Subject: [PATCH] fix the bug removing attrs (#3760) --- src/patterns.rs | 59 +++++++++++++++++++++++++++++--------- tests/target/issue-3759.rs | 27 +++++++++++++++++ 2 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 tests/target/issue-3759.rs diff --git a/src/patterns.rs b/src/patterns.rs index b5032eca6d5..a0493be0f1c 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -2,7 +2,7 @@ use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSynt use syntax::ptr; use syntax::source_map::{self, BytePos, Span}; -use crate::comment::FindUncommented; +use crate::comment::{combine_strs_with_missing_comments, FindUncommented}; use crate::config::lists::*; use crate::expr::{can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field}; use crate::lists::{ @@ -179,7 +179,13 @@ fn rewrite_struct_pat( fields.iter(), terminator, ",", - |f| f.span.lo(), + |f| { + if f.node.attrs.is_empty() { + f.span.lo() + } else { + f.node.attrs.first().unwrap().span.lo() + } + }, |f| f.span.hi(), |f| f.node.rewrite(context, v_shape), context.snippet_provider.span_after(span, "{"), @@ -225,25 +231,50 @@ fn rewrite_struct_pat( impl Rewrite for FieldPat { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { - let pat = self.pat.rewrite(context, shape); - if self.is_shorthand { - pat + let hi_pos = if let Some(last) = self.attrs.last() { + last.span.hi() } else { - let pat_str = pat?; + self.pat.span.lo() + }; + + let attrs_str = if self.attrs.is_empty() { + String::from("") + } else { + self.attrs.rewrite(context, shape)? + }; + + let pat_str = self.pat.rewrite(context, shape)?; + if self.is_shorthand { + combine_strs_with_missing_comments( + context, + &attrs_str, + &pat_str, + mk_sp(hi_pos, self.pat.span.lo()), + shape, + false, + ) + } else { + let nested_shape = shape.block_indent(context.config.tab_spaces()); let id_str = rewrite_ident(context, self.ident); let one_line_width = id_str.len() + 2 + pat_str.len(); - if one_line_width <= shape.width { - Some(format!("{}: {}", id_str, pat_str)) + let pat_and_id_str = if one_line_width <= shape.width { + format!("{}: {}", id_str, pat_str) } else { - let nested_shape = shape.block_indent(context.config.tab_spaces()); - let pat_str = self.pat.rewrite(context, nested_shape)?; - Some(format!( + format!( "{}:\n{}{}", id_str, nested_shape.indent.to_string(context.config), - pat_str, - )) - } + self.pat.rewrite(context, nested_shape)? + ) + }; + combine_strs_with_missing_comments( + context, + &attrs_str, + &pat_and_id_str, + mk_sp(hi_pos, self.pat.span.lo()), + nested_shape, + false, + ) } } } diff --git a/tests/target/issue-3759.rs b/tests/target/issue-3759.rs new file mode 100644 index 00000000000..b53f5391a34 --- /dev/null +++ b/tests/target/issue-3759.rs @@ -0,0 +1,27 @@ +fn main() { + let Test { + #[cfg(feature = "test")] + x, + } = Test { + #[cfg(feature = "test")] + x: 1, + }; + + let Test { + #[cfg(feature = "test")] + // comment + x, + } = Test { + #[cfg(feature = "test")] + x: 1, + }; + + let Test { + // comment + #[cfg(feature = "test")] + x, + } = Test { + #[cfg(feature = "test")] + x: 1, + }; +}