From 30a3f668360bcc49193f7c8d96d136c0ff72e82d Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Wed, 1 Oct 2025 12:53:31 +0800 Subject: [PATCH] Fix not applicable match inside if for pull_assignment_up Example --- ```rust fn foo() { let x; if true { match true { true => $0x = 2, false => x = 3, } } } ``` **Before this PR**: Assist not applicable **After this PR**: ```rust fn foo() { let x; if true { x = match true { true => 2, false => 3, }; } } ``` --- .../src/handlers/pull_assignment_up.rs | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/ide-assists/src/handlers/pull_assignment_up.rs b/crates/ide-assists/src/handlers/pull_assignment_up.rs index 00902fafe827..812ebf6c6e28 100644 --- a/crates/ide-assists/src/handlers/pull_assignment_up.rs +++ b/crates/ide-assists/src/handlers/pull_assignment_up.rs @@ -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::() { + let node: Either = 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::() { + } 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(