Skip to content

Commit 6b415bd

Browse files
authored
Dedup imports_granularity = "Item" (#4737)
* Fix for issue 4725 - dedup Item imports_granularity (2nd version) * Use unique() instead of unique_by()
1 parent 0e90855 commit 6b415bd

31 files changed

+62
-1
lines changed

Diff for: src/formatting/imports.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use std::borrow::Cow;
22
use std::cmp::Ordering;
33
use std::fmt;
44

5+
use core::hash::{Hash, Hasher};
6+
7+
use itertools::Itertools;
8+
59
use rustc_ast::ast::{self, UseTreeKind};
610
use rustc_span::{
711
symbol::{self, sym},
@@ -87,7 +91,7 @@ impl<'a> FmtVisitor<'a> {
8791
// sorting.
8892

8993
// FIXME we do a lot of allocation to make our own representation.
90-
#[derive(Clone, Eq, PartialEq)]
94+
#[derive(Clone, Eq, Hash, PartialEq)]
9195
pub(crate) enum UseSegment {
9296
Ident(String, Option<String>),
9397
Slf(Option<String>),
@@ -195,6 +199,8 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -
195199
}
196200

197201
pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
202+
// Return non-sorted single occurance of the use-trees text string;
203+
// order is by first occurance of the use-tree.
198204
use_trees
199205
.into_iter()
200206
.flat_map(UseTree::flatten)
@@ -209,6 +215,7 @@ pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
209215
}
210216
tree
211217
})
218+
.unique()
212219
.collect()
213220
}
214221

@@ -685,6 +692,12 @@ fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree, merge_by:
685692
trees.sort();
686693
}
687694

695+
impl Hash for UseTree {
696+
fn hash<H: Hasher>(&self, state: &mut H) {
697+
self.path.hash(state);
698+
}
699+
}
700+
688701
impl PartialOrd for UseSegment {
689702
fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
690703
Some(self.cmp(other))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::lexer;
2+
use crate::lexer::tokens::TokenData;
3+
use crate::lexer::{tokens::TokenData};
4+
use crate::lexer::self;
5+
use crate::lexer::{self};
6+
use crate::lexer::{self, tokens::TokenData};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-imports_granularity: Item
2+
// rustfmt-reorder_imports: false
3+
// rustfmt-group_imports: StdExternalCrate
4+
5+
use crate::lexer;
6+
use crate::lexer;
7+
use crate::lexer::tokens::TokenData;
8+
use crate::lexer::{tokens::TokenData};
9+
use crate::lexer::self;
10+
use crate::lexer;
11+
use crate::lexer;
12+
use crate::lexer::{self};
13+
use crate::lexer::{self, tokens::TokenData};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-imports_granularity: Item
2+
3+
use crate::lexer;
4+
use crate::lexer;
5+
use crate::lexer::tokens::TokenData;
6+
use crate::lexer::{tokens::TokenData};
7+
use crate::lexer::self;
8+
use crate::lexer;
9+
use crate::lexer;
10+
use crate::lexer::{self};
11+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::lexer;
2+
use crate::lexer;
3+
use crate::lexer::tokens::TokenData;
4+
use crate::lexer::tokens::TokenData;
5+
use crate::lexer::{self};
6+
use crate::lexer::{self, tokens::TokenData};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-imports_granularity: Item
2+
// rustfmt-reorder_imports: false
3+
// rustfmt-group_imports: StdExternalCrate
4+
5+
use crate::lexer;
6+
use crate::lexer::tokens::TokenData;
7+
use crate::lexer::{self};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-imports_granularity: Item
2+
3+
use crate::lexer;
4+
use crate::lexer::tokens::TokenData;
5+
use crate::lexer::{self};

0 commit comments

Comments
 (0)