1
1
use syntax:: {
2
2
ast:: {
3
3
edit:: AstNodeEdit , make, AstNode , BlockExpr , Condition , ElseBranch , Expr , IfExpr , MatchArm ,
4
+ Pat ,
4
5
} ,
5
6
SyntaxKind :: WHITESPACE ,
6
7
} ;
@@ -96,7 +97,6 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
96
97
// fn handle(action: Action) {
97
98
// match action {
98
99
// Action::Move { distance } if distance > 10 => foo(),
99
- // Action::Move { distance } => {}
100
100
// _ => (),
101
101
// }
102
102
// }
@@ -176,9 +176,18 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
176
176
}
177
177
}
178
178
} else {
179
- // There's no else branch. Add a pattern without guard
179
+ // There's no else branch. Add a pattern without guard, unless the following match
180
+ // arm is `_ => ...`
180
181
cov_mark:: hit!( move_guard_ifelse_notail) ;
181
- edit. insert ( then_arm_end, format ! ( "\n {}{} => {{}}" , spaces, match_pat) ) ;
182
+ match match_arm. syntax ( ) . next_sibling ( ) . and_then ( MatchArm :: cast) {
183
+ Some ( next_arm)
184
+ if matches ! ( next_arm. pat( ) , Some ( Pat :: WildcardPat ( _) ) )
185
+ && next_arm. guard ( ) . is_none ( ) =>
186
+ {
187
+ cov_mark:: hit!( move_guard_ifelse_has_wildcard) ;
188
+ }
189
+ _ => edit. insert ( then_arm_end, format ! ( "\n {}{} => {{}}" , spaces, match_pat) ) ,
190
+ }
182
191
}
183
192
} ,
184
193
)
@@ -312,7 +321,6 @@ fn main() {
312
321
fn main() {
313
322
match 92 {
314
323
x if x > 10 => false,
315
- x => {}
316
324
_ => true
317
325
}
318
326
}
@@ -322,6 +330,7 @@ fn main() {
322
330
323
331
#[ test]
324
332
fn move_arm_cond_in_block_to_match_guard_works ( ) {
333
+ cov_mark:: check!( move_guard_ifelse_has_wildcard) ;
325
334
check_assist (
326
335
move_arm_cond_to_match_guard,
327
336
r#"
@@ -340,14 +349,69 @@ fn main() {
340
349
fn main() {
341
350
match 92 {
342
351
x if x > 10 => false,
343
- x => {}
344
352
_ => true
345
353
}
346
354
}
347
355
"# ,
348
356
) ;
349
357
}
350
358
359
+ #[ test]
360
+ fn move_arm_cond_in_block_to_match_guard_no_wildcard_works ( ) {
361
+ cov_mark:: check_count!( move_guard_ifelse_has_wildcard, 0 ) ;
362
+ check_assist (
363
+ move_arm_cond_to_match_guard,
364
+ r#"
365
+ fn main() {
366
+ match 92 {
367
+ x => {
368
+ $0if x > 10 {
369
+ false
370
+ }
371
+ }
372
+ }
373
+ }
374
+ "# ,
375
+ r#"
376
+ fn main() {
377
+ match 92 {
378
+ x if x > 10 => false,
379
+ x => {}
380
+ }
381
+ }
382
+ "# ,
383
+ ) ;
384
+ }
385
+
386
+ #[ test]
387
+ fn move_arm_cond_in_block_to_match_guard_wildcard_guard_works ( ) {
388
+ cov_mark:: check_count!( move_guard_ifelse_has_wildcard, 0 ) ;
389
+ check_assist (
390
+ move_arm_cond_to_match_guard,
391
+ r#"
392
+ fn main() {
393
+ match 92 {
394
+ x => {
395
+ $0if x > 10 {
396
+ false
397
+ }
398
+ }
399
+ _ if x > 10 => true,
400
+ }
401
+ }
402
+ "# ,
403
+ r#"
404
+ fn main() {
405
+ match 92 {
406
+ x if x > 10 => false,
407
+ x => {}
408
+ _ if x > 10 => true,
409
+ }
410
+ }
411
+ "# ,
412
+ ) ;
413
+ }
414
+
351
415
#[ test]
352
416
fn move_arm_cond_in_block_to_match_guard_add_comma_works ( ) {
353
417
check_assist (
@@ -368,7 +432,6 @@ fn main() {
368
432
fn main() {
369
433
match 92 {
370
434
x if x > 10 => false,
371
- x => {}
372
435
_ => true
373
436
}
374
437
}
@@ -407,7 +470,6 @@ fn main() {
407
470
fn main() {
408
471
match 92 {
409
472
x if x > 10 => { }
410
- x => {}
411
473
_ => true
412
474
}
413
475
}
@@ -437,7 +499,6 @@ fn main() {
437
499
92;
438
500
false
439
501
}
440
- x => {}
441
502
_ => true
442
503
}
443
504
}
@@ -469,7 +530,6 @@ fn main() {
469
530
92;
470
531
false
471
532
}
472
- x => {}
473
533
_ => true
474
534
}
475
535
}
0 commit comments