diff --git a/src/patterns.rs b/src/patterns.rs index 0fa6edaa5d7..b1230a984f1 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -271,9 +271,13 @@ impl Rewrite for Pat { PatKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Pat) } - PatKind::Paren(ref pat) => pat - .rewrite(context, shape.offset_left(1)?.sub_width(1)?) - .map(|inner_pat| format!("({})", inner_pat)), + PatKind::Paren(ref pat) => { + let inner_pat = pat.rewrite(context, shape)?; + let pre_snippet = context.snippet(mk_sp(self.span.lo(), pat.span.lo())); + let post_snippet = context.snippet(mk_sp(pat.span.hi(), self.span.hi())); + let result = format!("{}{}{}", pre_snippet, inner_pat, post_snippet); + Some(result) + } } } } diff --git a/tests/source/comments_in_paren/comments_in_paren.rs b/tests/source/comments_in_paren/comments_in_paren.rs new file mode 100644 index 00000000000..1aeab77f185 --- /dev/null +++ b/tests/source/comments_in_paren/comments_in_paren.rs @@ -0,0 +1,54 @@ +// rustfmt-wrap_comments: true + +// Ensure comment preservation in parenthesis +fn main() { + let (/* */ () | () /* */) = (); + let (/**/ () | () /**/) = (); + let (/*comment*/ () | () /*comment*/) = (); + let (/*multi-line + comment*/ () | () /*multi-line + comment */) = (); + let (/*comment with new line + */ + () | () + /*comment with new line + */ + ) = (); +} + +// Ensure proper handling of line comments +fn line_comments() { + let (// Before + () + | + () + // After + ) = (); +} + +// Ensure proper handling of block comments with new lines +fn block_comments_with_new_lines() { + let (/* Before + * with new line */ + () + | + () + /* After + * with new line */ + ) = (); +} + +// Ensure inner pattern is getting formatted properly +// whilst preserving comments in outer parenthesis +fn inner_pat_formatting() { + let (/*comment*/ + ( + + ) + + + | ( + + ) + /*comment*/) = (); +} diff --git a/tests/target/comments_in_paren/comments_in_paren.rs b/tests/target/comments_in_paren/comments_in_paren.rs new file mode 100644 index 00000000000..eb45ad46b05 --- /dev/null +++ b/tests/target/comments_in_paren/comments_in_paren.rs @@ -0,0 +1,43 @@ +// rustfmt-wrap_comments: true + +// Ensure comment preservation in parenthesis +fn main() { + let (/* */ () | () /* */) = (); + let (/**/ () | () /**/) = (); + let (/*comment*/ () | () /*comment*/) = (); + let (/*multi-line + comment*/ () | () /*multi-line + comment */) = (); + let (/*comment with new line + */ + () | () + /*comment with new line + */ + ) = (); +} + +// Ensure proper handling of line comments +fn line_comments() { + let (// Before + () | () + // After + ) = (); +} + +// Ensure proper handling of block comments with new lines +fn block_comments_with_new_lines() { + let (/* Before + * with new line */ + () | () + /* After + * with new line */ + ) = (); +} + +// Ensure inner pattern is getting formatted properly +// whilst preserving comments in outer parenthesis +fn inner_pat_formatting() { + let (/*comment*/ + () | () + /*comment*/) = (); +}