From 6b415bd23a98531d26a213288cc60ffd0e898ef3 Mon Sep 17 00:00:00 2001 From: David Bar-On <61089727+davidBar-On@users.noreply.github.com> Date: Tue, 16 Mar 2021 03:57:04 +0200 Subject: [PATCH] Dedup `imports_granularity = "Item"` (#4737) * Fix for issue 4725 - dedup Item imports_granularity (2nd version) * Use unique() instead of unique_by() --- src/formatting/imports.rs | 15 ++++++++++++++- .../source/{ => imports}/imports-impl-only-use.rs | 0 .../imports-reorder-lines-and-items.rs | 0 .../source/{ => imports}/imports-reorder-lines.rs | 0 tests/source/{ => imports}/imports-reorder.rs | 0 tests/source/{ => imports}/imports.rs | 0 .../source/{ => imports}/imports_2015_edition.rs | 0 .../source/{ => imports}/imports_block_indent.rs | 0 .../{ => imports}/imports_granularity_crate.rs | 0 .../imports_granularity_default-with-dups.rs | 6 ++++++ ..._item-with-dups-StdExternalCrate-no-reorder.rs | 13 +++++++++++++ .../imports/imports_granularity_item-with-dups.rs | 11 +++++++++++ .../{ => imports}/imports_granularity_item.rs | 0 .../{ => imports}/imports_granularity_module.rs | 0 .../{ => imports}/imports_raw_identifiers.rs | 0 .../{ => imports}/import-fencepost-length.rs | 0 .../target/{ => imports}/imports-impl-only-use.rs | 0 .../imports-reorder-lines-and-items.rs | 0 .../target/{ => imports}/imports-reorder-lines.rs | 0 tests/target/{ => imports}/imports-reorder.rs | 0 tests/target/{ => imports}/imports.rs | 0 .../target/{ => imports}/imports_2015_edition.rs | 0 .../target/{ => imports}/imports_2021_edition.rs | 0 .../target/{ => imports}/imports_block_indent.rs | 0 .../{ => imports}/imports_granularity_crate.rs | 0 .../imports_granularity_default-with-dups.rs | 6 ++++++ ..._item-with-dups-StdExternalCrate-no-reorder.rs | 7 +++++++ .../imports/imports_granularity_item-with-dups.rs | 5 +++++ .../{ => imports}/imports_granularity_item.rs | 0 .../{ => imports}/imports_granularity_module.rs | 0 .../{ => imports}/imports_raw_identifiers.rs | 0 31 files changed, 62 insertions(+), 1 deletion(-) rename tests/source/{ => imports}/imports-impl-only-use.rs (100%) rename tests/source/{ => imports}/imports-reorder-lines-and-items.rs (100%) rename tests/source/{ => imports}/imports-reorder-lines.rs (100%) rename tests/source/{ => imports}/imports-reorder.rs (100%) rename tests/source/{ => imports}/imports.rs (100%) rename tests/source/{ => imports}/imports_2015_edition.rs (100%) rename tests/source/{ => imports}/imports_block_indent.rs (100%) rename tests/source/{ => imports}/imports_granularity_crate.rs (100%) create mode 100644 tests/source/imports/imports_granularity_default-with-dups.rs create mode 100644 tests/source/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs create mode 100644 tests/source/imports/imports_granularity_item-with-dups.rs rename tests/source/{ => imports}/imports_granularity_item.rs (100%) rename tests/source/{ => imports}/imports_granularity_module.rs (100%) rename tests/source/{ => imports}/imports_raw_identifiers.rs (100%) rename tests/target/{ => imports}/import-fencepost-length.rs (100%) rename tests/target/{ => imports}/imports-impl-only-use.rs (100%) rename tests/target/{ => imports}/imports-reorder-lines-and-items.rs (100%) rename tests/target/{ => imports}/imports-reorder-lines.rs (100%) rename tests/target/{ => imports}/imports-reorder.rs (100%) rename tests/target/{ => imports}/imports.rs (100%) rename tests/target/{ => imports}/imports_2015_edition.rs (100%) rename tests/target/{ => imports}/imports_2021_edition.rs (100%) rename tests/target/{ => imports}/imports_block_indent.rs (100%) rename tests/target/{ => imports}/imports_granularity_crate.rs (100%) create mode 100644 tests/target/imports/imports_granularity_default-with-dups.rs create mode 100644 tests/target/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs create mode 100644 tests/target/imports/imports_granularity_item-with-dups.rs rename tests/target/{ => imports}/imports_granularity_item.rs (100%) rename tests/target/{ => imports}/imports_granularity_module.rs (100%) rename tests/target/{ => imports}/imports_raw_identifiers.rs (100%) diff --git a/src/formatting/imports.rs b/src/formatting/imports.rs index ae5482a167c..67edfe02007 100644 --- a/src/formatting/imports.rs +++ b/src/formatting/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), Slf(Option), @@ -195,6 +199,8 @@ pub(crate) fn merge_use_trees(use_trees: Vec, merge_by: SharedPrefix) - } pub(crate) fn flatten_use_trees(use_trees: Vec) -> Vec { + // 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(UseTree::flatten) @@ -209,6 +215,7 @@ pub(crate) fn flatten_use_trees(use_trees: Vec) -> Vec { } tree }) + .unique() .collect() } @@ -685,6 +692,12 @@ fn merge_use_trees_inner(trees: &mut Vec, use_tree: UseTree, merge_by: trees.sort(); } +impl Hash for UseTree { + fn hash(&self, state: &mut H) { + self.path.hash(state); + } +} + impl PartialOrd for UseSegment { fn partial_cmp(&self, other: &UseSegment) -> Option { Some(self.cmp(other)) diff --git a/tests/source/imports-impl-only-use.rs b/tests/source/imports/imports-impl-only-use.rs similarity index 100% rename from tests/source/imports-impl-only-use.rs rename to tests/source/imports/imports-impl-only-use.rs diff --git a/tests/source/imports-reorder-lines-and-items.rs b/tests/source/imports/imports-reorder-lines-and-items.rs similarity index 100% rename from tests/source/imports-reorder-lines-and-items.rs rename to tests/source/imports/imports-reorder-lines-and-items.rs diff --git a/tests/source/imports-reorder-lines.rs b/tests/source/imports/imports-reorder-lines.rs similarity index 100% rename from tests/source/imports-reorder-lines.rs rename to tests/source/imports/imports-reorder-lines.rs diff --git a/tests/source/imports-reorder.rs b/tests/source/imports/imports-reorder.rs similarity index 100% rename from tests/source/imports-reorder.rs rename to tests/source/imports/imports-reorder.rs diff --git a/tests/source/imports.rs b/tests/source/imports/imports.rs similarity index 100% rename from tests/source/imports.rs rename to tests/source/imports/imports.rs diff --git a/tests/source/imports_2015_edition.rs b/tests/source/imports/imports_2015_edition.rs similarity index 100% rename from tests/source/imports_2015_edition.rs rename to tests/source/imports/imports_2015_edition.rs diff --git a/tests/source/imports_block_indent.rs b/tests/source/imports/imports_block_indent.rs similarity index 100% rename from tests/source/imports_block_indent.rs rename to tests/source/imports/imports_block_indent.rs diff --git a/tests/source/imports_granularity_crate.rs b/tests/source/imports/imports_granularity_crate.rs similarity index 100% rename from tests/source/imports_granularity_crate.rs rename to tests/source/imports/imports_granularity_crate.rs diff --git a/tests/source/imports/imports_granularity_default-with-dups.rs b/tests/source/imports/imports_granularity_default-with-dups.rs new file mode 100644 index 00000000000..cbb21a9f1b3 --- /dev/null +++ b/tests/source/imports/imports_granularity_default-with-dups.rs @@ -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}; diff --git a/tests/source/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs b/tests/source/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs new file mode 100644 index 00000000000..e23705a884f --- /dev/null +++ b/tests/source/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs @@ -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}; diff --git a/tests/source/imports/imports_granularity_item-with-dups.rs b/tests/source/imports/imports_granularity_item-with-dups.rs new file mode 100644 index 00000000000..3e9589c299f --- /dev/null +++ b/tests/source/imports/imports_granularity_item-with-dups.rs @@ -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}; diff --git a/tests/source/imports_granularity_item.rs b/tests/source/imports/imports_granularity_item.rs similarity index 100% rename from tests/source/imports_granularity_item.rs rename to tests/source/imports/imports_granularity_item.rs diff --git a/tests/source/imports_granularity_module.rs b/tests/source/imports/imports_granularity_module.rs similarity index 100% rename from tests/source/imports_granularity_module.rs rename to tests/source/imports/imports_granularity_module.rs diff --git a/tests/source/imports_raw_identifiers.rs b/tests/source/imports/imports_raw_identifiers.rs similarity index 100% rename from tests/source/imports_raw_identifiers.rs rename to tests/source/imports/imports_raw_identifiers.rs diff --git a/tests/target/import-fencepost-length.rs b/tests/target/imports/import-fencepost-length.rs similarity index 100% rename from tests/target/import-fencepost-length.rs rename to tests/target/imports/import-fencepost-length.rs diff --git a/tests/target/imports-impl-only-use.rs b/tests/target/imports/imports-impl-only-use.rs similarity index 100% rename from tests/target/imports-impl-only-use.rs rename to tests/target/imports/imports-impl-only-use.rs diff --git a/tests/target/imports-reorder-lines-and-items.rs b/tests/target/imports/imports-reorder-lines-and-items.rs similarity index 100% rename from tests/target/imports-reorder-lines-and-items.rs rename to tests/target/imports/imports-reorder-lines-and-items.rs diff --git a/tests/target/imports-reorder-lines.rs b/tests/target/imports/imports-reorder-lines.rs similarity index 100% rename from tests/target/imports-reorder-lines.rs rename to tests/target/imports/imports-reorder-lines.rs diff --git a/tests/target/imports-reorder.rs b/tests/target/imports/imports-reorder.rs similarity index 100% rename from tests/target/imports-reorder.rs rename to tests/target/imports/imports-reorder.rs diff --git a/tests/target/imports.rs b/tests/target/imports/imports.rs similarity index 100% rename from tests/target/imports.rs rename to tests/target/imports/imports.rs diff --git a/tests/target/imports_2015_edition.rs b/tests/target/imports/imports_2015_edition.rs similarity index 100% rename from tests/target/imports_2015_edition.rs rename to tests/target/imports/imports_2015_edition.rs diff --git a/tests/target/imports_2021_edition.rs b/tests/target/imports/imports_2021_edition.rs similarity index 100% rename from tests/target/imports_2021_edition.rs rename to tests/target/imports/imports_2021_edition.rs diff --git a/tests/target/imports_block_indent.rs b/tests/target/imports/imports_block_indent.rs similarity index 100% rename from tests/target/imports_block_indent.rs rename to tests/target/imports/imports_block_indent.rs diff --git a/tests/target/imports_granularity_crate.rs b/tests/target/imports/imports_granularity_crate.rs similarity index 100% rename from tests/target/imports_granularity_crate.rs rename to tests/target/imports/imports_granularity_crate.rs diff --git a/tests/target/imports/imports_granularity_default-with-dups.rs b/tests/target/imports/imports_granularity_default-with-dups.rs new file mode 100644 index 00000000000..5da6d588e6d --- /dev/null +++ b/tests/target/imports/imports_granularity_default-with-dups.rs @@ -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}; diff --git a/tests/target/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs b/tests/target/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs new file mode 100644 index 00000000000..ed4df544d6f --- /dev/null +++ b/tests/target/imports/imports_granularity_item-with-dups-StdExternalCrate-no-reorder.rs @@ -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}; diff --git a/tests/target/imports/imports_granularity_item-with-dups.rs b/tests/target/imports/imports_granularity_item-with-dups.rs new file mode 100644 index 00000000000..00df37f9332 --- /dev/null +++ b/tests/target/imports/imports_granularity_item-with-dups.rs @@ -0,0 +1,5 @@ +// rustfmt-imports_granularity: Item + +use crate::lexer; +use crate::lexer::tokens::TokenData; +use crate::lexer::{self}; diff --git a/tests/target/imports_granularity_item.rs b/tests/target/imports/imports_granularity_item.rs similarity index 100% rename from tests/target/imports_granularity_item.rs rename to tests/target/imports/imports_granularity_item.rs diff --git a/tests/target/imports_granularity_module.rs b/tests/target/imports/imports_granularity_module.rs similarity index 100% rename from tests/target/imports_granularity_module.rs rename to tests/target/imports/imports_granularity_module.rs diff --git a/tests/target/imports_raw_identifiers.rs b/tests/target/imports/imports_raw_identifiers.rs similarity index 100% rename from tests/target/imports_raw_identifiers.rs rename to tests/target/imports/imports_raw_identifiers.rs