Skip to content

Commit 1947949

Browse files
committed
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); }; } ```
1 parent c1cd1da commit 1947949

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::iter::once;
22

33
use hir::Semantics;
4+
use either::Either;
45
use ide_db::{RootDatabase, ty_filter::TryEnum};
56
use syntax::{
67
AstNode,
@@ -42,12 +43,9 @@ use crate::{
4243
// }
4344
// ```
4445
pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
45-
if let Some(let_stmt) = ctx.find_node_at_offset() {
46-
let_stmt_to_guarded_return(let_stmt, acc, ctx)
47-
} else if let Some(if_expr) = ctx.find_node_at_offset() {
48-
if_expr_to_guarded_return(if_expr, acc, ctx)
49-
} else {
50-
None
46+
match ctx.find_node_at_offset::<Either<ast::LetStmt, ast::IfExpr>>()? {
47+
Either::Left(let_stmt) => let_stmt_to_guarded_return(let_stmt, acc, ctx),
48+
Either::Right(if_expr) => if_expr_to_guarded_return(if_expr, acc, ctx),
5149
}
5250
}
5351

@@ -379,6 +377,30 @@ fn main() {
379377
);
380378
}
381379

380+
#[test]
381+
fn convert_if_let_result_inside_let() {
382+
check_assist(
383+
convert_to_guarded_return,
384+
r#"
385+
fn main() {
386+
let _x = loop {
387+
if$0 let Ok(x) = Err(92) {
388+
foo(x);
389+
}
390+
};
391+
}
392+
"#,
393+
r#"
394+
fn main() {
395+
let _x = loop {
396+
let Ok(x) = Err(92) else { continue };
397+
foo(x);
398+
};
399+
}
400+
"#,
401+
);
402+
}
403+
382404
#[test]
383405
fn convert_if_let_chain_result() {
384406
check_assist(

0 commit comments

Comments
 (0)