Skip to content

Commit 7d34cfa

Browse files
davidBar-Oncalebcartwright
authored andcommitted
Dedup imports_granularity = "Item" (#4737)
* Fix for issue 4725 - dedup Item imports_granularity (2nd version) * Use unique() instead of unique_by()
1 parent 5fa2727 commit 7d34cfa

27 files changed

+62
-1
lines changed

src/imports.rs

Lines changed: 14 additions & 1 deletion
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>),
@@ -232,10 +236,13 @@ fn flatten_use_trees(
232236
use_trees: Vec<UseTree>,
233237
import_granularity: ImportGranularity,
234238
) -> Vec<UseTree> {
239+
// Return non-sorted single occurance of the use-trees text string;
240+
// order is by first occurance of the use-tree.
235241
use_trees
236242
.into_iter()
237243
.flat_map(|tree| tree.flatten(import_granularity))
238244
.map(UseTree::nest_trailing_self)
245+
.unique()
239246
.collect()
240247
}
241248

@@ -780,6 +787,12 @@ fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree, merge_by:
780787
trees.sort();
781788
}
782789

790+
impl Hash for UseTree {
791+
fn hash<H: Hasher>(&self, state: &mut H) {
792+
self.path.hash(state);
793+
}
794+
}
795+
783796
impl PartialOrd for UseSegment {
784797
fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
785798
Some(self.cmp(other))
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.
Lines changed: 6 additions & 0 deletions
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};
Lines changed: 13 additions & 0 deletions
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};

0 commit comments

Comments
 (0)