Skip to content

Trailing whitespace when wrapping long tuple struct fields with parenthesized visibility (pub(crate), pub(super)) #6880

@jlizen

Description

@jlizen

Summary

When a tuple struct field has a parenthesized visibility qualifier (pub(crate), pub(super), pub(in path)) and the type is too long to fit on one line with the visibility, rustfmt wraps the type to the next line but leaves a trailing space after the visibility qualifier. It then detects its own trailing whitespace and reports an internal error, but still writes the broken output to disk.

If the type is short enough to fit on one line, rustfmt collapses it successfully. Plain pub and named struct fields are not affected.

I tried to format this code:

struct S(
    pub(crate)
        std::collections::HashMap<some::module::path::TypeNameA, some::module::path::TypeNameB>,
);

Expected behavior

I expected rustfmt to format the file without introducing trailing whitespace (or leave it unmodified if it cannot).

Actual behavior

rustfmt writes pub(crate) \n (with a trailing space) to disk and reports:

error[internal]: left behind trailing whitespace
 --> src/lib.rs:2:2:15
  |
2 |     pub(crate)
  |               ^
  |

warning: rustfmt has failed to format. See previous 1 errors.

The input file has no trailing whitespace. After cargo fmt, the file on disk contains the trailing space.

The bug requires all three conditions:

Condition Triggers?
Parenthesized visibility (pub(crate), pub(super), pub(in path)) on tuple struct field, type too long for one line yes
Plain pub on tuple struct field, type too long for one line no
Parenthesized visibility on named struct field, type too long for one line no (collapses successfully)
Parenthesized visibility on tuple struct field, type fits on one line no (collapses successfully)

Configuration

No custom configuration. Default rustfmt settings.

$ cargo fmt

Reproduction Steps

  1. Save the code above to src/lib.rs in a Cargo project (the input must have no trailing whitespace)
  2. Run cargo fmt
  3. Observe the error[internal]: left behind trailing whitespace error
  4. Observe that the file on disk now contains trailing whitespace that was not present before

Meta

rustfmt --version:

rustfmt 1.8.0-stable (ed61e7d7e2 2025-11-07)

Also confirmed on rustfmt 1.9.0-stable (Rust 1.95.0).

Context

Discovered via smithy-rs#4634, where code generation produces tuple structs with pub(crate) fields and long type paths. The codegen pipeline runs cargo fmt on its output, and this bug causes it to fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions