Repository: intellij-rust/intellij-rust
Issue: Nest use statements is incorrect for renamed imports

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.4.178.4845-222-nightly
* **Rust toolchain version:** 1.65.0-nightly (addacb587 2022-08-24) x86_64-unknown-linux-gnu
* **IDE name and version:** CLion 2022.2.1 (CL-222.3739.54)
* **Operating system:** Linux 5.4.0-124-generic
* **Macro expansion engine:** new
* **Name resolution engine:** new
* **Additional experimental features:** org.rust.cargo.features.settings.gutter, org.rust.cargo.emulate.terminal, org.rust.cargo.evaluate.build.scripts, org.rust.macros.proc

## Problem description

The refactoring "Nest use statements produces invalid code when the nesting module  is renamed in the import.

## Steps to reproduce

```rust
mod one {
    pub fn two() {}
}
use /*caret*/one as ein;
use one::two;
```

Apply "Nest use statements":

```rust
mod one {
    pub fn two() {}
}

use one::{
    /*caret*/one as ein,
    two
};
```


Changed files:
src/main/kotlin/org/rust/ide/intentions/NestUseStatementsIntention.kt, src/test/kotlin/org/rust/ide/intentions/NestUseStatementsIntentionTest.kt

diff --git a/src/main/kotlin/org/rust/ide/intentions/NestUseStatementsIntention.kt b/src/main/kotlin/org/rust/ide/intentions/NestUseStatementsIntention.kt
index 9ae0c8c44..edadb57f1 100644
--- a/src/main/kotlin/org/rust/ide/intentions/NestUseStatementsIntention.kt
+++ b/src/main/kotlin/org/rust/ide/intentions/NestUseStatementsIntention.kt
@@ -78,13 +78,16 @@ class NestUseStatementsIntention : RsElementBaseIntentionAction<NestUseStatement
 
     private fun makeGroupedPath(basePath: String, useSpecks: List<RsUseSpeck>): String {
         val useSpecksInGroup = useSpecks.flatMap { useSpeck ->
-            // Remove first group
-            val useGroup = useSpeck.useGroup
-            if (useSpeck.path?.referenceName == basePath && useGroup != null) {
-                useGroup.useSpeckList.map { it.text }
-            } else {
-                listOf(deleteBasePath(useSpeck.text, basePath))
+            if (useSpeck.path?.text == basePath) {
+                // Remove first group
+                useSpeck.useGroup?.let { useGroup ->
+                    return@flatMap useGroup.useSpeckList.map { it.text }
+                }
+                useSpeck.alias?.let { alias ->
+                    return@flatMap listOf("self ${alias.text}")
+                }
             }
+            listOf(deleteBasePath(useSpeck.text, basePath))
         }
         return useSpecksInGroup.joinToString(",\\n", "$basePath::{\\n", "\\n}")
     }
diff --git a/src/test/kotlin/org/rust/ide/intentions/NestUseStatementsIntentionTest.kt b/src/test/kotlin/org/rust/ide/intentions/NestUseStatementsIntentionTest.kt
index cdff779a3..d3bc1f358 100644
--- a/src/test/kotlin/org/rust/ide/intentions/NestUseStatementsIntentionTest.kt
+++ b/src/test/kotlin/org/rust/ide/intentions/NestUseStatementsIntentionTest.kt
@@ -335,4 +335,111 @@ class NestUseStatementsIntentionTest : RsIntentionTestBase(NestUseStatementsInte
         };
 
     """)
+
+    fun `test imported item with same name as module 1`() = doAvailableTest("""
+        use a::/*caret*/a;
+        use a::b;
+    """, """
+        use a::{
+            a,
+            b
+        };
+
+    """)
+
+    fun `test imported item with same name as module 2`() = doAvailableTest("""
+        use ::a::/*caret*/a;
+        use ::a::b;
+    """, """
+        use ::a::{
+            a,
+            b
+        };
+
+    """)
+
+    fun `test imported item with same name as module renamed 1`() = doAvailableTest("""
+        use a::/*caret*/a as x;
+        use a::b;
+    """, """
+        use a::{
+            a as x,
+            b
+        };
+
+    """)
+
+    fun `test imported item with same name as module renamed 2`() = doAvailableTest("""
+        use ::a::/*caret*/a as x;
+        use ::a::b;
+    """, """
+        use ::a::{
+            a as x,
+            b
+        };
+
+    """)
+
+    fun `test renamed import 1`() = doAvailableTest("""
+        use foo/*caret*/::bar as baz;
+        use foo::quux;
+    """, """
+        use foo::{
+            bar as baz,
+            quux
+        };
+
+    """)
+
+    fun `test renamed import 2`() = doAvailableTest("""
+        use foo/*caret*/::quux;
+        use foo::bar as baz;
+    """, """
+        use foo::{
+            quux,
+            bar as baz
+        };
+
+    """)
+
+    fun `test renamed import 3`() = doAvailableTest("""
+        use top::{
+            foo::quux,
+            foo/*caret*/::bar as baz,
+        };
+    """, """
+        use top::{
+            foo::{
+                quux,
+                bar as baz
+            }
+        };
+    """)
+
+    fun `test renamed import as nesting root 1`() = doAvailableTest("""
+        use /*caret*/top as hop;
+        use top::foo;
+    """, """
+        use top::{
+            self as hop,
+            foo
+        };
+
+    """)
+
+    fun `test renamed import as nesting root 2`() = doAvailableTest("""
+        use top::{
+            /*caret*/foo::bar,
+            foo as boo,
+        };
+
+    """, """
+        use top::{
+            foo::{
+                bar,
+                self as boo
+            }
+        };
+
+    """)
 }