Skip to content

Commit a19a324

Browse files
committed
Don't add pattern if there is a catch all afterwards
1 parent be52051 commit a19a324

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

crates/ide_assists/src/handlers/move_guard.rs

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use syntax::{
22
ast::{
33
edit::AstNodeEdit, make, AstNode, BlockExpr, Condition, ElseBranch, Expr, IfExpr, MatchArm,
4+
Pat,
45
},
56
SyntaxKind::WHITESPACE,
67
};
@@ -96,7 +97,6 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
9697
// fn handle(action: Action) {
9798
// match action {
9899
// Action::Move { distance } if distance > 10 => foo(),
99-
// Action::Move { distance } => {}
100100
// _ => (),
101101
// }
102102
// }
@@ -176,9 +176,18 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
176176
}
177177
}
178178
} 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 `_ => ...`
180181
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+
}
182191
}
183192
},
184193
)
@@ -312,7 +321,6 @@ fn main() {
312321
fn main() {
313322
match 92 {
314323
x if x > 10 => false,
315-
x => {}
316324
_ => true
317325
}
318326
}
@@ -322,6 +330,7 @@ fn main() {
322330

323331
#[test]
324332
fn move_arm_cond_in_block_to_match_guard_works() {
333+
cov_mark::check!(move_guard_ifelse_has_wildcard);
325334
check_assist(
326335
move_arm_cond_to_match_guard,
327336
r#"
@@ -340,14 +349,69 @@ fn main() {
340349
fn main() {
341350
match 92 {
342351
x if x > 10 => false,
343-
x => {}
344352
_ => true
345353
}
346354
}
347355
"#,
348356
);
349357
}
350358

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+
351415
#[test]
352416
fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
353417
check_assist(
@@ -368,7 +432,6 @@ fn main() {
368432
fn main() {
369433
match 92 {
370434
x if x > 10 => false,
371-
x => {}
372435
_ => true
373436
}
374437
}
@@ -407,7 +470,6 @@ fn main() {
407470
fn main() {
408471
match 92 {
409472
x if x > 10 => { }
410-
x => {}
411473
_ => true
412474
}
413475
}
@@ -437,7 +499,6 @@ fn main() {
437499
92;
438500
false
439501
}
440-
x => {}
441502
_ => true
442503
}
443504
}
@@ -469,7 +530,6 @@ fn main() {
469530
92;
470531
false
471532
}
472-
x => {}
473533
_ => true
474534
}
475535
}

0 commit comments

Comments
 (0)