Skip to content

Commit

Permalink
use tail_expr().
Browse files Browse the repository at this point in the history
  • Loading branch information
l1nxy committed Jan 3, 2024
1 parent a3be52c commit 161d305
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/ide-assists/src/handlers/merge_nested_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
return None;
}

let nested_if_to_merge = then_branch.syntax().descendants().find_map(ast::IfExpr::cast)?;
let nested_if_to_merge = then_branch.tail_expr().and_then(|e| match e {
ast::Expr::IfExpr(e) => Some(e),
_ => None,
})?;
// should not apply to nested if with else branch.
if nested_if_to_merge.else_branch().is_some() {
return None;
Expand Down Expand Up @@ -232,4 +235,12 @@ mod tests {
"fn f() { i$0f x == 0 { if y == 3 { foo(); } foo(); } }",
)
}

#[test]
fn merge_nested_if_do_not_apply_with_multiply_nested_if() {
check_assist_not_applicable(
merge_nested_if,
"fn f() { i$0f x == 0 { if y == 3 { foo(); } if z == 3 { 2 } } }",
)
}
}

0 comments on commit 161d305

Please sign in to comment.