about summary refs log tree commit diff
path: root/src/tools/rust-analyzer/crates/ide-db
diff options
context:
space:
mode:
authorShoyu Vanilla <modulo641@gmail.com>2024-09-22 20:23:59 +0900
committerShoyu Vanilla <modulo641@gmail.com>2024-09-24 23:48:04 +0900
commit8ca54f24e0fda5beaec369cc187c174eee1789eb (patch)
tree628d09781bfa5a09a559ec9f72aaaf120b32355d /src/tools/rust-analyzer/crates/ide-db
parent85e12146bacec41657dd7dbcffc61c8fbd3d80c0 (diff)
downloadrust-8ca54f24e0fda5beaec369cc187c174eee1789eb.tar.gz
rust-8ca54f24e0fda5beaec369cc187c174eee1789eb.zip
fix: Temporary fix for `remove_unused_imports` not handling import aliases correctly
Diffstat (limited to 'src/tools/rust-analyzer/crates/ide-db')
-rw-r--r--src/tools/rust-analyzer/crates/ide-db/src/search.rs23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-db/src/search.rs b/src/tools/rust-analyzer/crates/ide-db/src/search.rs
index ad30c624c41..4166b08339b 100644
--- a/src/tools/rust-analyzer/crates/ide-db/src/search.rs
+++ b/src/tools/rust-analyzer/crates/ide-db/src/search.rs
@@ -19,7 +19,7 @@ use parser::SyntaxKind;
 use rustc_hash::{FxHashMap, FxHashSet};
 use span::EditionedFileId;
 use syntax::{
-    ast::{self, HasName},
+    ast::{self, HasName, Rename},
     match_ast, AstNode, AstToken, SmolStr, SyntaxElement, SyntaxNode, TextRange, TextSize,
     ToSmolStr,
 };
@@ -405,6 +405,7 @@ impl Definition {
     pub fn usages<'a>(self, sema: &'a Semantics<'_, RootDatabase>) -> FindUsages<'a> {
         FindUsages {
             def: self,
+            rename: None,
             assoc_item_container: self.as_assoc_item(sema.db).map(|a| a.container(sema.db)),
             sema,
             scope: None,
@@ -417,6 +418,7 @@ impl Definition {
 #[derive(Clone)]
 pub struct FindUsages<'a> {
     def: Definition,
+    rename: Option<&'a Rename>,
     sema: &'a Semantics<'a, RootDatabase>,
     scope: Option<&'a SearchScope>,
     /// The container of our definition should it be an assoc item
@@ -447,6 +449,14 @@ impl<'a> FindUsages<'a> {
         self
     }
 
+    // FIXME: This is just a temporary fix for not handling import aliases like
+    // `use Foo as Bar`. We need to support them in a proper way.
+    // See issue #14079
+    pub fn with_rename(mut self, rename: Option<&'a Rename>) -> Self {
+        self.rename = rename;
+        self
+    }
+
     pub fn at_least_one(&self) -> bool {
         let mut found = false;
         self.search(&mut |_, _| {
@@ -884,9 +894,16 @@ impl<'a> FindUsages<'a> {
             }
         };
 
-        let name = match self.def {
+        let name = match (self.rename, self.def) {
+            (Some(rename), _) => {
+                if rename.underscore_token().is_some() {
+                    None
+                } else {
+                    rename.name().map(|n| n.to_smolstr())
+                }
+            }
             // special case crate modules as these do not have a proper name
-            Definition::Module(module) if module.is_crate_root() => {
+            (_, Definition::Module(module)) if module.is_crate_root() => {
                 // FIXME: This assumes the crate name is always equal to its display name when it
                 // really isn't
                 // we should instead look at the dependency edge name and recursively search our way