syntax: return owned string instead of leaking string

This commit is contained in:
cynecx 2021-03-26 18:30:59 +01:00
parent 4ecaad98e0
commit 5ff3299dd6
16 changed files with 27 additions and 27 deletions

View file

@ -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)

View file

@ -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())
}
}

View file

@ -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()?),
);

View file

@ -416,7 +416,7 @@ fn get_string_representation(expr: &ast::Expr) -> Option<String> {
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()),
}

View file

@ -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;
}

View file

@ -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)))),

View file

@ -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)?;

View file

@ -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)?;

View file

@ -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::<Vec<_>>();
let has_more_derives = !new_attr_input.is_empty();

View file

@ -246,7 +246,7 @@ fn invert_special_case(sema: &Semantics<RootDatabase>, 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()

View file

@ -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),

View file

@ -288,7 +288,7 @@ fn path_applicable_imports(
import_for_item(
sema.db,
mod_path,
unresolved_first_segment,
&unresolved_first_segment,
&unresolved_qualifier,
item,
)

View file

@ -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,

View file

@ -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;
}
}

View file

@ -268,14 +268,14 @@ pub fn arg_list(args: impl IntoIterator<Item = ast::Expr>) -> 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))

View file

@ -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(),