diff options
| author | Yacin Tmimi <ytmimi@horizonmedia.com> | 2021-10-05 01:24:10 -0400 |
|---|---|---|
| committer | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2021-10-12 20:08:07 -0500 |
| commit | f7c4a44149b4e3a683f5506929050f2b50117328 (patch) | |
| tree | f3d8ec2d149bbabf47f12424fca4d62aca9551f3 | |
| parent | d41805704d4a07ac77e0bc59ea5c91cdb69dea26 (diff) | |
| download | rust-f7c4a44149b4e3a683f5506929050f2b50117328.tar.gz rust-f7c4a44149b4e3a683f5506929050f2b50117328.zip | |
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.
| -rw-r--r-- | src/items.rs | 7 | ||||
| -rw-r--r-- | tests/source/issue-5011.rs | 12 | ||||
| -rw-r--r-- | tests/target/issue-5011.rs | 8 |
3 files changed, 26 insertions, 1 deletions
diff --git a/src/items.rs b/src/items.rs index e8eb1c5dfbb..471a365e470 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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, )?; diff --git a/tests/source/issue-5011.rs b/tests/source/issue-5011.rs new file mode 100644 index 00000000000..b48292164e4 --- /dev/null +++ b/tests/source/issue-5011.rs @@ -0,0 +1,12 @@ +pub(crate) struct ASlash( + // hello + i32 +); + +pub(crate) struct AStar( + /* hello */ + i32 +); + +pub(crate) struct BStar(/* hello */ i32); + diff --git a/tests/target/issue-5011.rs b/tests/target/issue-5011.rs new file mode 100644 index 00000000000..9ad4a1929bd --- /dev/null +++ b/tests/target/issue-5011.rs @@ -0,0 +1,8 @@ +pub(crate) struct ASlash( + // hello + i32, +); + +pub(crate) struct AStar(/* hello */ i32); + +pub(crate) struct BStar(/* hello */ i32); |
