]> git.lizzy.rs Git - rust.git/commitdiff
calc depth
authork-nasa <htilcs1115@gmail.com>
Wed, 13 Oct 2021 13:02:39 +0000 (22:02 +0900)
committerk-nasa <htilcs1115@gmail.com>
Wed, 13 Oct 2021 13:02:39 +0000 (22:02 +0900)
crates/ide_assists/src/handlers/replace_if_let_with_match.rs
crates/ide_assists/src/utils.rs

index f7b601d144a9d9642b5c300e3d2daeb591c5b781..dcbc277b26aa79db2c83e8b900565f5eee0f2ed6 100644 (file)
@@ -12,7 +12,7 @@
 };
 
 use crate::{
-    utils::{does_pat_match_variant, unwrap_trivial_block},
+    utils::{does_nested_pattern, does_pat_match_variant, unwrap_trivial_block},
     AssistContext, AssistId, AssistKind, Assists,
 };
 
@@ -143,6 +143,8 @@ fn make_else_arm(
             Some((it, pat)) => {
                 if does_pat_match_variant(pat, &it.sad_pattern()) {
                     it.happy_pattern_wildcard()
+                } else if does_nested_pattern(pat) {
+                    make::wildcard_pat().into()
                 } else {
                     it.sad_pattern()
                 }
@@ -596,6 +598,7 @@ fn foo(x: Result<i32, ()>) {
         Ok(Some(_)) => (),
         _ => (),
     }
+}
 "#,
         );
     }
index 3d4ab968fbb8c02a311ac61b58b2ea29ee34e300..64adddb1de609f9a55067a91cffaa717550ad59e 100644 (file)
@@ -285,6 +285,43 @@ pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
     pat_head == var_head
 }
 
+pub(crate) fn does_nested_pattern(pat: &ast::Pat) -> bool {
+    let depth = calc_depth(pat, 0);
+
+    if 1 < depth {
+        return true;
+    }
+    false
+}
+
+fn calc_depth(pat: &ast::Pat, mut depth: usize) -> usize {
+    match pat {
+        ast::Pat::IdentPat(_)
+        | ast::Pat::BoxPat(_)
+        | ast::Pat::RestPat(_)
+        | ast::Pat::LiteralPat(_)
+        | ast::Pat::MacroPat(_)
+        | ast::Pat::OrPat(_)
+        | ast::Pat::ParenPat(_)
+        | ast::Pat::PathPat(_)
+        | ast::Pat::WildcardPat(_)
+        | ast::Pat::RangePat(_)
+        | ast::Pat::RecordPat(_)
+        | ast::Pat::RefPat(_)
+        | ast::Pat::SlicePat(_)
+        | ast::Pat::TuplePat(_)
+        | ast::Pat::ConstBlockPat(_) => 1,
+
+        // TODO implement
+        ast::Pat::TupleStructPat(pat) => {
+            for p in pat.fields() {
+                depth += calc_depth(&p, depth + 1);
+            }
+            depth
+        }
+    }
+}
+
 // Uses a syntax-driven approach to find any impl blocks for the struct that
 // exist within the module/file
 //