8191: syntax: return owned string instead of leaking string r=cynecx a=cynecx

Quick hack? to alleviate https://github.com/rust-analyzer/rust-analyzer/issues/8181#issuecomment-806019126. I haven't run any tests but this should affect performance since we are deallocating more.

r? @matklad 

Co-authored-by: cynecx <me@cynecx.net>
This commit is contained in:
bors[bot] 2021-03-26 17:59:06 +00:00 committed by GitHub
commit 77a447dcda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 27 additions and 27 deletions

View File

@ -445,7 +445,7 @@ fn resolve_lifetime_param(&self, lifetime: &ast::Lifetime) -> Option<LifetimePar
}
};
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 @@ 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),

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

View File

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

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 @@
};
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<usize> {
}
}
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(),