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/syntax/src/ast/make.rs | 4 ++-- crates/syntax/src/ast/node_ext.rs | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) (limited to 'crates/syntax') 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