-
Notifications
You must be signed in to change notification settings - Fork 931
Dedup imports_granularity = "Item"
#4737
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
Merged
calebcartwright
merged 2 commits into
rust-lang:master
from
davidBar-On:issue-4725-dedup-Item-imports_granularity
Mar 16, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
tests/source/imports/imports_granularity_default-with-dups.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use crate::lexer; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::{tokens::TokenData}; | ||
use crate::lexer::self; | ||
use crate::lexer::{self}; | ||
use crate::lexer::{self, tokens::TokenData}; |
13 changes: 13 additions & 0 deletions
13
tests/source/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// rustfmt-imports_granularity: Item | ||
// rustfmt-reorder_imports: false | ||
// rustfmt-group_imports: StdExternalCrate | ||
|
||
use crate::lexer; | ||
use crate::lexer; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::{tokens::TokenData}; | ||
use crate::lexer::self; | ||
use crate::lexer; | ||
use crate::lexer; | ||
use crate::lexer::{self}; | ||
use crate::lexer::{self, tokens::TokenData}; |
11 changes: 11 additions & 0 deletions
11
tests/source/imports/imports_granularity_item-with-dups.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// rustfmt-imports_granularity: Item | ||
|
||
use crate::lexer; | ||
use crate::lexer; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::{tokens::TokenData}; | ||
use crate::lexer::self; | ||
use crate::lexer; | ||
use crate::lexer; | ||
use crate::lexer::{self}; | ||
use crate::lexer::{self, tokens::TokenData}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
tests/target/imports/imports_granularity_default-with-dups.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use crate::lexer; | ||
use crate::lexer; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::{self}; | ||
use crate::lexer::{self, tokens::TokenData}; |
7 changes: 7 additions & 0 deletions
7
tests/target/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// rustfmt-imports_granularity: Item | ||
// rustfmt-reorder_imports: false | ||
// rustfmt-group_imports: StdExternalCrate | ||
|
||
use crate::lexer; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::{self}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// rustfmt-imports_granularity: Item | ||
|
||
use crate::lexer; | ||
use crate::lexer::tokens::TokenData; | ||
use crate::lexer::{self}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some test files that have multiple groups, and perhaps some that have comments too? Maybe something like this:
It might also be useful to have some duplicates but with varying visibility levels (we may not handle this well with the other variants either FWIW, but would be good to check while we're working on this one)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't add such test files as there are some issues, especially related to comments, so maybe it is better to discuss first (here or in a new issue) which of these issues should be resolved, whether there are cases where the original code should be used, what should be the expected results, etc.
For example, the for the following input:
"Crate" granularity produce the following output:
Comments
foo 3
andfoo 4
disappeared and there is an extra;
after commentsbar 1
andfoo 1
."Module" granularity produce the following output:
In addition to the same issues as for "Crate",
foo::{...}
includesself
twice: one withfoo 2
comment and one without a comment."Item" granularity produce the following output:
Comment
foo 1
is missing because in the original codecrate::foo::self
is beforecrate::foo::self /* foo 1 */
. Thebar 1
comment does appear, sincecrate::bar::self
is beforecrate::foobar::self; /* foobar 1 */
. As above,foo 3
andfoo 4
comments disappeared and there is an extra;
after commentsbar 1
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow there sure is 🙃
We've always had comment issues with this option (which was named
merge_imports
prior to a recent rename/restructure) so I'm not too surprised by some of those, but the emission of invalid code in this type of case is particularly concerning:Good call 👍
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Submitted PR #4759 to fix the extra semicolon issue.
I will also try to resolve the issue where in some cases if the same import module appears twice - once with a comment and once without a comment, it appears twice in the formatted output. E.g. in the above output for
Crate
, 'create::barand
crate:::foo` appear twice.Update:
The reason some import items appear twice for
Crate
andModule
granularity, is becausemerge_use_trees()
don't try to merge items with comments and just push them to the output:rustfmt/src/formatting/imports.rs
Lines 170 to 172 in a9876e8
This is probably a patch done because imports comments are not handled well, but it ignores duplicates.
There is a relatively simple way to remove these duplicates by ignoring all non-commented flattened items that have a duplicate with comment. However, because duplicates with and without comments are not expected to be common in practice, and because no issue was opened by users against this problem, it seems that it is not worth trying to add this enhancement.