1
1
use std:: fmt:: Write ;
2
+ use itertools:: Itertools ;
2
3
3
4
use hir:: {
4
5
AdtDef , FieldSource , HasSource ,
@@ -9,19 +10,14 @@ use ra_syntax::ast::{self, AstNode};
9
10
use crate :: { AssistCtx , Assist , AssistId } ;
10
11
11
12
fn is_trivial_arm ( arm : & ast:: MatchArm ) -> bool {
12
- for ( i, p) in arm. pats ( ) . enumerate ( ) {
13
- if i > 0 {
14
- return false ;
15
- }
16
-
17
- match p. kind ( ) {
18
- ast:: PatKind :: PlaceholderPat ( _) => { }
19
- _ => {
20
- return false ;
21
- }
22
- } ;
13
+ fn single_pattern ( arm : & ast:: MatchArm ) -> Option < ast:: PatKind > {
14
+ let ( pat, ) = arm. pats ( ) . collect_tuple ( ) ?;
15
+ Some ( pat. kind ( ) )
16
+ }
17
+ match single_pattern ( arm) {
18
+ Some ( ast:: PatKind :: PlaceholderPat ( ..) ) => true ,
19
+ _ => false ,
23
20
}
24
- return true ;
25
21
}
26
22
27
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
28
// by match postfix complete. Trivial match arm is the catch all arm.
33
29
match match_expr. match_arm_list ( ) {
34
30
Some ( arm_list) => {
35
- for ( i, a) in arm_list. arms ( ) . enumerate ( ) {
36
- if i > 0 {
37
- return None ;
31
+ let mut arm_iter = arm_list. arms ( ) ;
32
+ let first = arm_iter. next ( ) ;
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
44
return None ;
42
45
}
43
46
}
0 commit comments