about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDropDemBits <r3usrlnd@gmail.com>2023-07-11 17:28:17 -0400
committerDropDemBits <r3usrlnd@gmail.com>2023-07-11 17:28:17 -0400
commitf9a144f0ed6d500706e008e9122dabce2c7553b1 (patch)
treebca3086c3443ecb96445fd47a6bf2b43bde98955
parent99abcdc1ebd21077533e7592fc0488f4ce264c52 (diff)
downloadrust-f9a144f0ed6d500706e008e9122dabce2c7553b1.tar.gz
rust-f9a144f0ed6d500706e008e9122dabce2c7553b1.zip
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.
-rw-r--r--crates/ide-assists/src/handlers/promote_local_to_const.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/promote_local_to_const.rs b/crates/ide-assists/src/handlers/promote_local_to_const.rs
index 5fcbe52c607..d26ed1cb8d2 100644
--- a/crates/ide-assists/src/handlers/promote_local_to_const.rs
+++ b/crates/ide-assists/src/handlers/promote_local_to_const.rs
@@ -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());
                 }
             }
 
@@ -157,6 +159,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);
         check_assist_not_applicable(