10696: internal: Replace more Name::to_string usages with Name::to_smol_str r=Veykril a=Veykril

Gets rid of some more unnecessary string allocs
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-11-04 17:13:33 +00:00 committed by GitHub
commit 8720281f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 49 additions and 41 deletions

View File

@ -580,7 +580,7 @@ pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
});
for token in tokens {
if token.kind() == SyntaxKind::IDENT
&& token.text() == derive_name.as_str()
&& token.text() == &**derive_name
{
precise_location = Some(token.text_range());
break 'outer;
@ -606,7 +606,12 @@ pub fn diagnostics(self, db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>) {
}
};
acc.push(
UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
UnresolvedProcMacro {
node,
precise_location,
macro_name: name.map(Into::into),
}
.into(),
);
}
@ -2219,7 +2224,7 @@ pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>
.attrs()
.filter_map(|it| {
let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
if path.as_ident()?.to_string() == "derive" {
if path.as_ident()?.to_smol_str() == "derive" {
Some(it)
} else {
None

View File

@ -796,7 +796,7 @@ pub fn attrs(self) -> impl Iterator<Item = &'a Attr> + Clone {
let key = self.key;
self.attrs
.iter()
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_string() == key))
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_smol_str() == key))
}
}

View File

@ -282,7 +282,7 @@ fn do_check(ra_fixture: &str, expected: &[&str]) {
let actual = scopes
.scope_chain(scope)
.flat_map(|scope| scopes.entries(scope))
.map(|it| it.name().to_string())
.map(|it| it.name().to_smol_str())
.collect::<Vec<_>>()
.join("\n");
let expected = expected.join("\n");

View File

@ -449,7 +449,7 @@ fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Stati
fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
let mut name = konst.name().map(|it| it.as_name());
if name.as_ref().map_or(false, |n| n.to_string().starts_with("_DERIVE_")) {
if name.as_ref().map_or(false, |n| n.to_smol_str().starts_with("_DERIVE_")) {
// FIXME: this is a hack to treat consts generated by synstructure as unnamed
// remove this some time in the future
name = None;

View File

@ -764,7 +764,7 @@ fn derive_macro_as_call_id(
krate,
MacroCallKind::Derive {
ast_id: item_attr.ast_id,
derive_name: last_segment.to_string(),
derive_name: last_segment.to_string().into_boxed_str(),
derive_attr_index: derive_attr.ast_index,
},
);
@ -801,7 +801,7 @@ fn attr_macro_as_call_id(
krate,
MacroCallKind::Attr {
ast_id: item_attr.ast_id,
attr_name: last_segment.to_string(),
attr_name: last_segment.to_string().into_boxed_str(),
attr_args: arg,
invoc_attr_index: macro_attr.id.ast_index,
},

View File

@ -1759,7 +1759,7 @@ fn resolve_attributes(&mut self, attrs: &Attrs, mod_item: ModItem) -> Result<(),
fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
if path.kind == PathKind::Plain {
if let Some(tool_module) = path.segments().first() {
let tool_module = tool_module.to_string();
let tool_module = tool_module.to_smol_str();
let is_tool = builtin_attr::TOOL_MODULES
.iter()
.copied()
@ -1771,7 +1771,7 @@ fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
}
if let Some(name) = path.as_ident() {
let name = name.to_string();
let name = name.to_smol_str();
let is_inert = builtin_attr::INERT_ATTRIBUTES
.iter()
.chain(builtin_attr::EXTRA_ATTRIBUTES)

View File

@ -42,7 +42,7 @@ pub(super) fn descend_into_definition(
let path = match attr_path.map(|it| it.as_str()) {
None => {
let mut path = self.dir_path.clone();
path.push(&name.to_string());
path.push(&name.to_smol_str());
path
}
Some(attr_path) => {

View File

@ -46,7 +46,7 @@ impl Display for ImportAlias {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ImportAlias::Underscore => f.write_str("_"),
ImportAlias::Alias(name) => f.write_str(&name.to_string()),
ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
}
}
}

View File

@ -122,7 +122,7 @@ pub enum MacroCallKind {
},
Derive {
ast_id: AstId<ast::Item>,
derive_name: String,
derive_name: Box<str>,
/// Syntactical index of the invoking `#[derive]` attribute.
///
/// Outer attributes are counted first, then inner attributes. This does not support
@ -131,7 +131,7 @@ pub enum MacroCallKind {
},
Attr {
ast_id: AstId<ast::Item>,
attr_name: String,
attr_name: Box<str>,
attr_args: (tt::Subtree, mbe::TokenMap),
/// Syntactical index of the invoking `#[attribute]`.
///

View File

@ -84,7 +84,8 @@ pub fn as_text(&self) -> Option<SmolStr> {
}
/// Returns the textual representation of this name as a [`SmolStr`].
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper.
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
/// the general case.
pub fn to_smol_str(&self) -> SmolStr {
match &self.0 {
Repr::Text(it) => it.clone(),

View File

@ -90,7 +90,7 @@ pub fn focus_or_full_range(&self) -> TextRange {
}
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
if let Some(src) = module.declaration_source(db) {
let node = src.syntax();
let full_range = node.original_file_range(db);
@ -275,7 +275,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
impl ToNav for hir::Module {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.definition_source(db);
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
let (syntax, focus) = match &src.value {
ModuleSource::SourceFile(node) => (node.syntax(), None),
ModuleSource::Module(node) => {
@ -399,7 +399,7 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let full_range = src.with_value(&node).original_file_range(db);
let name = match self.name(db) {
Some(it) => it.to_string().into(),
Some(it) => it.to_smol_str(),
None => "".into(),
};
let kind = if self.is_self(db) {
@ -429,7 +429,7 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let FileRange { file_id, range } = src.with_value(node).original_file_range(db);
let focus_range =
src.value.lifetime().and_then(|lt| lt.lifetime_ident_token()).map(|lt| lt.text_range());
let name = self.name(db).to_string().into();
let name = self.name(db).to_smol_str();
NavigationTarget {
file_id,
name,
@ -459,7 +459,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
.map(|it| it.syntax().text_range());
Some(NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
name: self.name(db).to_smol_str(),
kind: Some(SymbolKind::TypeParam),
full_range,
focus_range,
@ -476,7 +476,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let full_range = src.value.syntax().text_range();
Some(NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
name: self.name(db).to_smol_str(),
kind: Some(SymbolKind::LifetimeParam),
full_range,
focus_range: Some(full_range),
@ -493,7 +493,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let full_range = src.value.syntax().text_range();
Some(NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
name: self.name(db).to_smol_str(),
kind: Some(SymbolKind::ConstParam),
full_range,
focus_range: src.value.name().map(|n| n.syntax().text_range()),

View File

@ -344,7 +344,7 @@ fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir
enum_data
.variants(db)
.into_iter()
.map(|variant| variant.name(db).to_string())
.map(|variant| variant.name(db).to_smol_str())
.any(|enum_name| enum_name == pat_text)
} else {
false
@ -363,7 +363,7 @@ fn should_not_display_type_hint(
}
if let Some(hir::Adt::Struct(s)) = pat_ty.as_adt() {
if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() {
if s.fields(db).is_empty() && s.name(db).to_smol_str() == bind_pat.to_string() {
return true;
}
}
@ -419,7 +419,7 @@ fn should_hide_param_name_hint(
}
let fn_name = match callable.kind() {
hir::CallableKind::Function(it) => Some(it.name(sema.db).to_string()),
hir::CallableKind::Function(it) => Some(it.name(sema.db).to_smol_str()),
_ => None,
};
let fn_name = fn_name.as_deref();
@ -475,7 +475,9 @@ fn is_enum_name_similar_to_param_name(
param_name: &str,
) -> bool {
match sema.type_of_expr(argument).and_then(|t| t.original.as_adt()) {
Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
Some(hir::Adt::Enum(e)) => {
to_lower_snake_case(&e.name(sema.db).to_smol_str()) == param_name
}
_ => false,
}
}

View File

@ -159,7 +159,7 @@ fn find_definitions(
// if the name differs from the definitions name it has to be an alias
if def
.name(sema.db)
.map_or(false, |it| it.to_string() != name_ref.text())
.map_or(false, |it| it.to_smol_str() != name_ref.text().as_str())
{
Err(format_err!("Renaming aliases is currently unsupported"))
} else {

View File

@ -209,7 +209,7 @@ pub(crate) fn compute_fuzzy_completion_order_key(
) -> usize {
cov_mark::hit!(certain_fuzzy_order_test);
let import_name = match proposed_mod_path.segments().last() {
Some(name) => name.to_string().to_lowercase(),
Some(name) => name.to_smol_str().to_lowercase(),
None => return usize::MAX,
};
match import_name.match_indices(user_input_lowercased).next() {

View File

@ -34,7 +34,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
ctx.scope.process_all_names(&mut |name, res| {
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
if param_lifetime != Some(&*name.to_string()) {
if param_lifetime != Some(&*name.to_smol_str()) {
acc.add_resolution(ctx, name, &res);
}
}

View File

@ -121,7 +121,7 @@ fn directory_to_look_for_submodules(
module_chain_to_containing_module_file(module, db)
.into_iter()
.filter_map(|module| module.name(db))
.try_fold(base_directory, |path, name| path.join(&name.to_string()))
.try_fold(base_directory, |path, name| path.join(&name.to_smol_str()))
}
fn module_chain_to_containing_module_file(

View File

@ -404,7 +404,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_smol_str() == name_ref.text().as_str())
{
return Some(NameRefClass::Definition(Definition::ModuleDef(
ModuleDef::TypeAlias(ty),

View File

@ -113,7 +113,7 @@ pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
segments.extend(
path.segments()
.iter()
.map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
.map(|segment| make::path_segment(make::name_ref(&segment.to_smol_str()))),
);
make::path_from_segments(segments, is_abs)
}

View File

@ -139,7 +139,7 @@ fn find_crate(&self, name: &str) -> Option<Crate> {
let krate = self.1?;
let db = self.0.db;
let res =
krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
krate.dependencies(db).into_iter().find(|dep| dep.name.to_smol_str() == name)?.krate;
Some(res)
}
@ -153,7 +153,7 @@ fn find_def(&self, path: &str) -> Option<ScopeDef> {
for segment in path {
module = module.children(db).find_map(|child| {
let name = child.name(db)?;
if name.to_string() == segment {
if name.to_smol_str() == segment {
Some(child)
} else {
None
@ -161,7 +161,7 @@ fn find_def(&self, path: &str) -> Option<ScopeDef> {
})?;
}
let def =
module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
module.scope(db, None).into_iter().find(|(name, _def)| name.to_smol_str() == trait_)?.1;
Some(def)
}
}

View File

@ -410,7 +410,7 @@ fn find_import_for_segment(
unresolved_first_segment: &str,
) -> Option<ItemInNs> {
let segment_is_name = item_name(db, original_item)
.map(|name| name.to_string() == unresolved_first_segment)
.map(|name| name.to_smol_str() == unresolved_first_segment)
.unwrap_or(false);
Some(if segment_is_name {
@ -434,7 +434,7 @@ fn module_with_segment_name(
};
while let Some(module) = current_module {
if let Some(module_name) = module.name(db) {
if module_name.to_string() == segment_name {
if module_name.to_smol_str() == segment_name {
return Some(module);
}
}

View File

@ -385,7 +385,7 @@ fn search(&self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) {
})
});
let name = match name {
Some(name) => name.to_string(),
Some(name) => name.to_smol_str(),
None => return,
};
let name = name.as_str();

View File

@ -26,7 +26,7 @@ pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &hir::Type) -> Option<TryEnum
_ => return None,
};
TryEnum::ALL.iter().find_map(|&var| {
if enum_.name(sema.db).to_string() == var.type_name() {
if enum_.name(sema.db).to_smol_str() == var.type_name() {
return Some(var);
}
None

View File

@ -76,7 +76,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
Some(make::ext::expr_todo())
};
let field =
make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_string()), field_expr)
make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_smol_str()), field_expr)
.clone_for_update();
new_field_list.add_field(field);
}

View File

@ -226,7 +226,7 @@ fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> {
None,
|_ty, assoc_item| {
let item_name = assoc_item.name(self.scope.db)?;
if item_name.to_string().as_str() == name.text() {
if item_name.to_smol_str().as_str() == name.text() {
Some(hir::PathResolution::AssocItem(assoc_item))
} else {
None