Skip to content

[Feature Request] Maintain newlines between crate-level attributes #5947

Open
@joshlf

Description

@joshlf

Given the following code:

//! Foobar
#![doc = "baz"]

// Allow some lints
#![allow(unknown_lints)]

...rustfmt removes the newline separating the crate-level doc comment from the next block:

//! Foobar
#![doc = "baz"]
// Allow some lints
#![allow(unknown_lints)]

IMO this is the wrong behavior; in my particular use case, I intended to logically separate the crate-level doc comment from other crate-level attributes.

Activity

ytmimi

ytmimi commented on Oct 24, 2023

@ytmimi
Contributor

Thanks for reaching out and sharing your thoughts. To the best of my knowledge rustfmt doesn't currently special case the handling for any module level, inner attribute like #![doc = "baz"], #![allow(unknown_lints)], #![deny(...)], etc. to maintain whitespace between them.

changed the title [-]Spuriously removes newline between crate-level attributes[/-] [+][Feature Request] Maintain newlines between crate-level attributes[/+] on Oct 24, 2023
joshlf

joshlf commented on Oct 25, 2023

@joshlf
Author

IMO the right behavior in general would be to keep blocks separate, as that's often an intentional choice to group logically-related blocks, e.g.:

#![feature(foo)]
#![feature(bar)]
#![feature(baz)]

#![no_std]

As a slightly weaker position, even if we don't want to keep those separate, I'd argue that a leading comment should result in an exception. E.g.:

#![feature(foo)]
#![feature(bar)]
#![feature(baz)]

// We need to be no_std because it's cooler that way.
#![no_std]

vs

#![feature(foo)]
#![feature(bar)]
#![feature(baz)]
// We need to be no_std because it's cooler that way.
#![no_std]

IMO the latter is usually not what a programmer would want, as it makes it harder to skim a file and see the distinctions between logically-grouped blocks and to treat leading comments as "headers" for those blocks.

ytmimi

ytmimi commented on Oct 25, 2023

@ytmimi
Contributor

In the event that a user unintentionally added whitespace would there be a good way to remove it? For example:

#![feature(foo)]
                // <--- was this separation intentional?
#![feature(bar)]
#![feature(baz)]

#![no_std]

Also, The current behavior is to group all those attributes together. Although it's not your preference, there could be users out there who like what rustfmt is currently doing, and any solution that we come up with should keep that in mind.

The only approach that's jumping out to me right now is to provide some sort of configuration to allow users to specify their preference.

joshlf

joshlf commented on Oct 25, 2023

@joshlf
Author

In the event that a user unintentionally added whitespace would there be a good way to remove it? For example:

#![feature(foo)]
                // <--- was this separation intentional?
#![feature(bar)]
#![feature(baz)]

#![no_std]

I'd expect rustfmt to leave it since it can't reason about why a user wanted to group certain attributes.

Also, The current behavior is to group all those attributes together. Although it's not your preference, there could be users out there who like what rustfmt is currently doing, and any solution that we come up with should keep that in mind.

The only approach that's jumping out to me right now is to provide some sort of configuration to allow users to specify their preference.

Yeah I agree that a configuration option is the best approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @joshlf@ytmimi

        Issue actions

          [Feature Request] Maintain newlines between crate-level attributes · Issue #5947 · rust-lang/rustfmt