]> git.lizzy.rs Git - rust.git/commitdiff
Replace more Name::to_string usages with Name::to_smol_str
authorLukas Wirth <lukastw97@gmail.com>
Thu, 4 Nov 2021 17:12:05 +0000 (18:12 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Thu, 4 Nov 2021 17:12:05 +0000 (18:12 +0100)
24 files changed:
crates/hir/src/lib.rs
crates/hir_def/src/attr.rs
crates/hir_def/src/body/scope.rs
crates/hir_def/src/item_tree/lower.rs
crates/hir_def/src/lib.rs
crates/hir_def/src/nameres/collector.rs
crates/hir_def/src/nameres/mod_resolution.rs
crates/hir_def/src/path.rs
crates/hir_expand/src/lib.rs
crates/hir_expand/src/name.rs
crates/ide/src/display/navigation_target.rs
crates/ide/src/inlay_hints.rs
crates/ide/src/rename.rs
crates/ide_completion/src/completions/flyimport.rs
crates/ide_completion/src/completions/lifetime.rs
crates/ide_completion/src/completions/mod_.rs
crates/ide_db/src/defs.rs
crates/ide_db/src/helpers.rs
crates/ide_db/src/helpers/famous_defs.rs
crates/ide_db/src/helpers/import_assets.rs
crates/ide_db/src/search.rs
crates/ide_db/src/ty_filter.rs
crates/ide_diagnostics/src/handlers/missing_fields.rs
crates/ide_ssr/src/resolving.rs

index a221c4d57ebb214f8c1c639f2dc69c9c35f1d27c..2096c485e46c26d7654183aaf6d6d502c4d3e23e 100644 (file)
@@ -580,7 +580,7 @@ pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
                                     });
                                 for token in tokens {
                                     if token.kind() == SyntaxKind::IDENT
-                                        && token.text() == derive_name.as_str()
+                                        && token.text() == &**derive_name
                                     {
                                         precise_location = Some(token.text_range());
                                         break 'outer;
@@ -606,7 +606,12 @@ pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
                         }
                     };
                     acc.push(
-                        UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
+                        UnresolvedProcMacro {
+                            node,
+                            precise_location,
+                            macro_name: name.map(Into::into),
+                        }
+                        .into(),
                     );
                 }
 
@@ -2219,7 +2224,7 @@ pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>
             .attrs()
             .filter_map(|it| {
                 let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
-                if path.as_ident()?.to_string() == "derive" {
+                if path.as_ident()?.to_smol_str() == "derive" {
                     Some(it)
                 } else {
                     None
index 3bf7a330304c9c7248e47e596ef1841aeed319d2..4f68c91cc97bb88a457a468db348e0e4bd714264 100644 (file)
@@ -796,7 +796,7 @@ pub fn attrs(self) -> impl Iterator<Item = &'a Attr> + Clone {
         let key = self.key;
         self.attrs
             .iter()
-            .filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_string() == key))
+            .filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_smol_str() == key))
     }
 }
 
index 82b23f2f4674e46a12cfe4251c23e11867478c7c..f40eb2f9d41e38e8710e417637e2b30557cf73ee 100644 (file)
@@ -282,7 +282,7 @@ fn do_check(ra_fixture: &str, expected: &[&str]) {
         let actual = scopes
             .scope_chain(scope)
             .flat_map(|scope| scopes.entries(scope))
-            .map(|it| it.name().to_string())
+            .map(|it| it.name().to_smol_str())
             .collect::<Vec<_>>()
             .join("\n");
         let expected = expected.join("\n");
index a15125c3d4d286b1154cae3364fad66af7d68455..9381ca39f7e2a9153c5f875c1b66f7fd58126f8c 100644 (file)
@@ -449,7 +449,7 @@ fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Stati
 
     fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
         let mut name = konst.name().map(|it| it.as_name());
-        if name.as_ref().map_or(false, |n| n.to_string().starts_with("_DERIVE_")) {
+        if name.as_ref().map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_")) {
             // FIXME: this is a hack to treat consts generated by synstructure as unnamed
             // remove this some time in the future
             name = None;
index 6cc6bf98fbb5d573c35a51187f20a458845aaf3d..5ddef48495b5a21880c7781e545b2b171e05b32e 100644 (file)
@@ -764,7 +764,7 @@ fn derive_macro_as_call_id(
         krate,
         MacroCallKind::Derive {
             ast_id: item_attr.ast_id,
-            derive_name: last_segment.to_string(),
+            derive_name: last_segment.to_string().into_boxed_str(),
             derive_attr_index: derive_attr.ast_index,
         },
     );
@@ -801,7 +801,7 @@ fn attr_macro_as_call_id(
         krate,
         MacroCallKind::Attr {
             ast_id: item_attr.ast_id,
-            attr_name: last_segment.to_string(),
+            attr_name: last_segment.to_string().into_boxed_str(),
             attr_args: arg,
             invoc_attr_index: macro_attr.id.ast_index,
         },
index 0349754fbadde315d00dad7bfa2eb2eab28cea7b..1c578dbdc265dfb050fe03f7c65d7d5af49f82fa 100644 (file)
@@ -1759,7 +1759,7 @@ fn resolve_attributes(&mut self, attrs: &Attrs, mod_item: ModItem) -> Result<(),
     fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
         if path.kind == PathKind::Plain {
             if let Some(tool_module) = path.segments().first() {
-                let tool_module = tool_module.to_string();
+                let tool_module = tool_module.to_smol_str();
                 let is_tool = builtin_attr::TOOL_MODULES
                     .iter()
                     .copied()
@@ -1771,7 +1771,7 @@ fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
             }
 
             if let Some(name) = path.as_ident() {
-                let name = name.to_string();
+                let name = name.to_smol_str();
                 let is_inert = builtin_attr::INERT_ATTRIBUTES
                     .iter()
                     .chain(builtin_attr::EXTRA_ATTRIBUTES)
index 031ff8b181a69e4447911e9efc5e49f1ade15f37..bd2588e592e551ecbb19a1ef0980150192f0f81c 100644 (file)
@@ -42,7 +42,7 @@ pub(super) fn descend_into_definition(
         let path = match attr_path.map(|it| it.as_str()) {
             None => {
                 let mut path = self.dir_path.clone();
-                path.push(&name.to_string());
+                path.push(&name.to_smol_str());
                 path
             }
             Some(attr_path) => {
index 667092cd2083a1e30a7167ceca7a3fadc6d195b6..d17e6b1834eec324df818dda1c0d1a20f72b8736 100644 (file)
@@ -46,7 +46,7 @@ impl Display for ImportAlias {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match self {
             ImportAlias::Underscore => f.write_str("_"),
-            ImportAlias::Alias(name) => f.write_str(&name.to_string()),
+            ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
         }
     }
 }
index 4742cb089eb578c833b4bc990db48a5563f5b8ae..b6e8f58c2e44685cab58123b64ff471c17a4cac9 100644 (file)
@@ -122,7 +122,7 @@ pub enum MacroCallKind {
     },
     Derive {
         ast_id: AstId<ast::Item>,
-        derive_name: String,
+        derive_name: Box<str>,
         /// Syntactical index of the invoking `#[derive]` attribute.
         ///
         /// Outer attributes are counted first, then inner attributes. This does not support
@@ -131,7 +131,7 @@ pub enum MacroCallKind {
     },
     Attr {
         ast_id: AstId<ast::Item>,
-        attr_name: String,
+        attr_name: Box<str>,
         attr_args: (tt::Subtree, mbe::TokenMap),
         /// Syntactical index of the invoking `#[attribute]`.
         ///
index 0225ab425fcbbeb2b31f96b4162ed7e11948cca2..95d272ba682db02917ca92e11e9fda4605c3e85d 100644 (file)
@@ -84,7 +84,8 @@ pub fn as_text(&self) -> Option<SmolStr> {
     }
 
     /// Returns the textual representation of this name as a [`SmolStr`].
-    /// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper.
+    /// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
+    /// the general case.
     pub fn to_smol_str(&self) -> SmolStr {
         match &self.0 {
             Repr::Text(it) => it.clone(),
index 01c1259fc54ed1e5e8c0ba72b446601b6ca057f3..b21998e0d2293f1762c2c0fa814ad4b1b3dcb27c 100644 (file)
@@ -90,7 +90,7 @@ pub fn focus_or_full_range(&self) -> TextRange {
     }
 
     pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
-        let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
+        let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
         if let Some(src) = module.declaration_source(db) {
             let node = src.syntax();
             let full_range = node.original_file_range(db);
@@ -275,7 +275,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
 impl ToNav for hir::Module {
     fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
         let src = self.definition_source(db);
-        let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
+        let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
         let (syntax, focus) = match &src.value {
             ModuleSource::SourceFile(node) => (node.syntax(), None),
             ModuleSource::Module(node) => {
@@ -399,7 +399,7 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
 
         let full_range = src.with_value(&node).original_file_range(db);
         let name = match self.name(db) {
-            Some(it) => it.to_string().into(),
+            Some(it) => it.to_smol_str(),
             None => "".into(),
         };
         let kind = if self.is_self(db) {
@@ -429,7 +429,7 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
         let FileRange { file_id, range } = src.with_value(node).original_file_range(db);
         let focus_range =
             src.value.lifetime().and_then(|lt| lt.lifetime_ident_token()).map(|lt| lt.text_range());
-        let name = self.name(db).to_string().into();
+        let name = self.name(db).to_smol_str();
         NavigationTarget {
             file_id,
             name,
@@ -459,7 +459,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
         .map(|it| it.syntax().text_range());
         Some(NavigationTarget {
             file_id: src.file_id.original_file(db),
-            name: self.name(db).to_string().into(),
+            name: self.name(db).to_smol_str(),
             kind: Some(SymbolKind::TypeParam),
             full_range,
             focus_range,
@@ -476,7 +476,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
         let full_range = src.value.syntax().text_range();
         Some(NavigationTarget {
             file_id: src.file_id.original_file(db),
-            name: self.name(db).to_string().into(),
+            name: self.name(db).to_smol_str(),
             kind: Some(SymbolKind::LifetimeParam),
             full_range,
             focus_range: Some(full_range),
@@ -493,7 +493,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
         let full_range = src.value.syntax().text_range();
         Some(NavigationTarget {
             file_id: src.file_id.original_file(db),
-            name: self.name(db).to_string().into(),
+            name: self.name(db).to_smol_str(),
             kind: Some(SymbolKind::ConstParam),
             full_range,
             focus_range: src.value.name().map(|n| n.syntax().text_range()),
index db91e68ff31469b1e81d1f5cb68414939674166b..710d6b78821763f3a6f9eff5b4d25ecaae62cffc 100644 (file)
@@ -344,7 +344,7 @@ fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir
         enum_data
             .variants(db)
             .into_iter()
-            .map(|variant| variant.name(db).to_string())
+            .map(|variant| variant.name(db).to_smol_str())
             .any(|enum_name| enum_name == pat_text)
     } else {
         false
@@ -363,7 +363,7 @@ fn should_not_display_type_hint(
     }
 
     if let Some(hir::Adt::Struct(s)) = pat_ty.as_adt() {
-        if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() {
+        if s.fields(db).is_empty() && s.name(db).to_smol_str() == bind_pat.to_string() {
             return true;
         }
     }
@@ -419,7 +419,7 @@ fn should_hide_param_name_hint(
     }
 
     let fn_name = match callable.kind() {
-        hir::CallableKind::Function(it) => Some(it.name(sema.db).to_string()),
+        hir::CallableKind::Function(it) => Some(it.name(sema.db).to_smol_str()),
         _ => None,
     };
     let fn_name = fn_name.as_deref();
@@ -475,7 +475,9 @@ fn is_enum_name_similar_to_param_name(
     param_name: &str,
 ) -> bool {
     match sema.type_of_expr(argument).and_then(|t| t.original.as_adt()) {
-        Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
+        Some(hir::Adt::Enum(e)) => {
+            to_lower_snake_case(&e.name(sema.db).to_smol_str()) == param_name
+        }
         _ => false,
     }
 }
index 3afcccd64d93fc73482f329e0d30b1dfc4980e87..a7ebba82b03eaeb54f2efd7f0f61ff864e376d9b 100644 (file)
@@ -159,7 +159,7 @@ fn find_definitions(
                             // if the name differs from the definitions name it has to be an alias
                             if def
                                 .name(sema.db)
-                                .map_or(false, |it| it.to_string() != name_ref.text())
+                                .map_or(false, |it| it.to_smol_str() != name_ref.text().as_str())
                             {
                                 Err(format_err!("Renaming aliases is currently unsupported"))
                             } else {
index 956411f1a3bfa85e9a9e2f8fdbf598827db72378..47036f2e132cb7dd6eba790129b1a4b258ae192b 100644 (file)
@@ -209,7 +209,7 @@ pub(crate) fn compute_fuzzy_completion_order_key(
 ) -> usize {
     cov_mark::hit!(certain_fuzzy_order_test);
     let import_name = match proposed_mod_path.segments().last() {
-        Some(name) => name.to_string().to_lowercase(),
+        Some(name) => name.to_smol_str().to_lowercase(),
         None => return usize::MAX,
     };
     match import_name.match_indices(user_input_lowercased).next() {
index 283dc021d6ecb0c71339ded36c8ce09d35c5546f..f5308254346ed8ef24bd7531e777f63c35584d68 100644 (file)
@@ -34,7 +34,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
 
     ctx.scope.process_all_names(&mut |name, res| {
         if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
-            if param_lifetime != Some(&*name.to_string()) {
+            if param_lifetime != Some(&*name.to_smol_str()) {
                 acc.add_resolution(ctx, name, &res);
             }
         }
index 28be83f196aded7896ef1b4d8a6fa32f84eacf00..beef834581b9a0cf5bdfb518a38251005fd7b1f9 100644 (file)
@@ -121,7 +121,7 @@ fn directory_to_look_for_submodules(
     module_chain_to_containing_module_file(module, db)
         .into_iter()
         .filter_map(|module| module.name(db))
-        .try_fold(base_directory, |path, name| path.join(&name.to_string()))
+        .try_fold(base_directory, |path, name| path.join(&name.to_smol_str()))
 }
 
 fn module_chain_to_containing_module_file(
index 5bc8e8764f41f2ec6d4a59885b47b7841ba65e66..d95bca515fb95755c8ef1d70c90b3cff559e31b7 100644 (file)
@@ -404,7 +404,7 @@ pub fn classify(
                             hir::AssocItem::TypeAlias(it) => Some(*it),
                             _ => None,
                         })
-                        .find(|alias| alias.name(sema.db).to_string() == name_ref.text())
+                        .find(|alias| alias.name(sema.db).to_smol_str() == name_ref.text().as_str())
                     {
                         return Some(NameRefClass::Definition(Definition::ModuleDef(
                             ModuleDef::TypeAlias(ty),
index f6a1a5521836a8a1e41e258a217bae04301be4ed..5c5dbdfb5fe08f26d7678109ce782e3c2e9c8c5d 100644 (file)
@@ -113,7 +113,7 @@ pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
     segments.extend(
         path.segments()
             .iter()
-            .map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
+            .map(|segment| make::path_segment(make::name_ref(&segment.to_smol_str()))),
     );
     make::path_from_segments(segments, is_abs)
 }
index e8993d327f93b09d39dcb31795462c34a1343cb8..fd92c64f1740533fbc4a5728f5a9bf8e93928866 100644 (file)
@@ -139,7 +139,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)
     }
 
@@ -153,7 +153,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
@@ -161,7 +161,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)
     }
 }
index 0b3ecd095b04044e0917605f51914f42a6d541a0..9a8adf167c8a05f0a9248291b695e872b892e5d7 100644 (file)
@@ -410,7 +410,7 @@ fn find_import_for_segment(
     unresolved_first_segment: &str,
 ) -> Option<ItemInNs> {
     let segment_is_name = item_name(db, original_item)
-        .map(|name| name.to_string() == unresolved_first_segment)
+        .map(|name| name.to_smol_str() == unresolved_first_segment)
         .unwrap_or(false);
 
     Some(if segment_is_name {
@@ -434,7 +434,7 @@ fn module_with_segment_name(
     };
     while let Some(module) = current_module {
         if let Some(module_name) = module.name(db) {
-            if module_name.to_string() == segment_name {
+            if module_name.to_smol_str() == segment_name {
                 return Some(module);
             }
         }
index 707323272974f5308eb988f51fa4f8542d4e66f3..65deaf4d7df9069138035504a8fbcb7740f2ac05 100644 (file)
@@ -385,7 +385,7 @@ fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
             })
         });
         let name = match name {
-            Some(name) => name.to_string(),
+            Some(name) => name.to_smol_str(),
             None => return,
         };
         let name = name.as_str();
index 28c01d3173d2ae3b359d84bd354194d827912b72..2c6b00b1343b330a2fed635312eba3d56c6643c5 100644 (file)
@@ -26,7 +26,7 @@ pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &hir::Type) -> Option<TryEnum
             _ => return None,
         };
         TryEnum::ALL.iter().find_map(|&var| {
-            if enum_.name(sema.db).to_string() == var.type_name() {
+            if enum_.name(sema.db).to_smol_str() == var.type_name() {
                 return Some(var);
             }
             None
index 3d416591fe62f9504f949b45669285610b2b0622..8d17a7e714a6f373478b0580793a07afc88a67ef 100644 (file)
@@ -76,7 +76,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
             Some(make::ext::expr_todo())
         };
         let field =
-            make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_string()), field_expr)
+            make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_smol_str()), field_expr)
                 .clone_for_update();
         new_field_list.add_field(field);
     }
index 9cdd270b8036a2d52a40c7d2c7cff40c2f264aa7..84e5f82604b55b77dd823beef4e5d27559960885 100644 (file)
@@ -226,7 +226,7 @@ fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> {
                 None,
                 |_ty, assoc_item| {
                     let item_name = assoc_item.name(self.scope.db)?;
-                    if item_name.to_string().as_str() == name.text() {
+                    if item_name.to_smol_str().as_str() == name.text() {
                         Some(hir::PathResolution::AssocItem(assoc_item))
                     } else {
                         None