@@ -158,75 +158,51 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
158
158
}
159
159
}
160
160
}
161
- match tail {
162
- Some ( Tail :: IfLet ( e) ) => {
163
- cov_mark:: hit!( move_guard_ifelse_iflet_tail) ;
164
- let guard = format ! ( "\n {}{} => " , spaces, match_pat) ;
165
- // Put the if-let expression in a block
166
- let iflet_expr: Expr = e. reset_indent ( ) . indent ( 1 . into ( ) ) . into ( ) ;
167
- let iflet_block =
168
- make:: block_expr ( std:: iter:: empty ( ) , Some ( iflet_expr) ) . indent ( indent_level) ;
169
- edit. insert ( then_arm_end, guard) ;
170
- edit. insert ( then_arm_end, iflet_block. syntax ( ) . text ( ) ) ;
171
- }
172
- Some ( Tail :: Else ( e) ) => {
173
- cov_mark:: hit!( move_guard_ifelse_else_tail) ;
174
- let guard = format ! ( "\n {}{} => " , spaces, match_pat) ;
175
- edit. insert ( then_arm_end, guard) ;
176
- let only_expr = e. statements ( ) . next ( ) . is_none ( ) ;
177
- match & e. tail_expr ( ) {
178
- Some ( expr) if only_expr => {
179
- cov_mark:: hit!( move_guard_ifelse_expr_only) ;
180
- edit. insert ( then_arm_end, expr. syntax ( ) . text ( ) ) ;
181
- edit. insert ( then_arm_end, "," ) ;
182
- }
183
- _ => {
184
- let to_insert = e. dedent ( dedent. into ( ) ) . syntax ( ) . text ( ) ;
185
- edit. insert ( then_arm_end, to_insert)
186
- }
161
+ if let Some ( e) = tail {
162
+ cov_mark:: hit!( move_guard_ifelse_else_tail) ;
163
+ let guard = format ! ( "\n {}{} => " , spaces, match_pat) ;
164
+ edit. insert ( then_arm_end, guard) ;
165
+ let only_expr = e. statements ( ) . next ( ) . is_none ( ) ;
166
+ match & e. tail_expr ( ) {
167
+ Some ( expr) if only_expr => {
168
+ cov_mark:: hit!( move_guard_ifelse_expr_only) ;
169
+ edit. insert ( then_arm_end, expr. syntax ( ) . text ( ) ) ;
170
+ edit. insert ( then_arm_end, "," ) ;
171
+ }
172
+ _ => {
173
+ let to_insert = e. dedent ( dedent. into ( ) ) . syntax ( ) . text ( ) ;
174
+ edit. insert ( then_arm_end, to_insert)
187
175
}
188
176
}
189
- _ => {
190
- cov_mark:: hit!( move_guard_ifelse_notail) ;
191
- }
177
+ } else {
178
+ cov_mark:: hit!( move_guard_ifelse_notail) ;
192
179
}
193
180
} ,
194
181
)
195
182
}
196
183
197
- #[ derive( Debug ) ]
198
- enum Tail {
199
- Else ( BlockExpr ) ,
200
- IfLet ( IfExpr ) ,
201
- }
202
-
203
184
// Parses an if-else-if chain to get the conditons and the then branches until we encounter an else
204
185
// branch, an if-let branch or the end.
205
- fn parse_if_chain ( if_expr : IfExpr ) -> Option < ( Vec < ( Condition , BlockExpr ) > , Option < Tail > ) > {
186
+ fn parse_if_chain ( if_expr : IfExpr ) -> Option < ( Vec < ( Condition , BlockExpr ) > , Option < BlockExpr > ) > {
206
187
let mut conds_blocks = Vec :: new ( ) ;
207
188
let mut curr_if = if_expr;
208
- let mut applicable = false ;
209
- let tail: Option < Tail > = loop {
189
+ let tail = loop {
210
190
let cond = curr_if. condition ( ) ?;
191
+ // Not support moving if let to arm guard
211
192
if cond. is_pattern_cond ( ) {
212
- break Some ( Tail :: IfLet ( curr_if ) ) ;
193
+ return None ;
213
194
}
214
195
conds_blocks. push ( ( cond, curr_if. then_branch ( ) ?) ) ;
215
- applicable = true ;
216
196
match curr_if. else_branch ( ) {
217
197
Some ( ElseBranch :: IfExpr ( e) ) => {
218
198
curr_if = e;
219
199
}
220
200
Some ( ElseBranch :: Block ( b) ) => {
221
- break Some ( Tail :: Else ( b ) ) ;
201
+ break Some ( b ) ;
222
202
}
223
203
None => break None ,
224
204
}
225
205
} ;
226
- if !applicable {
227
- // The first if branch is an if-let branch
228
- return None ;
229
- }
230
206
Some ( ( conds_blocks, tail) )
231
207
}
232
208
@@ -853,8 +829,7 @@ fn main() {
853
829
854
830
#[ test]
855
831
fn move_arm_cond_to_match_guard_elseif_iflet ( ) {
856
- cov_mark:: check!( move_guard_ifelse_iflet_tail) ;
857
- check_assist (
832
+ check_assist_not_applicable (
858
833
move_arm_cond_to_match_guard,
859
834
r#"
860
835
fn main() {
@@ -872,23 +847,6 @@ fn main() {
872
847
},
873
848
}
874
849
}
875
- "# ,
876
- r#"
877
- fn main() {
878
- match 92 {
879
- 3 => 0,
880
- x if x > 10 => 1,
881
- x if x > 5 => 2,
882
- x => {
883
- if let 4 = 4 {
884
- 42;
885
- 3
886
- } else {
887
- 4
888
- }
889
- }
890
- }
891
- }
892
850
"# ,
893
851
)
894
852
}
0 commit comments