Skip to content

Commit f9a144f

Browse files
committed
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.
1 parent 99abcdc commit f9a144f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

crates/ide-assists/src/handlers/promote_local_to_const.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
7676
let name = to_upper_snake_case(&name.to_string());
7777
let usages = Definition::Local(local).usages(&ctx.sema).all();
7878
if let Some(usages) = usages.references.get(&ctx.file_id()) {
79+
let name = make::name_ref(&name);
80+
7981
for usage in usages {
8082
let Some(usage) = usage.name.as_name_ref().cloned() else { continue };
8183
let usage = edit.make_mut(usage);
82-
ted::replace(usage.syntax(), make::name_ref(&name).clone_for_update().syntax());
84+
ted::replace(usage.syntax(), name.clone_for_update().syntax());
8385
}
8486
}
8587

@@ -156,6 +158,27 @@ fn foo() {
156158
);
157159
}
158160

161+
#[test]
162+
fn multiple_uses() {
163+
check_assist(
164+
promote_local_to_const,
165+
r"
166+
fn foo() {
167+
let x$0 = 0;
168+
let y = x;
169+
let z = (x, x, x, x);
170+
}
171+
",
172+
r"
173+
fn foo() {
174+
const $0X: i32 = 0;
175+
let y = X;
176+
let z = (X, X, X, X);
177+
}
178+
",
179+
);
180+
}
181+
159182
#[test]
160183
fn not_applicable_non_const_meth_call() {
161184
cov_mark::check!(promote_local_non_const);

0 commit comments

Comments
 (0)