Don't add pattern if there is a catch all afterwards

This commit is contained in:
Wang Ruochen 2022-01-03 09:37:29 -08:00
parent be5205170c
commit a19a32488d
No known key found for this signature in database
GPG Key ID: C6DEA570A3B026FC

View File

@ -1,6 +1,7 @@
use syntax::{ use syntax::{
ast::{ ast::{
edit::AstNodeEdit, make, AstNode, BlockExpr, Condition, ElseBranch, Expr, IfExpr, MatchArm, edit::AstNodeEdit, make, AstNode, BlockExpr, Condition, ElseBranch, Expr, IfExpr, MatchArm,
Pat,
}, },
SyntaxKind::WHITESPACE, SyntaxKind::WHITESPACE,
}; };
@ -96,7 +97,6 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
// fn handle(action: Action) { // fn handle(action: Action) {
// match action { // match action {
// Action::Move { distance } if distance > 10 => foo(), // Action::Move { distance } if distance > 10 => foo(),
// Action::Move { distance } => {}
// _ => (), // _ => (),
// } // }
// } // }
@ -176,9 +176,18 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
} }
} }
} else { } else {
// There's no else branch. Add a pattern without guard // There's no else branch. Add a pattern without guard, unless the following match
// arm is `_ => ...`
cov_mark::hit!(move_guard_ifelse_notail); cov_mark::hit!(move_guard_ifelse_notail);
edit.insert(then_arm_end, format!("\n{}{} => {{}}", spaces, match_pat)); match match_arm.syntax().next_sibling().and_then(MatchArm::cast) {
Some(next_arm)
if matches!(next_arm.pat(), Some(Pat::WildcardPat(_)))
&& next_arm.guard().is_none() =>
{
cov_mark::hit!(move_guard_ifelse_has_wildcard);
}
_ => edit.insert(then_arm_end, format!("\n{}{} => {{}}", spaces, match_pat)),
}
} }
}, },
) )
@ -312,7 +321,6 @@ fn main() {
fn main() { fn main() {
match 92 { match 92 {
x if x > 10 => false, x if x > 10 => false,
x => {}
_ => true _ => true
} }
} }
@ -322,6 +330,7 @@ fn main() {
#[test] #[test]
fn move_arm_cond_in_block_to_match_guard_works() { fn move_arm_cond_in_block_to_match_guard_works() {
cov_mark::check!(move_guard_ifelse_has_wildcard);
check_assist( check_assist(
move_arm_cond_to_match_guard, move_arm_cond_to_match_guard,
r#" r#"
@ -340,7 +349,6 @@ fn main() {
fn main() { fn main() {
match 92 { match 92 {
x if x > 10 => false, x if x > 10 => false,
x => {}
_ => true _ => true
} }
} }
@ -348,6 +356,62 @@ fn main() {
); );
} }
#[test]
fn move_arm_cond_in_block_to_match_guard_no_wildcard_works() {
cov_mark::check_count!(move_guard_ifelse_has_wildcard, 0);
check_assist(
move_arm_cond_to_match_guard,
r#"
fn main() {
match 92 {
x => {
$0if x > 10 {
false
}
}
}
}
"#,
r#"
fn main() {
match 92 {
x if x > 10 => false,
x => {}
}
}
"#,
);
}
#[test]
fn move_arm_cond_in_block_to_match_guard_wildcard_guard_works() {
cov_mark::check_count!(move_guard_ifelse_has_wildcard, 0);
check_assist(
move_arm_cond_to_match_guard,
r#"
fn main() {
match 92 {
x => {
$0if x > 10 {
false
}
}
_ if x > 10 => true,
}
}
"#,
r#"
fn main() {
match 92 {
x if x > 10 => false,
x => {}
_ if x > 10 => true,
}
}
"#,
);
}
#[test] #[test]
fn move_arm_cond_in_block_to_match_guard_add_comma_works() { fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
check_assist( check_assist(
@ -368,7 +432,6 @@ fn main() {
fn main() { fn main() {
match 92 { match 92 {
x if x > 10 => false, x if x > 10 => false,
x => {}
_ => true _ => true
} }
} }
@ -407,7 +470,6 @@ fn main() {
fn main() { fn main() {
match 92 { match 92 {
x if x > 10 => { } x if x > 10 => { }
x => {}
_ => true _ => true
} }
} }
@ -437,7 +499,6 @@ fn main() {
92; 92;
false false
} }
x => {}
_ => true _ => true
} }
} }
@ -469,7 +530,6 @@ fn main() {
92; 92;
false false
} }
x => {}
_ => true _ => true
} }
} }