aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorChangyu Li <[email protected]>2019-06-25 21:14:54 +0100
committerChangyu Li <[email protected]>2019-06-25 21:26:12 +0100
commit3a2a13756f325b1dd8ddd54a0a0b24b99180fb82 (patch)
treed4fd5b96086c5594abce5a7a1040395849c5b013 /crates/ra_assists
parent888157b52eace7a4dbf4138ad48926a78ed1d0e1 (diff)
Review 1
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/fill_match_arms.rs35
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 @@
1use std::fmt::Write; 1use std::fmt::Write;
2use itertools::Itertools;
2 3
3use hir::{ 4use hir::{
4 AdtDef, FieldSource, HasSource, 5 AdtDef, FieldSource, HasSource,
@@ -9,19 +10,14 @@ use ra_syntax::ast::{self, AstNode};
9use crate::{AssistCtx, Assist, AssistId}; 10use crate::{AssistCtx, Assist, AssistId};
10 11
11fn is_trivial_arm(arm: &ast::MatchArm) -> bool { 12fn 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
27pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 23pub(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 }