]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/lifetime.rs
simplify
[rust.git] / crates / ide_completion / src / completions / lifetime.rs
index 628c1fb9b6cb97dabd9dd8926fe43a591cc191cc..5f6285b849723e7f0ef81f56eb85a44d3ec04187 100644 (file)
@@ -8,19 +8,24 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
     if !ctx.lifetime_allowed {
         return;
     }
+    let lp_string;
     let param_lifetime = match (
         &ctx.lifetime_syntax,
         ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()),
     ) {
         (Some(lt), Some(lp)) if lp == lt.clone() => return,
-        (Some(_), Some(lp)) => Some(lp.to_string()),
+        (Some(_), Some(lp)) => {
+            lp_string = lp.to_string();
+            Some(&lp_string)
+        }
         _ => None,
     };
 
     ctx.scope.process_all_names(&mut |name, res| {
         if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
-            if param_lifetime != Some(name.to_string()) {
-                acc.add_resolution(ctx, name.to_string(), &res);
+            let name = name.to_string();
+            if param_lifetime != Some(&name) {
+                acc.add_resolution(ctx, name, &res);
             }
         }
     });
@@ -68,6 +73,16 @@ fn func<'lifetime>(foo: &'li$0) {}
 "#,
             r#"
 fn func<'lifetime>(foo: &'lifetime) {}
+"#,
+        );
+        cov_mark::check!(completes_if_lifetime_without_idents);
+        check_edit(
+            "'lifetime",
+            r#"
+fn func<'lifetime>(foo: &'$0) {}
+"#,
+            r#"
+fn func<'lifetime>(foo: &'lifetime) {}
 "#,
         );
     }
@@ -191,6 +206,27 @@ fn foo<'footime, 'lifetime: 'a$0>() {}
         );
     }
 
+    #[test]
+    fn check_label_edit() {
+        check_edit(
+            "'label",
+            r#"
+fn foo() {
+    'label: loop {
+        break '$0
+    }
+}
+"#,
+            r#"
+fn foo() {
+    'label: loop {
+        break 'label
+    }
+}
+"#,
+        );
+    }
+
     #[test]
     fn complete_label_in_loop() {
         check(