Merge pull request #20772 from A4-Tacks/fix-nested-pull-assign-up

Fix not applicable match inside if for pull_assignment_up
This commit is contained in:
Shoyu Vanilla (Flint) 2025-10-14 07:58:34 +00:00 committed by GitHub
commit 4794fb9562
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,4 @@
use either::Either;
use syntax::{
AstNode,
algo::find_node_at_range,
@ -52,14 +53,15 @@ pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext<'_>) ->
assignments: Vec::new(),
};
let tgt: ast::Expr = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
let node: Either<ast::IfExpr, ast::MatchExpr> = ctx.find_node_at_offset()?;
let tgt: ast::Expr = if let Either::Left(if_expr) = node {
let if_expr = std::iter::successors(Some(if_expr), |it| {
it.syntax().parent().and_then(ast::IfExpr::cast)
})
.last()?;
collector.collect_if(&if_expr)?;
if_expr.into()
} else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
} else if let Either::Right(match_expr) = node {
collector.collect_match(&match_expr)?;
match_expr.into()
} else {
@ -311,6 +313,33 @@ fn foo() {
);
}
#[test]
fn test_pull_assignment_up_match_in_if_expr() {
check_assist(
pull_assignment_up,
r#"
fn foo() {
let x;
if true {
match true {
true => $0x = 2,
false => x = 3,
}
}
}"#,
r#"
fn foo() {
let x;
if true {
x = match true {
true => 2,
false => 3,
};
}
}"#,
);
}
#[test]
fn test_pull_assignment_up_assignment_expressions() {
check_assist(