diff options
| author | A4-Tacks <wdsjxhno1001@163.com> | 2025-09-22 14:18:48 +0800 |
|---|---|---|
| committer | A4-Tacks <wdsjxhno1001@163.com> | 2025-09-22 14:32:46 +0800 |
| commit | dc805bf49844d7bde1013f817c4368c1c3c485aa (patch) | |
| tree | 2ea6783ac206ba1162cbfa12961cf70086426bd2 | |
| parent | 45dda35957ed322c3bccd094dcfa9b925336cc8d (diff) | |
| download | rust-dc805bf49844d7bde1013f817c4368c1c3c485aa.tar.gz rust-dc805bf49844d7bde1013f817c4368c1c3c485aa.zip | |
Fix apply in internal if for pull_assignment_up
Example
---
```rust
fn foo() {
let mut a = 1;
if true {
a = 2;
} else if true {
$0a = 3;
} else {
a = 4;
}
}
```
**Before this PR**:
```rust
fn foo() {
let mut a = 1;
if true {
a = 2;
} else a = if true {
3
} else {
4
};
}
```
**After this PR**:
```rust
fn foo() {
let mut a = 1;
a = if true {
2
} else if true {
3
} else {
4
};
}
```
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/pull_assignment_up.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/pull_assignment_up.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/pull_assignment_up.rs index 21debf6745a..00902fafe82 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/pull_assignment_up.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/pull_assignment_up.rs @@ -53,6 +53,10 @@ pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext<'_>) -> }; let tgt: ast::Expr = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() { + 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>() { @@ -238,6 +242,37 @@ fn foo() { } #[test] + fn test_pull_assignment_up_inner_if() { + check_assist( + pull_assignment_up, + r#" +fn foo() { + let mut a = 1; + + if true { + a = 2; + } else if true { + $0a = 3; + } else { + a = 4; + } +}"#, + r#" +fn foo() { + let mut a = 1; + + a = if true { + 2 + } else if true { + 3 + } else { + 4 + }; +}"#, + ); + } + + #[test] fn test_pull_assignment_up_match() { check_assist( pull_assignment_up, |
