Fix invalid logic op for replace_let_with_if_let

Example
---
```rust
fn main() {
    $0let x = true && false;
}
```

**Before this PR**

```rust
fn main() {
    if let x = true {
    }
}
```

**After this PR**

```rust
fn main() {
    if let x = (true && false) {
    }
}
```
This commit is contained in:
A4-Tacks 2025-12-14 18:51:52 +08:00
parent b3e6363a47
commit 25376c8498
No known key found for this signature in database
GPG key ID: 9E63F956E66DD9C7

View file

@ -60,11 +60,13 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>
}
}
};
let init_expr =
if let_expr_needs_paren(&init) { make.expr_paren(init).into() } else { init };
let block = make.block_expr([], None);
block.indent(IndentLevel::from_node(let_stmt.syntax()));
let if_expr = make.expr_if(
make.expr_let(pat, init).into(),
make.expr_let(pat, init_expr).into(),
block,
let_stmt
.let_else()
@ -79,6 +81,16 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>
)
}
fn let_expr_needs_paren(expr: &ast::Expr) -> bool {
let fake_expr_let =
ast::make::expr_let(ast::make::tuple_pat(None).into(), ast::make::ext::expr_unit());
let Some(fake_expr) = fake_expr_let.expr() else {
stdx::never!();
return false;
};
expr.needs_parens_in_place_of(fake_expr_let.syntax(), fake_expr.syntax())
}
#[cfg(test)]
mod tests {
use crate::tests::check_assist;
@ -107,6 +119,42 @@ fn main() {
)
}
#[test]
fn replace_let_logic_and() {
check_assist(
replace_let_with_if_let,
r"
fn main() {
$0let x = true && false;
}
",
r"
fn main() {
if let x = (true && false) {
}
}
",
)
}
#[test]
fn replace_let_logic_or() {
check_assist(
replace_let_with_if_let,
r"
fn main() {
$0let x = true || false;
}
",
r"
fn main() {
if let x = (true || false) {
}
}
",
)
}
#[test]
fn replace_let_else() {
check_assist(