Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/ide-assists/src/handlers/move_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pub(crate) fn move_arm_cond_to_match_guard(
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())?;
if block_expr.statements().next().is_some() {
cov_mark::hit!(move_guard_non_naked_if);
return None;
}
if let Expr::IfExpr(e) = block_expr.tail_expr()? {
replace_node = Some(block_expr.syntax().clone());
Some(e)
Expand Down Expand Up @@ -238,6 +242,46 @@ fn main() {
"#,
);
}

#[test]
fn move_non_naked_arm_cond_to_guard() {
cov_mark::check!(move_guard_non_naked_if);
check_assist_not_applicable(
move_arm_cond_to_match_guard,
r#"
fn main() {
match 92 {
_ => {
let cond = true;
$0if cond {
foo()
}
},
_ => true
}
}
"#,
);
check_assist_not_applicable(
move_arm_cond_to_match_guard,
r#"
fn main() {
match 92 {
_ => {
let cond = true;
$0if cond {
foo()
} else {
bar()
}
},
_ => true
}
}
"#,
);
}

#[test]
fn move_guard_to_arm_body_target() {
check_assist_target(
Expand Down