Skip to content

Preserve comments before semicolon in use statements - #7067

Closed
mask66666 wants to merge 2 commits into
rust-lang:mainfrom
mask66666:fix/issue-7051
Closed

mask66666 wants to merge 2 commits into
rust-lang:mainfrom
mask66666:fix/issue-7051

Conversation

@mask66666

Copy link
Copy Markdown

Fixes #7051.

Recovers comments located between the imported path and the terminating semicolon (e.g. use std /* goodbye */;) on top-level use statements, preventing them from being dropped.

Includes regression tests in tests/source/issue-7051.rs.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 25, 2026

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to change the approach slightly to preserve the comments original location and I think we should expand the current set of test cases.

View changes since this review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense to leave the comment where it was written instead of moving it outside the ;. Let's try to perserve that.

Let's also expand on the test cases. What about nested imports? For example:

use std {
    sync::Arc /* some explanation */;
    thread;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with normal // comments? For example:

use std // comment
;

Should it stay like that or still move the comment outside the ;?

I think leaving the ; on its own line looks awkward.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's awkward to put a comment there in the first place. The best rustfmt can do is put the ; on the next line.

Comment on lines +3 to +4
use std // bye
;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's include a mutli-single-line test case.

use std // some
// multi-line
// set of single comments
;

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 26, 2026
@rustbot

rustbot commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@mask66666

Copy link
Copy Markdown
Author

I think I addressed all requests.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels Aug 27, 2026
Comment on lines +56 to +59
use std // comment
;

use std /* comment */ ;

@ytmimi ytmimi Aug 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two more test cases that I'm curious about. What if there's extra whitespace between the comment and the end of the use statement? How is that reformatted.

    use std



// comment
;

    use std


/* comment */ 
;

View changes since the review

Comment thread src/imports.rs
Comment on lines +361 to +363
if self.span.is_dummy() || self.has_comment() {
return Ok(format!("{lhs};"));
}

@ytmimi ytmimi Aug 27, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using span.is_dummy()? I don't think that's used anywhere else in the codebase. How much of this PR was written with AI?

View changes since the review

@mask66666 mask66666 closed this Aug 27, 2026
@rustbot rustbot removed the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

removes comment after import

3 participants