use syntax::{
ast::{
edit::AstNodeEdit, make, AstNode, BlockExpr, Condition, ElseBranch, Expr, IfExpr, MatchArm,
+ Pat,
},
SyntaxKind::WHITESPACE,
};
// fn handle(action: Action) {
// match action {
// Action::Move { distance } if distance > 10 => foo(),
-// Action::Move { distance } => {}
// _ => (),
// }
// }
}
}
} 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);
- 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)),
+ }
}
},
)
fn main() {
match 92 {
x if x > 10 => false,
- x => {}
_ => true
}
}
#[test]
fn move_arm_cond_in_block_to_match_guard_works() {
+ cov_mark::check!(move_guard_ifelse_has_wildcard);
check_assist(
move_arm_cond_to_match_guard,
r#"
fn main() {
match 92 {
x if x > 10 => false,
- x => {}
_ => true
}
}
);
}
+ #[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]
fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
check_assist(
fn main() {
match 92 {
x if x > 10 => false,
- x => {}
_ => true
}
}
fn main() {
match 92 {
x if x > 10 => { }
- x => {}
_ => true
}
}
92;
false
}
- x => {}
_ => true
}
}
92;
false
}
- x => {}
_ => true
}
}