Skip to content

Commit f7c4a44

Browse files
ytmimicalebcartwright
authored andcommitted
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 d418057 commit f7c4a44

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/items.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,12 +1469,17 @@ 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 lo = if let Some(generics) = struct_parts.generics {
1473+
generics.span.hi()
1474+
} else {
1475+
struct_parts.ident.span.hi()
1476+
};
14721477
result = overflow::rewrite_with_parens(
14731478
context,
14741479
&result,
14751480
fields.iter(),
14761481
shape,
1477-
span,
1482+
mk_sp(lo, span.hi()),
14781483
context.config.fn_call_width(),
14791484
None,
14801485
)?;

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)