about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorDavid Bar-On <61089727+davidBar-On@users.noreply.github.com>2021-03-16 03:57:04 +0200
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2022-06-11 19:57:47 -0500
commit7d34cfaf2c31dc90f6dc8fc1cdc001fcbd7ebbfa (patch)
tree118cc273ea9d00d40b1e614ddec03ca8bb24dc73 /src
parent5fa2727ddeef534a7cd437f9e288c221a2cf0b6a (diff)
downloadrust-7d34cfaf2c31dc90f6dc8fc1cdc001fcbd7ebbfa.tar.gz
rust-7d34cfaf2c31dc90f6dc8fc1cdc001fcbd7ebbfa.zip
Dedup `imports_granularity = "Item"` (#4737)
* Fix for issue 4725 - dedup Item imports_granularity (2nd version)

* Use unique() instead of unique_by()
Diffstat (limited to 'src')
-rw-r--r--src/imports.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/imports.rs b/src/imports.rs
index 962f2126c66..559ed3917db 100644
--- a/src/imports.rs
+++ b/src/imports.rs
@@ -2,6 +2,10 @@ use std::borrow::Cow;
 use std::cmp::Ordering;
 use std::fmt;
 
+use core::hash::{Hash, Hasher};
+
+use itertools::Itertools;
+
 use rustc_ast::ast::{self, UseTreeKind};
 use rustc_span::{
     symbol::{self, sym},
@@ -87,7 +91,7 @@ impl<'a> FmtVisitor<'a> {
 // sorting.
 
 // FIXME we do a lot of allocation to make our own representation.
-#[derive(Clone, Eq, PartialEq)]
+#[derive(Clone, Eq, Hash, PartialEq)]
 pub(crate) enum UseSegment {
     Ident(String, Option<String>),
     Slf(Option<String>),
@@ -232,10 +236,13 @@ fn flatten_use_trees(
     use_trees: Vec<UseTree>,
     import_granularity: ImportGranularity,
 ) -> Vec<UseTree> {
+    // Return non-sorted single occurance of the use-trees text string;
+    // order is by first occurance of the use-tree.
     use_trees
         .into_iter()
         .flat_map(|tree| tree.flatten(import_granularity))
         .map(UseTree::nest_trailing_self)
+        .unique()
         .collect()
 }
 
@@ -780,6 +787,12 @@ fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree, merge_by:
     trees.sort();
 }
 
+impl Hash for UseTree {
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        self.path.hash(state);
+    }
+}
+
 impl PartialOrd for UseSegment {
     fn partial_cmp(&self, other: &UseSegment) -> Option<Ordering> {
         Some(self.cmp(other))