Skip to content

Commit d6e14cc

Browse files
authored
Merge pull request #20722 from A4-Tacks/pull-assign-up-inner-if
Fix apply in inner if for pull_assignment_up
2 parents b465ae0 + 23535a6 commit d6e14cc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

crates/ide-assists/src/handlers/pull_assignment_up.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5353
};
5454

5555
let tgt: ast::Expr = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
56+
let if_expr = std::iter::successors(Some(if_expr), |it| {
57+
it.syntax().parent().and_then(ast::IfExpr::cast)
58+
})
59+
.last()?;
5660
collector.collect_if(&if_expr)?;
5761
if_expr.into()
5862
} else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
@@ -237,6 +241,37 @@ fn foo() {
237241
);
238242
}
239243

244+
#[test]
245+
fn test_pull_assignment_up_inner_if() {
246+
check_assist(
247+
pull_assignment_up,
248+
r#"
249+
fn foo() {
250+
let mut a = 1;
251+
252+
if true {
253+
a = 2;
254+
} else if true {
255+
$0a = 3;
256+
} else {
257+
a = 4;
258+
}
259+
}"#,
260+
r#"
261+
fn foo() {
262+
let mut a = 1;
263+
264+
a = if true {
265+
2
266+
} else if true {
267+
3
268+
} else {
269+
4
270+
};
271+
}"#,
272+
);
273+
}
274+
240275
#[test]
241276
fn test_pull_assignment_up_match() {
242277
check_assist(

0 commit comments

Comments
 (0)