Skip to content

Commit 439a819

Browse files
committed
Replace more Name::to_string usages with Name::to_smol_str
1 parent 962be38 commit 439a819

File tree

24 files changed

+49
-41
lines changed

24 files changed

+49
-41
lines changed

crates/hir/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl Module {
580580
});
581581
for token in tokens {
582582
if token.kind() == SyntaxKind::IDENT
583-
&& token.text() == derive_name.as_str()
583+
&& token.text() == &**derive_name
584584
{
585585
precise_location = Some(token.text_range());
586586
break 'outer;
@@ -606,7 +606,12 @@ impl Module {
606606
}
607607
};
608608
acc.push(
609-
UnresolvedProcMacro { node, precise_location, macro_name: name }.into(),
609+
UnresolvedProcMacro {
610+
node,
611+
precise_location,
612+
macro_name: name.map(Into::into),
613+
}
614+
.into(),
610615
);
611616
}
612617

@@ -2219,7 +2224,7 @@ impl Impl {
22192224
.attrs()
22202225
.filter_map(|it| {
22212226
let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
2222-
if path.as_ident()?.to_string() == "derive" {
2227+
if path.as_ident()?.to_smol_str() == "derive" {
22232228
Some(it)
22242229
} else {
22252230
None

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl<'a> AttrQuery<'a> {
796796
let key = self.key;
797797
self.attrs
798798
.iter()
799-
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_string() == key))
799+
.filter(move |attr| attr.path.as_ident().map_or(false, |s| s.to_smol_str() == key))
800800
}
801801
}
802802

crates/hir_def/src/body/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ mod tests {
282282
let actual = scopes
283283
.scope_chain(scope)
284284
.flat_map(|scope| scopes.entries(scope))
285-
.map(|it| it.name().to_string())
285+
.map(|it| it.name().to_smol_str())
286286
.collect::<Vec<_>>()
287287
.join("\n");
288288
let expected = expected.join("\n");

crates/hir_def/src/item_tree/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'a> Ctx<'a> {
449449

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

crates/hir_def/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ fn derive_macro_as_call_id(
764764
krate,
765765
MacroCallKind::Derive {
766766
ast_id: item_attr.ast_id,
767-
derive_name: last_segment.to_string(),
767+
derive_name: last_segment.to_string().into_boxed_str(),
768768
derive_attr_index: derive_attr.ast_index,
769769
},
770770
);
@@ -801,7 +801,7 @@ fn attr_macro_as_call_id(
801801
krate,
802802
MacroCallKind::Attr {
803803
ast_id: item_attr.ast_id,
804-
attr_name: last_segment.to_string(),
804+
attr_name: last_segment.to_string().into_boxed_str(),
805805
attr_args: arg,
806806
invoc_attr_index: macro_attr.id.ast_index,
807807
},

crates/hir_def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,7 @@ impl ModCollector<'_, '_> {
17591759
fn is_builtin_or_registered_attr(&self, path: &ModPath) -> bool {
17601760
if path.kind == PathKind::Plain {
17611761
if let Some(tool_module) = path.segments().first() {
1762-
let tool_module = tool_module.to_string();
1762+
let tool_module = tool_module.to_smol_str();
17631763
let is_tool = builtin_attr::TOOL_MODULES
17641764
.iter()
17651765
.copied()
@@ -1771,7 +1771,7 @@ impl ModCollector<'_, '_> {
17711771
}
17721772

17731773
if let Some(name) = path.as_ident() {
1774-
let name = name.to_string();
1774+
let name = name.to_smol_str();
17751775
let is_inert = builtin_attr::INERT_ATTRIBUTES
17761776
.iter()
17771777
.chain(builtin_attr::EXTRA_ATTRIBUTES)

crates/hir_def/src/nameres/mod_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl ModDir {
4242
let path = match attr_path.map(|it| it.as_str()) {
4343
None => {
4444
let mut path = self.dir_path.clone();
45-
path.push(&name.to_string());
45+
path.push(&name.to_smol_str());
4646
path
4747
}
4848
Some(attr_path) => {

crates/hir_def/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Display for ImportAlias {
4646
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4747
match self {
4848
ImportAlias::Underscore => f.write_str("_"),
49-
ImportAlias::Alias(name) => f.write_str(&name.to_string()),
49+
ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
5050
}
5151
}
5252
}

crates/hir_expand/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub enum MacroCallKind {
122122
},
123123
Derive {
124124
ast_id: AstId<ast::Item>,
125-
derive_name: String,
125+
derive_name: Box<str>,
126126
/// Syntactical index of the invoking `#[derive]` attribute.
127127
///
128128
/// Outer attributes are counted first, then inner attributes. This does not support
@@ -131,7 +131,7 @@ pub enum MacroCallKind {
131131
},
132132
Attr {
133133
ast_id: AstId<ast::Item>,
134-
attr_name: String,
134+
attr_name: Box<str>,
135135
attr_args: (tt::Subtree, mbe::TokenMap),
136136
/// Syntactical index of the invoking `#[attribute]`.
137137
///

crates/hir_expand/src/name.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl Name {
8484
}
8585

8686
/// Returns the textual representation of this name as a [`SmolStr`].
87-
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper.
87+
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
88+
/// the general case.
8889
pub fn to_smol_str(&self) -> SmolStr {
8990
match &self.0 {
9091
Repr::Text(it) => it.clone(),

crates/ide/src/display/navigation_target.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl NavigationTarget {
9090
}
9191

9292
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
93-
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
93+
let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
9494
if let Some(src) = module.declaration_source(db) {
9595
let node = src.syntax();
9696
let full_range = node.original_file_range(db);
@@ -275,7 +275,7 @@ where
275275
impl ToNav for hir::Module {
276276
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
277277
let src = self.definition_source(db);
278-
let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default();
278+
let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
279279
let (syntax, focus) = match &src.value {
280280
ModuleSource::SourceFile(node) => (node.syntax(), None),
281281
ModuleSource::Module(node) => {
@@ -399,7 +399,7 @@ impl ToNav for hir::Local {
399399

400400
let full_range = src.with_value(&node).original_file_range(db);
401401
let name = match self.name(db) {
402-
Some(it) => it.to_string().into(),
402+
Some(it) => it.to_smol_str(),
403403
None => "".into(),
404404
};
405405
let kind = if self.is_self(db) {
@@ -429,7 +429,7 @@ impl ToNav for hir::Label {
429429
let FileRange { file_id, range } = src.with_value(node).original_file_range(db);
430430
let focus_range =
431431
src.value.lifetime().and_then(|lt| lt.lifetime_ident_token()).map(|lt| lt.text_range());
432-
let name = self.name(db).to_string().into();
432+
let name = self.name(db).to_smol_str();
433433
NavigationTarget {
434434
file_id,
435435
name,
@@ -459,7 +459,7 @@ impl TryToNav for hir::TypeParam {
459459
.map(|it| it.syntax().text_range());
460460
Some(NavigationTarget {
461461
file_id: src.file_id.original_file(db),
462-
name: self.name(db).to_string().into(),
462+
name: self.name(db).to_smol_str(),
463463
kind: Some(SymbolKind::TypeParam),
464464
full_range,
465465
focus_range,
@@ -476,7 +476,7 @@ impl TryToNav for hir::LifetimeParam {
476476
let full_range = src.value.syntax().text_range();
477477
Some(NavigationTarget {
478478
file_id: src.file_id.original_file(db),
479-
name: self.name(db).to_string().into(),
479+
name: self.name(db).to_smol_str(),
480480
kind: Some(SymbolKind::LifetimeParam),
481481
full_range,
482482
focus_range: Some(full_range),
@@ -493,7 +493,7 @@ impl TryToNav for hir::ConstParam {
493493
let full_range = src.value.syntax().text_range();
494494
Some(NavigationTarget {
495495
file_id: src.file_id.original_file(db),
496-
name: self.name(db).to_string().into(),
496+
name: self.name(db).to_smol_str(),
497497
kind: Some(SymbolKind::ConstParam),
498498
full_range,
499499
focus_range: src.value.name().map(|n| n.syntax().text_range()),

crates/ide/src/inlay_hints.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir
344344
enum_data
345345
.variants(db)
346346
.into_iter()
347-
.map(|variant| variant.name(db).to_string())
347+
.map(|variant| variant.name(db).to_smol_str())
348348
.any(|enum_name| enum_name == pat_text)
349349
} else {
350350
false
@@ -363,7 +363,7 @@ fn should_not_display_type_hint(
363363
}
364364

365365
if let Some(hir::Adt::Struct(s)) = pat_ty.as_adt() {
366-
if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() {
366+
if s.fields(db).is_empty() && s.name(db).to_smol_str() == bind_pat.to_string() {
367367
return true;
368368
}
369369
}
@@ -419,7 +419,7 @@ fn should_hide_param_name_hint(
419419
}
420420

421421
let fn_name = match callable.kind() {
422-
hir::CallableKind::Function(it) => Some(it.name(sema.db).to_string()),
422+
hir::CallableKind::Function(it) => Some(it.name(sema.db).to_smol_str()),
423423
_ => None,
424424
};
425425
let fn_name = fn_name.as_deref();
@@ -475,7 +475,9 @@ fn is_enum_name_similar_to_param_name(
475475
param_name: &str,
476476
) -> bool {
477477
match sema.type_of_expr(argument).and_then(|t| t.original.as_adt()) {
478-
Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
478+
Some(hir::Adt::Enum(e)) => {
479+
to_lower_snake_case(&e.name(sema.db).to_smol_str()) == param_name
480+
}
479481
_ => false,
480482
}
481483
}

crates/ide/src/rename.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn find_definitions(
159159
// if the name differs from the definitions name it has to be an alias
160160
if def
161161
.name(sema.db)
162-
.map_or(false, |it| it.to_string() != name_ref.text())
162+
.map_or(false, |it| it.to_smol_str() != name_ref.text().as_str())
163163
{
164164
Err(format_err!("Renaming aliases is currently unsupported"))
165165
} else {

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub(crate) fn compute_fuzzy_completion_order_key(
209209
) -> usize {
210210
cov_mark::hit!(certain_fuzzy_order_test);
211211
let import_name = match proposed_mod_path.segments().last() {
212-
Some(name) => name.to_string().to_lowercase(),
212+
Some(name) => name.to_smol_str().to_lowercase(),
213213
None => return usize::MAX,
214214
};
215215
match import_name.match_indices(user_input_lowercased).next() {

crates/ide_completion/src/completions/lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
3434

3535
ctx.scope.process_all_names(&mut |name, res| {
3636
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
37-
if param_lifetime != Some(&*name.to_string()) {
37+
if param_lifetime != Some(&*name.to_smol_str()) {
3838
acc.add_resolution(ctx, name, &res);
3939
}
4040
}

crates/ide_completion/src/completions/mod_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn directory_to_look_for_submodules(
121121
module_chain_to_containing_module_file(module, db)
122122
.into_iter()
123123
.filter_map(|module| module.name(db))
124-
.try_fold(base_directory, |path, name| path.join(&name.to_string()))
124+
.try_fold(base_directory, |path, name| path.join(&name.to_smol_str()))
125125
}
126126

127127
fn module_chain_to_containing_module_file(

crates/ide_db/src/defs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl NameRefClass {
404404
hir::AssocItem::TypeAlias(it) => Some(*it),
405405
_ => None,
406406
})
407-
.find(|alias| alias.name(sema.db).to_string() == name_ref.text())
407+
.find(|alias| alias.name(sema.db).to_smol_str() == name_ref.text().as_str())
408408
{
409409
return Some(NameRefClass::Definition(Definition::ModuleDef(
410410
ModuleDef::TypeAlias(ty),

crates/ide_db/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
113113
segments.extend(
114114
path.segments()
115115
.iter()
116-
.map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
116+
.map(|segment| make::path_segment(make::name_ref(&segment.to_smol_str()))),
117117
);
118118
make::path_from_segments(segments, is_abs)
119119
}

crates/ide_db/src/helpers/famous_defs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl FamousDefs<'_, '_> {
139139
let krate = self.1?;
140140
let db = self.0.db;
141141
let res =
142-
krate.dependencies(db).into_iter().find(|dep| dep.name.to_string() == name)?.krate;
142+
krate.dependencies(db).into_iter().find(|dep| dep.name.to_smol_str() == name)?.krate;
143143
Some(res)
144144
}
145145

@@ -153,15 +153,15 @@ impl FamousDefs<'_, '_> {
153153
for segment in path {
154154
module = module.children(db).find_map(|child| {
155155
let name = child.name(db)?;
156-
if name.to_string() == segment {
156+
if name.to_smol_str() == segment {
157157
Some(child)
158158
} else {
159159
None
160160
}
161161
})?;
162162
}
163163
let def =
164-
module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
164+
module.scope(db, None).into_iter().find(|(name, _def)| name.to_smol_str() == trait_)?.1;
165165
Some(def)
166166
}
167167
}

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ fn find_import_for_segment(
410410
unresolved_first_segment: &str,
411411
) -> Option<ItemInNs> {
412412
let segment_is_name = item_name(db, original_item)
413-
.map(|name| name.to_string() == unresolved_first_segment)
413+
.map(|name| name.to_smol_str() == unresolved_first_segment)
414414
.unwrap_or(false);
415415

416416
Some(if segment_is_name {
@@ -434,7 +434,7 @@ fn module_with_segment_name(
434434
};
435435
while let Some(module) = current_module {
436436
if let Some(module_name) = module.name(db) {
437-
if module_name.to_string() == segment_name {
437+
if module_name.to_smol_str() == segment_name {
438438
return Some(module);
439439
}
440440
}

crates/ide_db/src/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl<'a> FindUsages<'a> {
385385
})
386386
});
387387
let name = match name {
388-
Some(name) => name.to_string(),
388+
Some(name) => name.to_smol_str(),
389389
None => return,
390390
};
391391
let name = name.as_str();

crates/ide_db/src/ty_filter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl TryEnum {
2626
_ => return None,
2727
};
2828
TryEnum::ALL.iter().find_map(|&var| {
29-
if enum_.name(sema.db).to_string() == var.type_name() {
29+
if enum_.name(sema.db).to_smol_str() == var.type_name() {
3030
return Some(var);
3131
}
3232
None

crates/ide_diagnostics/src/handlers/missing_fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
7676
Some(make::ext::expr_todo())
7777
};
7878
let field =
79-
make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_string()), field_expr)
79+
make::record_expr_field(make::name_ref(&f.name(ctx.sema.db).to_smol_str()), field_expr)
8080
.clone_for_update();
8181
new_field_list.add_field(field);
8282
}

crates/ide_ssr/src/resolving.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'db> ResolutionScope<'db> {
226226
None,
227227
|_ty, assoc_item| {
228228
let item_name = assoc_item.name(self.scope.db)?;
229-
if item_name.to_string().as_str() == name.text() {
229+
if item_name.to_smol_str().as_str() == name.text() {
230230
Some(hir::PathResolution::AssocItem(assoc_item))
231231
} else {
232232
None

0 commit comments

Comments
 (0)