Skip to content

Commit 71d06e7

Browse files
authored
Merge pull request #21865 from A4-Tacks/desugar-try-expr-wrap-result-ty
fix: wrap `Result<>` for desugar_try_expr_let_else
2 parents eabb84b + 16e0a5e commit 71d06e7

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
116116
let fill_expr = || crate::utils::expr_fill_default(ctx.config);
117117
let new_let_stmt = make.let_else_stmt(
118118
try_enum.happy_pattern(pat),
119-
let_stmt.ty().map(|ty| make.ty_option(ty).into()),
119+
let_stmt.ty().map(|ty| match try_enum {
120+
TryEnum::Option => make.ty_option(ty).into(),
121+
TryEnum::Result => make.ty_result(ty, make.ty_infer().into()).into(),
122+
}),
120123
expr,
121124
make.block_expr(
122125
iter::once(
@@ -264,6 +267,27 @@ fn test() {
264267
let Some(pat): Option<bool> = Some(true) else {
265268
return None;
266269
};
270+
}
271+
"#,
272+
"Replace try expression with let else",
273+
);
274+
}
275+
276+
#[test]
277+
fn test_desugar_try_expr_result_let_else_with_type() {
278+
check_assist_by_label(
279+
desugar_try_expr,
280+
r#"
281+
//- minicore: try, result
282+
fn test() {
283+
let pat: bool = Ok(true)$0?;
284+
}
285+
"#,
286+
r#"
287+
fn test() {
288+
let Ok(pat): Result<bool, _> = Ok(true) else {
289+
return Err(todo!());
290+
};
267291
}
268292
"#,
269293
"Replace try expression with let else",

0 commit comments

Comments
 (0)