about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyan Mehri <ryan.mehri1@gmail.com>2023-10-01 11:32:37 -0700
committerRyan Mehri <ryan.mehri1@gmail.com>2023-10-01 11:48:10 -0700
commit146a7cc490b6e45e8b4192e38cab3bae00216712 (patch)
tree83600dffc917019d4c91e0f8060b021d99fd25f7
parent0840038f02daec6ba3238f05d8caa037d28701a0 (diff)
downloadrust-146a7cc490b6e45e8b4192e38cab3bae00216712.tar.gz
rust-146a7cc490b6e45e8b4192e38cab3bae00216712.zip
fix: allow more kinds of if let patterns in guarded return assist
-rw-r--r--crates/ide-assists/src/handlers/convert_to_guarded_return.rs76
1 files changed, 57 insertions, 19 deletions
diff --git a/crates/ide-assists/src/handlers/convert_to_guarded_return.rs b/crates/ide-assists/src/handlers/convert_to_guarded_return.rs
index 7d0e424769e..73ba3f5c4cd 100644
--- a/crates/ide-assists/src/handlers/convert_to_guarded_return.rs
+++ b/crates/ide-assists/src/handlers/convert_to_guarded_return.rs
@@ -51,22 +51,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'
     // Check if there is an IfLet that we can handle.
     let (if_let_pat, cond_expr) = if is_pattern_cond(cond.clone()) {
         let let_ = single_let(cond)?;
-        match let_.pat() {
-            Some(ast::Pat::TupleStructPat(pat)) if pat.fields().count() == 1 => {
-                let path = pat.path()?;
-                if path.qualifier().is_some() {
-                    return None;
-                }
-
-                let bound_ident = pat.fields().next()?;
-                if !ast::IdentPat::can_cast(bound_ident.syntax().kind()) {
-                    return None;
-                }
-
-                (Some((path, bound_ident)), let_.expr()?)
-            }
-            _ => return None, // Unsupported IfLet.
-        }
+        (Some(let_.pat()?), let_.expr()?)
     } else {
         (None, cond)
     };
@@ -136,11 +121,10 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'
                     };
                     new_expr.syntax().clone_for_update()
                 }
-                Some((path, bound_ident)) => {
+                Some(pat) => {
                     // If-let.
-                    let pat = make::tuple_struct_pat(path, once(bound_ident));
                     let let_else_stmt = make::let_else_stmt(
-                        pat.into(),
+                        pat,
                         None,
                         cond_expr,
                         ast::make::tail_only_block_expr(early_expression),
@@ -443,6 +427,60 @@ fn main() {
     }
 
     #[test]
+    fn convert_arbitrary_if_let_patterns() {
+        check_assist(
+            convert_to_guarded_return,
+            r#"
+fn main() {
+    $0if let None = Some(92) {
+        foo();
+    }
+}
+"#,
+            r#"
+fn main() {
+    let None = Some(92) else { return };
+    foo();
+}
+"#,
+        );
+
+        check_assist(
+            convert_to_guarded_return,
+            r#"
+fn main() {
+    $0if let [1, x] = [1, 92] {
+        foo(x);
+    }
+}
+"#,
+            r#"
+fn main() {
+    let [1, x] = [1, 92] else { return };
+    foo(x);
+}
+"#,
+        );
+
+        check_assist(
+            convert_to_guarded_return,
+            r#"
+fn main() {
+    $0if let (Some(x), None) = (Some(92), None) {
+        foo(x);
+    }
+}
+"#,
+            r#"
+fn main() {
+    let (Some(x), None) = (Some(92), None) else { return };
+    foo(x);
+}
+"#,
+        );
+    }
+
+    #[test]
     fn ignore_already_converted_if() {
         check_assist_not_applicable(
             convert_to_guarded_return,