diff options
-rw-r--r-- | crates/ra_assists/src/fill_match_arms.rs | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index a0e0f110f..71b101b40 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use std::fmt::Write; | 1 | use std::fmt::Write; |
2 | use itertools::Itertools; | ||
2 | 3 | ||
3 | use hir::{ | 4 | use hir::{ |
4 | AdtDef, FieldSource, HasSource, | 5 | AdtDef, FieldSource, HasSource, |
@@ -9,19 +10,14 @@ use ra_syntax::ast::{self, AstNode}; | |||
9 | use crate::{AssistCtx, Assist, AssistId}; | 10 | use crate::{AssistCtx, Assist, AssistId}; |
10 | 11 | ||
11 | fn is_trivial_arm(arm: &ast::MatchArm) -> bool { | 12 | fn is_trivial_arm(arm: &ast::MatchArm) -> bool { |
12 | for (i, p) in arm.pats().enumerate() { | 13 | fn single_pattern(arm: &ast::MatchArm) -> Option<ast::PatKind> { |
13 | if i > 0 { | 14 | let (pat,) = arm.pats().collect_tuple()?; |
14 | return false; | 15 | Some(pat.kind()) |
15 | } | 16 | } |
16 | 17 | match single_pattern(arm) { | |
17 | match p.kind() { | 18 | Some(ast::PatKind::PlaceholderPat(..)) => true, |
18 | ast::PatKind::PlaceholderPat(_) => {} | 19 | _ => false, |
19 | _ => { | ||
20 | return false; | ||
21 | } | ||
22 | }; | ||
23 | } | 20 | } |
24 | return true; | ||
25 | } | 21 | } |
26 | 22 | ||
27 | pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 23 | pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
@@ -32,12 +28,19 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
32 | // by match postfix complete. Trivial match arm is the catch all arm. | 28 | // by match postfix complete. Trivial match arm is the catch all arm. |
33 | match match_expr.match_arm_list() { | 29 | match match_expr.match_arm_list() { |
34 | Some(arm_list) => { | 30 | Some(arm_list) => { |
35 | for (i, a) in arm_list.arms().enumerate() { | 31 | let mut arm_iter = arm_list.arms(); |
36 | if i > 0 { | 32 | let first = arm_iter.next(); |
37 | return None; | 33 | |
34 | match first { | ||
35 | // If there arm list is empty or there is only one trivial arm, then proceed. | ||
36 | Some(arm) if is_trivial_arm(arm) => { | ||
37 | if arm_iter.next() != None { | ||
38 | return None; | ||
39 | } | ||
38 | } | 40 | } |
41 | None => {} | ||
39 | 42 | ||
40 | if !is_trivial_arm(a) { | 43 | _ => { |
41 | return None; | 44 | return None; |
42 | } | 45 | } |
43 | } | 46 | } |