]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_db/src/helpers/famous_defs.rs
fix: Do not complete `Drop::drop`, complete `std::mem::drop` instead
[rust.git] / crates / ide_db / src / helpers / famous_defs.rs
index 7c59bc28d75562ee60d90ccd555ef5fba21d4345..ee7bf9540bc2b04b8e36b706c190e6ffd89fe676 100644 (file)
@@ -1,5 +1,5 @@
 //! See [`FamousDefs`].
-use hir::{Crate, Enum, Module, ScopeDef, Semantics, Trait};
+use hir::{Crate, Enum, MacroDef, Module, ScopeDef, Semantics, Trait};
 
 use crate::RootDatabase;
 
@@ -68,6 +68,49 @@ pub fn core_ops_Deref(&self) -> Option<Trait> {
         self.find_trait("core:ops:Deref")
     }
 
+    pub fn core_convert_AsRef(&self) -> Option<Trait> {
+        self.find_trait("core:convert:AsRef")
+    }
+
+    pub fn core_ops_ControlFlow(&self) -> Option<Enum> {
+        self.find_enum("core:ops:ControlFlow")
+    }
+
+    pub fn core_ops_Drop(&self) -> Option<Trait> {
+        self.find_trait("core:ops:Drop")
+    }
+
+    pub fn core_marker_Copy(&self) -> Option<Trait> {
+        self.find_trait("core:marker:Copy")
+    }
+
+    pub fn core_macros_builtin_derive(&self) -> Option<MacroDef> {
+        self.find_macro("core:macros:builtin:derive")
+    }
+
+    pub fn alloc(&self) -> Option<Crate> {
+        self.find_crate("alloc")
+    }
+
+    pub fn test(&self) -> Option<Crate> {
+        self.find_crate("test")
+    }
+
+    pub fn proc_macro(&self) -> Option<Crate> {
+        self.find_crate("proc_macro")
+    }
+
+    pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
+        IntoIterator::into_iter([
+            self.std(),
+            self.core(),
+            self.alloc(),
+            self.test(),
+            self.proc_macro(),
+        ])
+        .filter_map(|it| it)
+    }
+
     fn find_trait(&self, path: &str) -> Option<Trait> {
         match self.find_def(path)? {
             hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
@@ -75,6 +118,13 @@ fn find_trait(&self, path: &str) -> Option<Trait> {
         }
     }
 
+    fn find_macro(&self, path: &str) -> Option<MacroDef> {
+        match self.find_def(path)? {
+            hir::ScopeDef::MacroDef(it) => Some(it),
+            _ => None,
+        }
+    }
+
     fn find_enum(&self, path: &str) -> Option<Enum> {
         match self.find_def(path)? {
             hir::ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(it))) => Some(it),
@@ -93,7 +143,7 @@ fn find_crate(&self, name: &str) -> Option<Crate> {
         let krate = self.1?;
         let db = self.0.db;
         let res =
-            krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
+            krate.dependencies(db).into_iter().find(|dep| dep.name.to_smol_str() == name)?.krate;
         Some(res)
     }
 
@@ -107,7 +157,7 @@ fn find_def(&self, path: &str) -> Option<ScopeDef> {
         for segment in path {
             module = module.children(db).find_map(|child| {
                 let name = child.name(db)?;
-                if name.to_string() == segment {
+                if name.to_smol_str() == segment {
                     Some(child)
                 } else {
                     None
@@ -115,7 +165,7 @@ fn find_def(&self, path: &str) -> Option<ScopeDef> {
             })?;
         }
         let def =
-            module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
+            module.scope(db, None).into_iter().find(|(name, _def)| name.to_smol_str() == trait_)?.1;
         Some(def)
     }
 }