From 439a8194b0c498ee2ac12832c5d5d80316a6c0ab Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 4 Nov 2021 18:12:05 +0100 Subject: [PATCH] Replace more Name::to_string usages with Name::to_smol_str --- crates/hir/src/lib.rs | 11 ++++++++--- crates/hir_def/src/attr.rs | 2 +- crates/hir_def/src/body/scope.rs | 2 +- crates/hir_def/src/item_tree/lower.rs | 2 +- crates/hir_def/src/lib.rs | 4 ++-- crates/hir_def/src/nameres/collector.rs | 4 ++-- crates/hir_def/src/nameres/mod_resolution.rs | 2 +- crates/hir_def/src/path.rs | 2 +- crates/hir_expand/src/lib.rs | 4 ++-- crates/hir_expand/src/name.rs | 3 ++- crates/ide/src/display/navigation_target.rs | 14 +++++++------- crates/ide/src/inlay_hints.rs | 10 ++++++---- crates/ide/src/rename.rs | 2 +- crates/ide_completion/src/completions/flyimport.rs | 2 +- crates/ide_completion/src/completions/lifetime.rs | 2 +- crates/ide_completion/src/completions/mod_.rs | 2 +- crates/ide_db/src/defs.rs | 2 +- crates/ide_db/src/helpers.rs | 2 +- crates/ide_db/src/helpers/famous_defs.rs | 6 +++--- crates/ide_db/src/helpers/import_assets.rs | 4 ++-- crates/ide_db/src/search.rs | 2 +- crates/ide_db/src/ty_filter.rs | 2 +- .../ide_diagnostics/src/handlers/missing_fields.rs | 2 +- crates/ide_ssr/src/resolving.rs | 2 +- 24 files changed, 49 insertions(+), 41 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index a221c4d57eb..2096c485e46 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -580,7 +580,7 @@ impl Module { }); 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 @@ impl Module { } }; 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 @@ impl Impl { .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 diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 3bf7a330304..4f68c91cc97 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -796,7 +796,7 @@ impl<'a> AttrQuery<'a> { 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)) } } diff --git a/crates/hir_def/src/body/scope.rs b/crates/hir_def/src/body/scope.rs index 82b23f2f467..f40eb2f9d41 100644 --- a/crates/hir_def/src/body/scope.rs +++ b/crates/hir_def/src/body/scope.rs @@ -282,7 +282,7 @@ mod tests { 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::>() .join("\n"); let expected = expected.join("\n"); diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index a15125c3d4d..9381ca39f7e 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -449,7 +449,7 @@ impl<'a> Ctx<'a> { fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId { 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; diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 6cc6bf98fbb..5ddef48495b 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -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, }, diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 0349754fbad..1c578dbdc26 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1759,7 +1759,7 @@ impl ModCollector<'_, '_> { 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 @@ impl ModCollector<'_, '_> { } 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) diff --git a/crates/hir_def/src/nameres/mod_resolution.rs b/crates/hir_def/src/nameres/mod_resolution.rs index 031ff8b181a..bd2588e592e 100644 --- a/crates/hir_def/src/nameres/mod_resolution.rs +++ b/crates/hir_def/src/nameres/mod_resolution.rs @@ -42,7 +42,7 @@ impl ModDir { 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) => { diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 667092cd208..d17e6b1834e 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -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()), } } } diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 4742cb089eb..b6e8f58c2e4 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -122,7 +122,7 @@ pub enum MacroCallKind { }, Derive { ast_id: AstId, - derive_name: String, + derive_name: Box, /// 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, - attr_name: String, + attr_name: Box, attr_args: (tt::Subtree, mbe::TokenMap), /// Syntactical index of the invoking `#[attribute]`. /// diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index 0225ab425fc..95d272ba682 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs @@ -84,7 +84,8 @@ impl Name { } /// 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(), diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 01c1259fc54..b21998e0d22 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -90,7 +90,7 @@ impl NavigationTarget { } 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 @@ where 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 @@ impl ToNav for hir::Local { 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 @@ impl ToNav for hir::Label { 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 @@ impl TryToNav for hir::TypeParam { .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 @@ impl TryToNav for hir::LifetimeParam { 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 @@ impl TryToNav for hir::ConstParam { 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()), diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index db91e68ff31..710d6b78821 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -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, } } diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs index 3afcccd64d9..a7ebba82b03 100644 --- a/crates/ide/src/rename.rs +++ b/crates/ide/src/rename.rs @@ -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 { diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 956411f1a3b..47036f2e132 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -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() { diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs index 283dc021d6e..f5308254346 100644 --- a/crates/ide_completion/src/completions/lifetime.rs +++ b/crates/ide_completion/src/completions/lifetime.rs @@ -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); } } diff --git a/crates/ide_completion/src/completions/mod_.rs b/crates/ide_completion/src/completions/mod_.rs index 28be83f196a..beef834581b 100644 --- a/crates/ide_completion/src/completions/mod_.rs +++ b/crates/ide_completion/src/completions/mod_.rs @@ -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( diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index 5bc8e8764f4..d95bca515fb 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -404,7 +404,7 @@ impl NameRefClass { 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), diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index f6a1a552183..5c5dbdfb5fe 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -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) } diff --git a/crates/ide_db/src/helpers/famous_defs.rs b/crates/ide_db/src/helpers/famous_defs.rs index e8993d327f9..fd92c64f174 100644 --- a/crates/ide_db/src/helpers/famous_defs.rs +++ b/crates/ide_db/src/helpers/famous_defs.rs @@ -139,7 +139,7 @@ impl FamousDefs<'_, '_> { 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 @@ impl FamousDefs<'_, '_> { 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 @@ impl FamousDefs<'_, '_> { })?; } 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) } } diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 0b3ecd095b0..9a8adf167c8 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs @@ -410,7 +410,7 @@ fn find_import_for_segment( unresolved_first_segment: &str, ) -> Option { 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); } } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 70732327297..65deaf4d7df 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -385,7 +385,7 @@ impl<'a> FindUsages<'a> { }) }); let name = match name { - Some(name) => name.to_string(), + Some(name) => name.to_smol_str(), None => return, }; let name = name.as_str(); diff --git a/crates/ide_db/src/ty_filter.rs b/crates/ide_db/src/ty_filter.rs index 28c01d3173d..2c6b00b1343 100644 --- a/crates/ide_db/src/ty_filter.rs +++ b/crates/ide_db/src/ty_filter.rs @@ -26,7 +26,7 @@ impl 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 diff --git a/crates/ide_diagnostics/src/handlers/missing_fields.rs b/crates/ide_diagnostics/src/handlers/missing_fields.rs index 3d416591fe6..8d17a7e714a 100644 --- a/crates/ide_diagnostics/src/handlers/missing_fields.rs +++ b/crates/ide_diagnostics/src/handlers/missing_fields.rs @@ -76,7 +76,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option ResolutionScope<'db> { 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