Fix not applicable for if-expr in let-stmt

Example
---
```rust
fn main() {
    let _x = loop {
        if$0 let Ok(x) = Err(92) {
            foo(x);
        }
    };
}
```

**Before**:

Assist not applicable

**After**:

```rust
fn main() {
    let _x = loop {
        let Ok(x) = Err(92) else { continue };
        foo(x);
    };
}
```
This commit is contained in:
A4-Tacks 2025-09-25 21:57:10 +08:00
parent a98da9f795
commit 4e3b6b38dd
No known key found for this signature in database
GPG key ID: DBD861323040663B

View file

@ -1,6 +1,7 @@
use std::iter::once;
use hir::Semantics;
use either::Either;
use ide_db::{RootDatabase, ty_filter::TryEnum};
use syntax::{
AstNode,
@ -42,12 +43,9 @@ use crate::{
// }
// ```
pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
if let Some(let_stmt) = ctx.find_node_at_offset() {
let_stmt_to_guarded_return(let_stmt, acc, ctx)
} else if let Some(if_expr) = ctx.find_node_at_offset() {
if_expr_to_guarded_return(if_expr, acc, ctx)
} else {
None
match ctx.find_node_at_offset::<Either<ast::LetStmt, ast::IfExpr>>()? {
Either::Left(let_stmt) => let_stmt_to_guarded_return(let_stmt, acc, ctx),
Either::Right(if_expr) => if_expr_to_guarded_return(if_expr, acc, ctx),
}
}
@ -379,6 +377,30 @@ fn main() {
);
}
#[test]
fn convert_if_let_result_inside_let() {
check_assist(
convert_to_guarded_return,
r#"
fn main() {
let _x = loop {
if$0 let Ok(x) = Err(92) {
foo(x);
}
};
}
"#,
r#"
fn main() {
let _x = loop {
let Ok(x) = Err(92) else { continue };
foo(x);
};
}
"#,
);
}
#[test]
fn convert_if_let_chain_result() {
check_assist(