diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 1198e3f0be5..3ff135f411d 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -445,7 +445,7 @@ fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option Name { match self.as_tuple_field() { Some(idx) => Name::new_tuple_field(idx), - None => Name::resolve(self.text()), + None => Name::resolve(&self.text()), } } } impl AsName for ast::Name { fn as_name(&self) -> Name { - Name::resolve(self.text()) + Name::resolve(&self.text()) } } diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 2f840909c1c..5fb3e2d91c2 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -210,7 +210,7 @@ fn missing_record_expr_field_fix( } let new_field = make::record_field( None, - make::name(record_expr_field.field_name()?.text()), + make::name(&record_expr_field.field_name()?.text()), make::ty(&new_field_type.display_source_code(sema.db, module.into()).ok()?), ); diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 16c04eeeeec..25f96222cd8 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -416,7 +416,7 @@ fn get_string_representation(expr: &ast::Expr) -> Option { match expr { ast::Expr::MethodCallExpr(method_call_expr) => { let name_ref = method_call_expr.name_ref()?; - match name_ref.text() { + match name_ref.text().as_str() { "clone" => method_call_expr.receiver().map(|rec| rec.to_string()), name_ref => Some(name_ref.to_owned()), } diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index e503abc93a4..5bbadb0f45b 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs @@ -31,7 +31,7 @@ fn is_format_string(string: &ast::String) -> Option<()> { let parent = string.syntax().parent()?; let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?; - if !matches!(name.text(), "format_args" | "format_args_nl") { + if !matches!(name.text().as_str(), "format_args" | "format_args_nl") { return None; } diff --git a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs index 596c536a763..a8d6355bdd2 100644 --- a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs @@ -195,7 +195,7 @@ fn extract_struct_def( fn update_variant(rewriter: &mut SyntaxRewriter, variant: &ast::Variant) -> Option<()> { let name = variant.name()?; - let tuple_field = make::tuple_field(None, make::ty(name.text())); + let tuple_field = make::tuple_field(None, make::ty(&name.text())); let replacement = make::variant( name, Some(ast::FieldList::TupleFieldList(make::tuple_field_list(iter::once(tuple_field)))), diff --git a/crates/ide_assists/src/handlers/generate_enum_is_method.rs b/crates/ide_assists/src/handlers/generate_enum_is_method.rs index 7e181a4801e..a9f71a70329 100644 --- a/crates/ide_assists/src/handlers/generate_enum_is_method.rs +++ b/crates/ide_assists/src/handlers/generate_enum_is_method.rs @@ -44,7 +44,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) -> }; let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string()); - let fn_name = format!("is_{}", &to_lower_snake_case(variant_name.text())); + let fn_name = format!("is_{}", &to_lower_snake_case(&variant_name.text())); // Return early if we've found an existing new fn let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?; diff --git a/crates/ide_assists/src/handlers/generate_enum_projection_method.rs b/crates/ide_assists/src/handlers/generate_enum_projection_method.rs index 871bcab50b0..e2f572ba371 100644 --- a/crates/ide_assists/src/handlers/generate_enum_projection_method.rs +++ b/crates/ide_assists/src/handlers/generate_enum_projection_method.rs @@ -132,7 +132,8 @@ fn generate_enum_projection_method( ast::StructKind::Unit => return None, }; - let fn_name = format!("{}_{}", props.fn_name_prefix, &to_lower_snake_case(variant_name.text())); + let fn_name = + format!("{}_{}", props.fn_name_prefix, &to_lower_snake_case(&variant_name.text())); // Return early if we've found an existing new fn let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?; diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 4f0ef52ca04..f872d20c844 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -165,7 +165,7 @@ fn impl_def_from_trait( } let impl_def = make::impl_trait( trait_path.clone(), - make::path_unqualified(make::path_segment(make::name_ref(annotated_name.text()))), + make::path_unqualified(make::path_segment(make::name_ref(&annotated_name.text()))), ); let (impl_def, first_assoc_item) = add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope); @@ -178,12 +178,13 @@ fn update_attribute( trait_name: &ast::NameRef, attr: &ast::Attr, ) { + let trait_name = trait_name.text(); let new_attr_input = input .syntax() .descendants_with_tokens() .filter(|t| t.kind() == IDENT) .filter_map(|t| t.into_token().map(|t| t.text().to_string())) - .filter(|t| t != trait_name.text()) + .filter(|t| t != &trait_name) .collect::>(); let has_more_derives = !new_attr_input.is_empty(); diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 62f959082ec..9de9e4dbd3d 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -246,7 +246,7 @@ fn invert_special_case(sema: &Semantics, expr: &ast::Expr) -> Opti let method = mce.name_ref()?; let arg_list = mce.arg_list()?; - let method = match method.text() { + let method = match method.text().as_str() { "is_some" => "is_none", "is_none" => "is_some", "is_ok" => "is_err", @@ -442,7 +442,7 @@ fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str buf.push_str(trait_text); buf.push_str(" for "); } - buf.push_str(adt.name().unwrap().text()); + buf.push_str(&adt.name().unwrap().text()); if let Some(generic_params) = generic_params { let lifetime_params = generic_params .lifetime_params() diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index 75167ff3942..ab23dd7ac87 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -343,7 +343,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_string() == &name_ref.text()) { return Some(NameRefClass::Definition(Definition::ModuleDef( ModuleDef::TypeAlias(ty), diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 1881c746f69..3deb0d159de 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs @@ -288,7 +288,7 @@ fn path_applicable_imports( import_for_item( sema.db, mod_path, - unresolved_first_segment, + &unresolved_first_segment, &unresolved_qualifier, item, ) diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index 20c195f8251..e681ced801e 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -509,7 +509,7 @@ fn new(path: &ast::Path) -> ImportGroup { PathSegmentKind::SelfKw => ImportGroup::ThisModule, PathSegmentKind::SuperKw => ImportGroup::SuperModule, PathSegmentKind::CrateKw => ImportGroup::ThisCrate, - PathSegmentKind::Name(name) => match name.text() { + PathSegmentKind::Name(name) => match name.text().as_str() { "std" => ImportGroup::Std, "core" => ImportGroup::Std, _ => ImportGroup::ExternCrate, diff --git a/crates/ide_ssr/src/resolving.rs b/crates/ide_ssr/src/resolving.rs index dc78354739a..541da4122f7 100644 --- a/crates/ide_ssr/src/resolving.rs +++ b/crates/ide_ssr/src/resolving.rs @@ -150,7 +150,7 @@ fn resolve( fn path_contains_placeholder(&self, path: &ast::Path) -> bool { if let Some(segment) = path.segment() { if let Some(name_ref) = segment.name_ref() { - if self.placeholders_by_stand_in.contains_key(name_ref.text()) { + if self.placeholders_by_stand_in.contains_key(name_ref.text().as_str()) { return true; } } diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index c08f2c14f56..c6a7b99b7c9 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -268,14 +268,14 @@ pub fn arg_list(args: impl IntoIterator) -> ast::ArgList { } pub fn ident_pat(name: ast::Name) -> ast::IdentPat { - return from_text(name.text()); + return from_text(&name.text()); fn from_text(text: &str) -> ast::IdentPat { ast_from_text(&format!("fn f({}: ())", text)) } } pub fn ident_mut_pat(name: ast::Name) -> ast::IdentPat { - return from_text(name.text()); + return from_text(&name.text()); fn from_text(text: &str) -> ast::IdentPat { ast_from_text(&format!("fn f(mut {}: ())", text)) diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index bdf907a21a3..6d7db5fb26c 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -12,19 +12,19 @@ }; impl ast::Lifetime { - pub fn text(&self) -> &str { + pub fn text(&self) -> SmolStr { text_of_first_token(self.syntax()) } } impl ast::Name { - pub fn text(&self) -> &str { + pub fn text(&self) -> SmolStr { text_of_first_token(self.syntax()) } } impl ast::NameRef { - pub fn text(&self) -> &str { + pub fn text(&self) -> SmolStr { text_of_first_token(self.syntax()) } @@ -33,10 +33,8 @@ pub fn as_tuple_field(&self) -> Option { } } -fn text_of_first_token(node: &SyntaxNode) -> &str { - let t = - node.green().children().next().and_then(|it| it.into_token()).unwrap().text().to_string(); - Box::leak(Box::new(t)) +fn text_of_first_token(node: &SyntaxNode) -> SmolStr { + node.green().children().next().and_then(|it| it.into_token()).unwrap().text().into() } pub enum Macro { @@ -378,7 +376,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } impl NameOrNameRef { - pub fn text(&self) -> &str { + pub fn text(&self) -> SmolStr { match self { NameOrNameRef::Name(name) => name.text(), NameOrNameRef::NameRef(name_ref) => name_ref.text(),