From 35da0c174bf02d733b2cab4e3e4e45756b3c7405 Mon Sep 17 00:00:00 2001 From: David Bar-On Date: Mon, 22 Feb 2021 17:35:10 +0200 Subject: [PATCH 1/2] Fix for issue 4681 - Module imports_granularity and self in path --- src/formatting/imports.rs | 14 +++++++++++++- .../issue-4681-imports_granularity_default.rs | 6 ++++++ .../issue-4681-imports_granularity_module.rs | 8 ++++++++ .../issue-4681-imports_granularity_default.rs | 6 ++++++ .../issue-4681-imports_granularity_module.rs | 9 +++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-4681-imports_granularity_default.rs create mode 100644 tests/source/issue-4681-imports_granularity_module.rs create mode 100644 tests/target/issue-4681-imports_granularity_default.rs create mode 100644 tests/target/issue-4681-imports_granularity_module.rs diff --git a/src/formatting/imports.rs b/src/formatting/imports.rs index 7b688c2f11c..ae5482a167c 100644 --- a/src/formatting/imports.rs +++ b/src/formatting/imports.rs @@ -168,7 +168,19 @@ pub(crate) fn merge_use_trees(use_trees: Vec, merge_by: SharedPrefix) - continue; } - for flattened in use_tree.flatten() { + for mut flattened in use_tree.flatten() { + if merge_by == SharedPrefix::Module { + // If a path ends in `::self`, rewrite it to `::{self}`. + if let Some(UseSegment::Slf(..)) = flattened.path.last() { + let self_segment = flattened.path.pop().unwrap(); + flattened + .path + .push(UseSegment::List(vec![UseTree::from_path( + vec![self_segment], + DUMMY_SP, + )])); + } + } if let Some(tree) = result .iter_mut() .find(|tree| tree.share_prefix(&flattened, merge_by)) diff --git a/tests/source/issue-4681-imports_granularity_default.rs b/tests/source/issue-4681-imports_granularity_default.rs new file mode 100644 index 00000000000..cbb21a9f1b3 --- /dev/null +++ b/tests/source/issue-4681-imports_granularity_default.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/issue-4681-imports_granularity_module.rs b/tests/source/issue-4681-imports_granularity_module.rs new file mode 100644 index 00000000000..601ad070dc8 --- /dev/null +++ b/tests/source/issue-4681-imports_granularity_module.rs @@ -0,0 +1,8 @@ +// rustfmt-imports_granularity: Item + +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/target/issue-4681-imports_granularity_default.rs b/tests/target/issue-4681-imports_granularity_default.rs new file mode 100644 index 00000000000..5da6d588e6d --- /dev/null +++ b/tests/target/issue-4681-imports_granularity_default.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/issue-4681-imports_granularity_module.rs b/tests/target/issue-4681-imports_granularity_module.rs new file mode 100644 index 00000000000..2ecb12e42b8 --- /dev/null +++ b/tests/target/issue-4681-imports_granularity_module.rs @@ -0,0 +1,9 @@ +// rustfmt-imports_granularity: Item + +use crate::lexer; +use crate::lexer; +use crate::lexer::tokens::TokenData; +use crate::lexer::tokens::TokenData; +use crate::lexer::tokens::TokenData; +use crate::lexer::{self}; +use crate::lexer::{self}; From 21b59a7e947cddfd9e91a31eeff83d4d44198bdf Mon Sep 17 00:00:00 2001 From: David Bar-On Date: Wed, 24 Feb 2021 10:15:59 +0200 Subject: [PATCH 2/2] Change wrong rustfmt-imports_granularity Item in test file to Module --- tests/source/issue-4681-imports_granularity_module.rs | 5 +---- tests/target/issue-4681-imports_granularity_module.rs | 6 +----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/tests/source/issue-4681-imports_granularity_module.rs b/tests/source/issue-4681-imports_granularity_module.rs index 601ad070dc8..881a721261c 100644 --- a/tests/source/issue-4681-imports_granularity_module.rs +++ b/tests/source/issue-4681-imports_granularity_module.rs @@ -1,8 +1,5 @@ -// rustfmt-imports_granularity: Item +// rustfmt-imports_granularity: Module 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/target/issue-4681-imports_granularity_module.rs b/tests/target/issue-4681-imports_granularity_module.rs index 2ecb12e42b8..369743d437d 100644 --- a/tests/target/issue-4681-imports_granularity_module.rs +++ b/tests/target/issue-4681-imports_granularity_module.rs @@ -1,9 +1,5 @@ -// rustfmt-imports_granularity: Item +// rustfmt-imports_granularity: Module use crate::lexer; -use crate::lexer; -use crate::lexer::tokens::TokenData; use crate::lexer::tokens::TokenData; -use crate::lexer::tokens::TokenData; -use crate::lexer::{self}; use crate::lexer::{self};