Skip to content

Clean up more comments near use declarations#126776

Merged
bors merged 3 commits intorust-lang:masterfrom
nnethercote:rustfmt-use-pre-cleanups-2
Jul 17, 2024
Merged

Clean up more comments near use declarations#126776
bors merged 3 commits intorust-lang:masterfrom
nnethercote:rustfmt-use-pre-cleanups-2

Conversation

@nnethercote
Copy link
Copy Markdown
Contributor

@nnethercote nnethercote commented Jun 21, 2024

#125443 will reformat all use declarations in the repository. There are a few edge cases involving comments on use declarations that require care. This PR fixes them up so #125443 can go ahead with a simple x fmt --all. A follow-up to #126717.

r? @cuviper

[Post-merge update] Just to clarify what happened here: with auto-formatting of use declarations there is a very small loss of expressivity involving comments that apply to multiple use declarations. Imagine you want to write this:

use a::b::c;
use a::c::d;

// These two are special.
use a::d::e;
use a::e::f;

You just can't have a custom separate section any more; the sections are fixed. (Unless rust-lang/rustfmt#6241 is addressed, which may happen in the future.)

[Second update] I learned that you can have custom groups, by using #[rustfmt::skip]:

use a::b::c;
use a::c::d;

// These two are special.
#[rustfmt::skip]
use a::d::e;
use a::e::f;

The use a::d::e acts as a barrier: rustfmt won't reorder other use declarations around it. This is a very nice, idiomatic way of doing a custom group.)

[end of second update]

Fortunately, there are multiple reasonable alternatives:

use a::b::c;
use a::c::d;
use a::d::e; // this one is special
use a::e::f; // this one is special

or this:

use a::b::c;
use a::c::d;
// This one is special.
use a::d::e;
// This one is special.
use a::e::f; 

or this:

// `a::d::e` and `a::e::f` are special.

use a::b::c;
use a::c::d;
use a::d::e;
use a::e::f;

or you could even exclude the comment if it's not that important.

Comments having the first form are rare in practice. This PR converted the small number of them in the repository into one of the subsequent forms.

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

Labels

O-SGX Target: SGX O-wasi Operating system: Wasi, Webassembly System Interface S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants