2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2022-01-23 16:37:59 -06:00
|
|
|
ast::{edit::AstNodeEdit, make, AstNode, BlockExpr, ElseBranch, Expr, IfExpr, MatchArm, Pat},
|
2021-12-19 11:26:52 -06:00
|
|
|
SyntaxKind::WHITESPACE,
|
2019-05-15 22:54:32 -05:00
|
|
|
};
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2019-10-27 03:26:46 -05:00
|
|
|
// Assist: move_guard_to_arm_body
|
|
|
|
//
|
|
|
|
// Moves match guard into match arm body.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
2021-01-06 14:15:48 -06:00
|
|
|
// Action::Move { distance } $0if distance > 10 => foo(),
|
2019-10-27 03:26:46 -05:00
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
2020-08-13 03:32:03 -05:00
|
|
|
// Action::Move { distance } => if distance > 10 {
|
|
|
|
// foo()
|
|
|
|
// },
|
2019-10-27 03:26:46 -05:00
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let match_arm = ctx.find_node_at_offset::<MatchArm>()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let guard = match_arm.guard()?;
|
2021-09-21 03:34:11 -05:00
|
|
|
if ctx.offset() > guard.syntax().text_range().end() {
|
|
|
|
cov_mark::hit!(move_guard_unapplicable_in_arm_body);
|
|
|
|
return None;
|
|
|
|
}
|
2019-05-15 22:54:32 -05:00
|
|
|
let space_before_guard = guard.syntax().prev_sibling_or_token();
|
|
|
|
|
2022-01-23 16:37:59 -06:00
|
|
|
let guard_condition = guard.condition()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let arm_expr = match_arm.expr()?;
|
2022-01-23 16:37:59 -06:00
|
|
|
let if_expr =
|
|
|
|
make::expr_if(guard_condition, make::block_expr(None, Some(arm_expr.clone())), None)
|
|
|
|
.indent(arm_expr.indent_level());
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = guard.syntax().text_range();
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Move guard to arm body",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
match space_before_guard {
|
|
|
|
Some(element) if element.kind() == WHITESPACE => {
|
|
|
|
edit.delete(element.text_range());
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
edit.delete(guard.syntax().text_range());
|
2020-08-13 03:32:03 -05:00
|
|
|
edit.replace_ast(arm_expr, if_expr);
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
|
|
|
|
2019-10-27 03:26:46 -05:00
|
|
|
// Assist: move_arm_cond_to_match_guard
|
|
|
|
//
|
|
|
|
// Moves if expression from match arm body into a guard.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
2021-01-06 14:15:48 -06:00
|
|
|
// Action::Move { distance } => $0if distance > 10 { foo() },
|
2019-10-27 03:26:46 -05:00
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
|
|
|
// Action::Move { distance } if distance > 10 => foo(),
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
|
2020-02-09 12:57:01 -06:00
|
|
|
let match_pat = match_arm.pat()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let arm_body = match_arm.expr()?;
|
2020-10-24 01:08:21 -05:00
|
|
|
|
|
|
|
let mut replace_node = None;
|
|
|
|
let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
|
|
|
|
let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
|
2021-01-05 06:45:46 -06:00
|
|
|
if let Expr::IfExpr(e) = block_expr.tail_expr()? {
|
2020-10-24 01:08:21 -05:00
|
|
|
replace_node = Some(block_expr.syntax().clone());
|
|
|
|
Some(e)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})?;
|
2022-06-03 10:00:03 -05:00
|
|
|
if ctx.offset() > if_expr.then_branch()?.syntax().text_range().start() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-10-24 01:08:21 -05:00
|
|
|
let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
|
2021-12-21 18:48:02 -06:00
|
|
|
let needs_dedent = replace_node != *if_expr.syntax();
|
2021-12-19 11:26:52 -06:00
|
|
|
let (conds_blocks, tail) = parse_if_chain(if_expr)?;
|
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
|
2020-01-14 11:32:26 -06:00
|
|
|
"Move condition to match guard",
|
2020-10-24 01:08:21 -05:00
|
|
|
replace_node.text_range(),
|
2019-05-15 22:54:32 -05:00
|
|
|
|edit| {
|
2021-12-19 11:26:52 -06:00
|
|
|
edit.delete(match_arm.syntax().text_range());
|
2021-12-21 18:48:02 -06:00
|
|
|
// Dedent if if_expr is in a BlockExpr
|
|
|
|
let dedent = if needs_dedent {
|
|
|
|
cov_mark::hit!(move_guard_ifelse_in_block);
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
cov_mark::hit!(move_guard_ifelse_else_block);
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let then_arm_end = match_arm.syntax().text_range().end();
|
|
|
|
let indent_level = match_arm.indent_level();
|
|
|
|
let spaces = " ".repeat(indent_level.0 as _);
|
|
|
|
|
2021-12-19 11:26:52 -06:00
|
|
|
let mut first = true;
|
|
|
|
for (cond, block) in conds_blocks {
|
|
|
|
if !first {
|
|
|
|
edit.insert(then_arm_end, format!("\n{}", spaces));
|
|
|
|
} else {
|
|
|
|
first = false;
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
2021-12-19 11:26:52 -06:00
|
|
|
let guard = format!("{} if {} => ", match_pat, cond.syntax().text());
|
|
|
|
edit.insert(then_arm_end, guard);
|
|
|
|
let only_expr = block.statements().next().is_none();
|
|
|
|
match &block.tail_expr() {
|
|
|
|
Some(then_expr) if only_expr => {
|
|
|
|
edit.insert(then_arm_end, then_expr.syntax().text());
|
2021-12-14 12:27:44 -06:00
|
|
|
edit.insert(then_arm_end, ",");
|
|
|
|
}
|
2021-12-15 16:44:47 -06:00
|
|
|
_ => {
|
2021-12-19 11:26:52 -06:00
|
|
|
let to_insert = block.dedent(dedent.into()).syntax().text();
|
|
|
|
edit.insert(then_arm_end, to_insert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 18:51:26 -06:00
|
|
|
if let Some(e) = tail {
|
|
|
|
cov_mark::hit!(move_guard_ifelse_else_tail);
|
|
|
|
let guard = format!("\n{}{} => ", spaces, match_pat);
|
|
|
|
edit.insert(then_arm_end, guard);
|
|
|
|
let only_expr = e.statements().next().is_none();
|
|
|
|
match &e.tail_expr() {
|
|
|
|
Some(expr) if only_expr => {
|
|
|
|
cov_mark::hit!(move_guard_ifelse_expr_only);
|
|
|
|
edit.insert(then_arm_end, expr.syntax().text());
|
|
|
|
edit.insert(then_arm_end, ",");
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let to_insert = e.dedent(dedent.into()).syntax().text();
|
|
|
|
edit.insert(then_arm_end, to_insert)
|
2021-12-15 16:44:47 -06:00
|
|
|
}
|
2021-12-14 12:27:44 -06:00
|
|
|
}
|
2021-12-21 18:51:26 -06:00
|
|
|
} else {
|
2022-01-03 11:37:29 -06:00
|
|
|
// There's no else branch. Add a pattern without guard, unless the following match
|
|
|
|
// arm is `_ => ...`
|
2021-12-21 18:51:26 -06:00
|
|
|
cov_mark::hit!(move_guard_ifelse_notail);
|
2022-01-03 11:37:29 -06:00
|
|
|
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)),
|
|
|
|
}
|
2021-12-14 12:27:44 -06:00
|
|
|
}
|
2019-05-15 22:54:32 -05:00
|
|
|
},
|
2019-10-27 09:35:37 -05:00
|
|
|
)
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
|
|
|
|
2022-01-23 16:37:59 -06:00
|
|
|
// Parses an if-else-if chain to get the conditions and the then branches until we encounter an else
|
2021-12-22 16:17:43 -06:00
|
|
|
// branch or the end.
|
2022-01-23 16:37:59 -06:00
|
|
|
fn parse_if_chain(if_expr: IfExpr) -> Option<(Vec<(Expr, BlockExpr)>, Option<BlockExpr>)> {
|
2021-12-19 11:26:52 -06:00
|
|
|
let mut conds_blocks = Vec::new();
|
|
|
|
let mut curr_if = if_expr;
|
2021-12-21 18:51:26 -06:00
|
|
|
let tail = loop {
|
2021-12-19 11:26:52 -06:00
|
|
|
let cond = curr_if.condition()?;
|
|
|
|
conds_blocks.push((cond, curr_if.then_branch()?));
|
|
|
|
match curr_if.else_branch() {
|
|
|
|
Some(ElseBranch::IfExpr(e)) => {
|
|
|
|
curr_if = e;
|
|
|
|
}
|
|
|
|
Some(ElseBranch::Block(b)) => {
|
2021-12-21 18:51:26 -06:00
|
|
|
break Some(b);
|
2021-12-19 11:26:52 -06:00
|
|
|
}
|
|
|
|
None => break None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some((conds_blocks, tail))
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:54:32 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2021-09-21 03:34:11 -05:00
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_range() {
|
|
|
|
cov_mark::check!(move_guard_unapplicable_in_arm_body);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => $0false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2019-05-15 22:54:32 -05:00
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_target() {
|
|
|
|
check_assist_target(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x $0if x > 10 => false,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"if x > 10"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x $0if x > 10 => false,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => if x > 10 {
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-23 20:50:07 -06:00
|
|
|
#[test]
|
|
|
|
fn move_let_guard_to_arm_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x $0if (let 1 = x) => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => if (let 1 = x) {
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:54:32 -05:00
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_works_complex_match() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0x @ 4 | x @ 5 if x > 5 => true,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x @ 4 | x @ 5 => if x > 5 {
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x > 10$0 { false },
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-24 01:08:21 -05:00
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_in_block_to_match_guard_works() {
|
2022-01-03 11:37:29 -06:00
|
|
|
cov_mark::check!(move_guard_ifelse_has_wildcard);
|
2020-10-24 01:08:21 -05:00
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0if x > 10 {
|
2020-10-24 01:08:21 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-03 11:37:29 -06:00
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:26:24 -06:00
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
|
|
|
$0if x > 10 {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:54:32 -05:00
|
|
|
#[test]
|
2022-01-23 20:50:07 -06:00
|
|
|
fn move_arm_cond_to_match_guard_if_let_works() {
|
|
|
|
check_assist(
|
2019-05-15 22:54:32 -05:00
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if let 62 = x $0&& true { false },
|
2022-01-23 20:50:07 -06:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if let 62 = x && true => false,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_empty_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x $0> 10 { },
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-12-19 11:26:52 -06:00
|
|
|
x if x > 10 => { }
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_multiline_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if$0 x > 10 {
|
|
|
|
92;
|
2020-08-13 03:32:03 -05:00
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => {
|
|
|
|
92;
|
|
|
|
false
|
2021-12-19 11:26:52 -06:00
|
|
|
}
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
2020-10-24 01:08:21 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
2022-06-03 10:00:03 -05:00
|
|
|
if x > $010 {
|
|
|
|
92;
|
2020-10-24 01:08:21 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => {
|
|
|
|
92;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 12:27:44 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x > $010 {
|
2021-12-14 12:27:44 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
x => true,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_block_works() {
|
2021-12-15 16:44:47 -06:00
|
|
|
cov_mark::check!(move_guard_ifelse_expr_only);
|
2021-12-14 12:27:44 -06:00
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
2022-06-03 10:00:03 -05:00
|
|
|
if x $0> 10 {
|
2021-12-14 12:27:44 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
x => true,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_else_if_empty_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x > $010 { } else { },
|
2021-12-14 12:27:44 -06:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-12-19 11:26:52 -06:00
|
|
|
x if x > 10 => { }
|
2021-12-14 12:27:44 -06:00
|
|
|
x => { }
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_multiline_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if$0 x > 10 {
|
|
|
|
92;
|
2021-12-14 12:27:44 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => {
|
|
|
|
92;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
x => true,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_multiline_else_works() {
|
2021-12-15 16:44:47 -06:00
|
|
|
cov_mark::check!(move_guard_ifelse_else_block);
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x $0> 10 {
|
2021-12-15 16:44:47 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
42;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
x => {
|
|
|
|
42;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_multiline_else_block_works() {
|
|
|
|
cov_mark::check!(move_guard_ifelse_in_block);
|
2021-12-14 12:27:44 -06:00
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
2022-06-03 10:00:03 -05:00
|
|
|
if x > $010 {
|
2021-12-14 12:27:44 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
42;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
x => {
|
|
|
|
42;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_last_arm_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
|
|
|
x => {
|
2022-06-03 10:00:03 -05:00
|
|
|
if x > $010 {
|
2021-12-14 12:27:44 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
92;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
|
|
|
x if x > 10 => false,
|
|
|
|
x => {
|
|
|
|
92;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_with_else_comma_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x > $010 {
|
2021-12-14 12:27:44 -06:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
92;
|
|
|
|
true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
|
|
|
x if x > 10 => false,
|
|
|
|
x => {
|
|
|
|
92;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 11:26:52 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_elseif() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x $0> 10 {
|
2021-12-19 11:26:52 -06:00
|
|
|
false
|
|
|
|
} else if x > 5 {
|
|
|
|
true
|
|
|
|
} else if x > 4 {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
|
|
|
x if x > 10 => false,
|
|
|
|
x if x > 5 => true,
|
|
|
|
x if x > 4 => false,
|
|
|
|
x => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_elseif_in_block() {
|
|
|
|
cov_mark::check!(move_guard_ifelse_in_block);
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
|
|
|
x => {
|
2022-06-03 10:00:03 -05:00
|
|
|
if x > $010 {
|
2021-12-19 11:26:52 -06:00
|
|
|
false
|
|
|
|
} else if x > 5 {
|
|
|
|
true
|
|
|
|
} else if x > 4 {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => true,
|
|
|
|
x if x > 10 => false,
|
|
|
|
x if x > 5 => true,
|
|
|
|
x if x > 4 => false,
|
|
|
|
x => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_elseif_chain() {
|
|
|
|
cov_mark::check!(move_guard_ifelse_else_tail);
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => 0,
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x $0> 10 {
|
2021-12-19 11:26:52 -06:00
|
|
|
1
|
|
|
|
} else if x > 5 {
|
|
|
|
2
|
|
|
|
} else if x > 3 {
|
|
|
|
42;
|
|
|
|
3
|
|
|
|
} else {
|
|
|
|
4
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => 0,
|
|
|
|
x if x > 10 => 1,
|
|
|
|
x if x > 5 => 2,
|
|
|
|
x if x > 3 => {
|
|
|
|
42;
|
|
|
|
3
|
|
|
|
}
|
|
|
|
x => 4,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_elseif_iflet() {
|
2022-01-23 20:50:07 -06:00
|
|
|
check_assist(
|
2021-12-19 11:26:52 -06:00
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => 0,
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x $0> 10 {
|
2021-12-19 11:26:52 -06:00
|
|
|
1
|
|
|
|
} else if x > 5 {
|
|
|
|
2
|
|
|
|
} else if let 4 = 4 {
|
|
|
|
42;
|
|
|
|
3
|
|
|
|
} else {
|
|
|
|
4
|
|
|
|
},
|
|
|
|
}
|
2022-01-23 20:50:07 -06:00
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => 0,
|
|
|
|
x if x > 10 => 1,
|
|
|
|
x if x > 5 => 2,
|
|
|
|
x if let 4 = 4 => {
|
|
|
|
42;
|
|
|
|
3
|
|
|
|
}
|
|
|
|
x => 4,
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
2021-12-19 11:26:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_elseif_notail() {
|
|
|
|
cov_mark::check!(move_guard_ifelse_notail);
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => 0,
|
2022-06-03 10:00:03 -05:00
|
|
|
x => if x > $010 {
|
2021-12-19 11:26:52 -06:00
|
|
|
1
|
|
|
|
} else if x > 5 {
|
|
|
|
2
|
|
|
|
} else if x > 4 {
|
|
|
|
42;
|
|
|
|
3
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
3 => 0,
|
|
|
|
x if x > 10 => 1,
|
|
|
|
x if x > 5 => 2,
|
|
|
|
x if x > 4 => {
|
|
|
|
42;
|
|
|
|
3
|
|
|
|
}
|
2021-12-22 10:58:37 -06:00
|
|
|
x => {}
|
2021-12-19 11:26:52 -06:00
|
|
|
}
|
|
|
|
}
|
2020-10-24 01:08:21 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|