Skip to content

Commit 4794fb9

Browse files
authored
Merge pull request rust-lang#20772 from A4-Tacks/fix-nested-pull-assign-up
Fix not applicable match inside if for pull_assignment_up
2 parents 0ebc92a + 3f0ae50 commit 4794fb9

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/pull_assignment_up.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use either::Either;
12
use syntax::{
23
AstNode,
34
algo::find_node_at_range,
@@ -52,14 +53,15 @@ pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5253
assignments: Vec::new(),
5354
};
5455

55-
let tgt: ast::Expr = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
56+
let node: Either<ast::IfExpr, ast::MatchExpr> = ctx.find_node_at_offset()?;
57+
let tgt: ast::Expr = if let Either::Left(if_expr) = node {
5658
let if_expr = std::iter::successors(Some(if_expr), |it| {
5759
it.syntax().parent().and_then(ast::IfExpr::cast)
5860
})
5961
.last()?;
6062
collector.collect_if(&if_expr)?;
6163
if_expr.into()
62-
} else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
64+
} else if let Either::Right(match_expr) = node {
6365
collector.collect_match(&match_expr)?;
6466
match_expr.into()
6567
} else {
@@ -311,6 +313,33 @@ fn foo() {
311313
);
312314
}
313315

316+
#[test]
317+
fn test_pull_assignment_up_match_in_if_expr() {
318+
check_assist(
319+
pull_assignment_up,
320+
r#"
321+
fn foo() {
322+
let x;
323+
if true {
324+
match true {
325+
true => $0x = 2,
326+
false => x = 3,
327+
}
328+
}
329+
}"#,
330+
r#"
331+
fn foo() {
332+
let x;
333+
if true {
334+
x = match true {
335+
true => 2,
336+
false => 3,
337+
};
338+
}
339+
}"#,
340+
);
341+
}
342+
314343
#[test]
315344
fn test_pull_assignment_up_assignment_expressions() {
316345
check_assist(

0 commit comments

Comments
 (0)