Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustfmt removes import if it only contains comments #5899

Open
ytmimi opened this issue Aug 31, 2023 · 6 comments
Open

rustfmt removes import if it only contains comments #5899

ytmimi opened this issue Aug 31, 2023 · 6 comments
Labels

Comments

@ytmimi
Copy link
Contributor

ytmimi commented Aug 31, 2023

Formatting deletes the import if all of the items are commented out.

Reproducible using rustfmt 1.6.0-nightly (16db2a4b 2023-08-18) and rustfmt 1.5.3-nightly (f4b80cac 2023-06-30). Likely other versions as well.

input

use std::fs::{
    // File
};

output

expect

use std::fs::{
    // File
};

Originally posted by @okaneco in #5852 (comment)

@ytmimi
Copy link
Contributor Author

ytmimi commented Aug 31, 2023

@rsammelson any interest in helping out with this one since you also worked on #5853?

@ytmimi
Copy link
Contributor Author

ytmimi commented Aug 31, 2023

I also noticed that comments aren't necessary to trigger this.

input

use a::b::c::{};

output

@rsammelson
Copy link
Contributor

With the comment I would agree that this is definitely a bug, but without the comment isn't this desirable (just like removing the braces when they are unused; once the braces are gone it would be an invalid statement so it's just removed)?

@ytmimi
Copy link
Contributor Author

ytmimi commented Aug 31, 2023

I found this comment that suggests that the case without any comments is intentional:

rustfmt/src/imports.rs

Lines 537 to 543 in b636723

// Remove foo::{} or self without attributes.
match last.kind {
_ if self.attrs.is_some() => (),
UseSegmentKind::List(ref list) if list.is_empty() => {
self.path = vec![];
return self;
}

@okaneco
Copy link

okaneco commented Aug 31, 2023

I found this comment that suggests that the case without any comments is intentional:

That's how I found the originally posted issue, I was trying to work around that behavior which I do not find desirable and would like if there was an option to disable.

I would prefer that an unused import is preserved rather than deleted. This crops up for me while refactoring and prototyping with format-on-save, which results in having to retype it or go back in the undo-stack to find the deleted import (or disable formatting temporarily which is also inconvenient).

@ytmimi
Copy link
Contributor Author

ytmimi commented Aug 31, 2023

@okaneco thanks for providing that additional context!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants