Skip to content

Commit 021a01a

Browse files
committed
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.
1 parent f0f449d commit 021a01a

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/items.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,12 +1469,16 @@ fn format_tuple_struct(
14691469
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
14701470
} else {
14711471
let shape = Shape::indented(offset, context.config).sub_width(1)?;
1472+
let span_start_before_tuple_fields = mk_sp(
1473+
context.snippet_provider.span_before_last(span, "("),
1474+
span.hi(),
1475+
);
14721476
result = overflow::rewrite_with_parens(
14731477
context,
14741478
&result,
14751479
fields.iter(),
14761480
shape,
1477-
span,
1481+
span_start_before_tuple_fields,
14781482
context.config.fn_call_width(),
14791483
None,
14801484
)?;

tests/source/issue-5011.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub(crate) struct ASlash(
2+
// hello
3+
i32
4+
);
5+
6+
pub(crate) struct AStar(
7+
/* hello */
8+
i32
9+
);
10+
11+
pub(crate) struct BStar(/* hello */ i32);
12+

tests/target/issue-5011.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub(crate) struct ASlash(
2+
// hello
3+
i32,
4+
);
5+
6+
pub(crate) struct AStar(/* hello */ i32);
7+
8+
pub(crate) struct BStar(/* hello */ i32);

0 commit comments

Comments
 (0)