From 5ff3299dd61ea5c5790c01819994c9d8fa6afc09 Mon Sep 17 00:00:00 2001 From: cynecx Date: Fri, 26 Mar 2021 18:30:59 +0100 Subject: syntax: return owned string instead of leaking string --- crates/hir/src/semantics.rs | 2 +- crates/hir_expand/src/name.rs | 4 ++-- crates/ide/src/diagnostics/fixes.rs | 2 +- crates/ide/src/inlay_hints.rs | 2 +- crates/ide/src/syntax_highlighting/format.rs | 2 +- .../src/handlers/extract_struct_from_enum_variant.rs | 2 +- crates/ide_assists/src/handlers/generate_enum_is_method.rs | 2 +- .../src/handlers/generate_enum_projection_method.rs | 3 ++- .../src/handlers/replace_derive_with_manual_impl.rs | 5 +++-- crates/ide_assists/src/utils.rs | 4 ++-- crates/ide_db/src/defs.rs | 2 +- crates/ide_db/src/helpers/import_assets.rs | 2 +- crates/ide_db/src/helpers/insert_use.rs | 2 +- crates/ide_ssr/src/resolving.rs | 2 +- crates/syntax/src/ast/make.rs | 4 ++-- crates/syntax/src/ast/node_ext.rs | 14 ++++++-------- 16 files changed, 27 insertions(+), 27 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 1198e3f0b..3ff135f41 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -445,7 +445,7 @@ impl<'db> SemanticsImpl<'db> { } }; gpl.lifetime_params() - .find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()) == Some(text)) + .find(|tp| tp.lifetime().as_ref().map(|lt| lt.text()).as_ref() == Some(&text)) })?; let src = self.find_file(lifetime_param.syntax().clone()).with_value(lifetime_param); ToDef::to_def(self, src) diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index 0aeea48d5..cd691b1d2 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs @@ -75,14 +75,14 @@ impl AsName for ast::NameRef { fn as_name(&self) -> 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 2f840909c..5fb3e2d91 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 16c04eeee..25f96222c 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 e503abc93..5bbadb0f4 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 596c536a7..a8d6355bd 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 7e181a480..a9f71a703 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 871bcab50..e2f572ba3 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 4f0ef52ca..f872d20c8 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 62f959082..9de9e4dbd 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 75167ff39..ab23dd7ac 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -343,7 +343,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_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 1881c746f..3deb0d159 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 20c195f82..e681ced80 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -509,7 +509,7 @@ impl 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 dc7835473..541da4122 100644 --- a/crates/ide_ssr/src/resolving.rs +++ b/crates/ide_ssr/src/resolving.rs @@ -150,7 +150,7 @@ impl Resolver<'_, '_> { 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 c08f2c14f..c6a7b99b7 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 bdf907a21..6d7db5fb2 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -12,19 +12,19 @@ use crate::{ }; 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 @@ impl ast::NameRef { } } -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 @@ impl fmt::Display for NameOrNameRef { } 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(), -- cgit v1.2.3