Open
Description
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
cargo doc
under--no-default-features
google/zerocopy#537ytmimi commentedon Oct 24, 2023
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.[-]Spuriously removes newline between crate-level attributes[/-][+][Feature Request] Maintain newlines between crate-level attributes[/+]joshlf commentedon Oct 25, 2023
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.:
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.:
vs
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 commentedon Oct 25, 2023
In the event that a user unintentionally added whitespace would there be a good way to remove it? For example:
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 commentedon Oct 25, 2023
I'd expect rustfmt to leave it since it can't reason about why a user wanted to group certain attributes.
Yeah I agree that a configuration option is the best approach.