Skip to content

Tuple struct field comments consume the field itself, only with doc comment #5215

@danjl1100

Description

@danjl1100

Describe the bug

When using the sequence /// doc comment, // regular comment, <field definition> within a tuple struct definition, rustfmt joins the field with the regular comment above, removing the field from the struct definition.

Input

struct MyTuple(
    /// Doc Comments
    // TODO note to add more to Doc Comments
    u32,
);

Output - correctness issue

struct MyTuple(
    /// Doc Comments
    // TODO note to add more to Doc Commentsu32,
);

The same applies for /* block comments */, but in this case the issue is purely cosmetic (does not change the struct definition).

Input (block comment)

struct MyTuple(
    /// Doc Comments
    /* TODO note to add more to Doc Comments */
    u32,
);

Output (block comment) - cosmetic issue

struct MyTuple(
    /// Doc Comments
    /* TODO note to add more to Doc Comments */u32,
);

At the very least, I expect there to be a separating space between the block comment and the u32, similar to the similar example with no doc comment

-    /* TODO note to add more to Doc Comments */u32,
+    /* TODO note to add more to Doc Comments */ u32,

Working cases

However, this issue doesn't seem to appear when the /// doc comment is removed, nor in named-field structs:

  • No doc comment - works as expected
    struct MyTuple(
        // TODO note to add more to Doc Comments
        u32,
    );
    
  • Struct with named fields (non-tuple) - works as expected
    struct MyStruct {
        /// Doc Comments
        // TODO note to add more to Doc Comments
        field: u32,
    }
    

To Reproduce

  • Tuple struct with doc comment, single line comment (broken) - Playground
  • Tuple struct with doc comment, block comment (broken, cosmetic only) - Playground
  • Tuple struct with single line comment, only (not broken, just for comparison) - Playground
  • Struct (named fields) with doc comment, single line comment (not broken, just for comparison) - Playground

Expected behavior

I expect rustfmt to never change the tuple struct definition (MyStruct(u32) --> MyStruct()), and also preserve cosmetic newlines in the block comment case.

Meta

  • rustfmt version: rustfmt 1.4.38-nightly (2022-02-02 27f5d83)
  • From where did you install rustfmt?: using playground only
  • How do you run rustfmt: using playground only

Additional remarks

Metadata

Metadata

Assignees

Labels

A-commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEE-help-wantedCall for participation: help is requested to fix this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions