Dedup imports_granularity = "Item" (#4737)

* Fix for issue 4725 - dedup Item imports_granularity (2nd version)

* Use unique() instead of unique_by()
This commit is contained in:
David Bar-On
2021-03-16 03:57:04 +02:00
committed by Caleb Cartwright
parent 5fa2727dde
commit 7d34cfaf2c
27 changed files with 62 additions and 1 deletions
+14 -1
View File
@@ -2,6 +2,10 @@
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 @@ pub(crate) fn format_import(&mut self, item: &ast::Item, tree: &ast::UseTree) {
// 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 @@ struct SimilarTree<'a> {
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))
@@ -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};
@@ -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};
@@ -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};
@@ -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};
@@ -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};
@@ -0,0 +1,5 @@
// rustfmt-imports_granularity: Item
use crate::lexer;
use crate::lexer::tokens::TokenData;
use crate::lexer::{self};