Hoist out make::name_ref
`clone_for_update` is relatively cheap in comparison, since making a node require parsing an entire source text Adds a test to make sure that it doesn't crash when multiple uses are present.
This commit is contained in:
parent
99abcdc1eb
commit
f9a144f0ed
1 changed files with 24 additions and 1 deletions
|
@ -76,10 +76,12 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
let name = to_upper_snake_case(&name.to_string());
|
||||
let usages = Definition::Local(local).usages(&ctx.sema).all();
|
||||
if let Some(usages) = usages.references.get(&ctx.file_id()) {
|
||||
let name = make::name_ref(&name);
|
||||
|
||||
for usage in usages {
|
||||
let Some(usage) = usage.name.as_name_ref().cloned() else { continue };
|
||||
let usage = edit.make_mut(usage);
|
||||
ted::replace(usage.syntax(), make::name_ref(&name).clone_for_update().syntax());
|
||||
ted::replace(usage.syntax(), name.clone_for_update().syntax());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,6 +158,27 @@ fn foo() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_uses() {
|
||||
check_assist(
|
||||
promote_local_to_const,
|
||||
r"
|
||||
fn foo() {
|
||||
let x$0 = 0;
|
||||
let y = x;
|
||||
let z = (x, x, x, x);
|
||||
}
|
||||
",
|
||||
r"
|
||||
fn foo() {
|
||||
const $0X: i32 = 0;
|
||||
let y = X;
|
||||
let z = (X, X, X, X);
|
||||
}
|
||||
",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_applicable_non_const_meth_call() {
|
||||
cov_mark::check!(promote_local_non_const);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue