Adjust non-empty tuple struct span to start before fields

Resolves 5011

Tuple structs with visibility modifiers and comments before the first
field were incorrectly formatted. Comments would duplicate part of the
visibility modifier and struct name.

When trying to parse the tuple fields the ``items::Context`` searches
for the opening '(', but because the visibility modifier introduces
another '(' -- for example ``pub(crate)`` -- the parsing gets messed up.

Now the span is adjusted to start after the struct identifier, or after
any generics. Adjusting the span in this way ensures that the
``items::Contex`` will correctly find the tuple fields.
This commit is contained in:
Yacin Tmimi 2021-10-05 01:24:10 -04:00 committed by Caleb Cartwright
parent d41805704d
commit f7c4a44149
3 changed files with 26 additions and 1 deletions

View File

@ -1469,12 +1469,17 @@ fn format_tuple_struct(
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
} else {
let shape = Shape::indented(offset, context.config).sub_width(1)?;
let lo = if let Some(generics) = struct_parts.generics {
generics.span.hi()
} else {
struct_parts.ident.span.hi()
};
result = overflow::rewrite_with_parens(
context,
&result,
fields.iter(),
shape,
span,
mk_sp(lo, span.hi()),
context.config.fn_call_width(),
None,
)?;

View File

@ -0,0 +1,12 @@
pub(crate) struct ASlash(
// hello
i32
);
pub(crate) struct AStar(
/* hello */
i32
);
pub(crate) struct BStar(/* hello */ i32);

View File

@ -0,0 +1,8 @@
pub(crate) struct ASlash(
// hello
i32,
);
pub(crate) struct AStar(/* hello */ i32);
pub(crate) struct BStar(/* hello */ i32);