Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/desugar_try_expr.rs')
-rw-r--r--crates/ide-assists/src/handlers/desugar_try_expr.rs111
1 files changed, 61 insertions, 50 deletions
diff --git a/crates/ide-assists/src/handlers/desugar_try_expr.rs b/crates/ide-assists/src/handlers/desugar_try_expr.rs
index 4022030159..865dc86221 100644
--- a/crates/ide-assists/src/handlers/desugar_try_expr.rs
+++ b/crates/ide-assists/src/handlers/desugar_try_expr.rs
@@ -1,9 +1,6 @@
use std::iter;
-use ide_db::{
- assists::{AssistId, ExprFillDefaultMode},
- ty_filter::TryEnum,
-};
+use ide_db::{assists::AssistId, ty_filter::TryEnum};
use syntax::{
AstNode, T,
ast::{
@@ -80,16 +77,9 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
)
.into(),
};
- let sad_expr = match try_enum {
- TryEnum::Option => make.expr_return(Some(make.expr_path(make.ident_path("None")))),
- TryEnum::Result => make.expr_return(Some(
- make.expr_call(
- make.expr_path(make.ident_path("Err")),
- make.arg_list(iter::once(make.expr_path(make.ident_path("err")))),
- )
- .into(),
- )),
- };
+ let sad_expr = make.expr_return(Some(sad_expr(try_enum, &make, || {
+ make.expr_path(make.ident_path("err"))
+ })));
let happy_arm = make.match_arm(
try_enum.happy_pattern(make.ident_pat(false, false, make.name("it")).into()),
@@ -123,48 +113,18 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
let mut editor = builder.make_editor(let_stmt.syntax());
let indent_level = IndentLevel::from_node(let_stmt.syntax());
+ let fill_expr = || crate::utils::expr_fill_default(ctx.config);
let new_let_stmt = make.let_else_stmt(
try_enum.happy_pattern(pat),
- let_stmt.ty(),
+ let_stmt.ty().map(|ty| match try_enum {
+ TryEnum::Option => make.ty_option(ty).into(),
+ TryEnum::Result => make.ty_result(ty, make.ty_infer().into()).into(),
+ }),
expr,
make.block_expr(
iter::once(
make.expr_stmt(
- make.expr_return(Some(match try_enum {
- TryEnum::Option => make.expr_path(make.ident_path("None")),
- TryEnum::Result => make
- .expr_call(
- make.expr_path(make.ident_path("Err")),
- make.arg_list(iter::once(
- match ctx.config.expr_fill_default {
- ExprFillDefaultMode::Todo => make
- .expr_macro(
- make.ident_path("todo"),
- make.token_tree(
- syntax::SyntaxKind::L_PAREN,
- [],
- ),
- )
- .into(),
- ExprFillDefaultMode::Underscore => {
- make.expr_underscore().into()
- }
- ExprFillDefaultMode::Default => make
- .expr_macro(
- make.ident_path("todo"),
- make.token_tree(
- syntax::SyntaxKind::L_PAREN,
- [],
- ),
- )
- .into(),
- },
- )),
- )
- .into(),
- }))
- .indent(indent_level + 1)
- .into(),
+ make.expr_return(Some(sad_expr(try_enum, &make, fill_expr))).into(),
)
.into(),
),
@@ -181,6 +141,15 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
Some(())
}
+fn sad_expr(try_enum: TryEnum, make: &SyntaxFactory, err: impl Fn() -> ast::Expr) -> ast::Expr {
+ match try_enum {
+ TryEnum::Option => make.expr_path(make.ident_path("None")),
+ TryEnum::Result => make
+ .expr_call(make.expr_path(make.ident_path("Err")), make.arg_list(iter::once(err())))
+ .into(),
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -282,4 +251,46 @@ fn test() {
"Replace try expression with let else",
);
}
+
+ #[test]
+ fn test_desugar_try_expr_option_let_else_with_type() {
+ check_assist_by_label(
+ desugar_try_expr,
+ r#"
+//- minicore: try, option
+fn test() {
+ let pat: bool = Some(true)$0?;
+}
+ "#,
+ r#"
+fn test() {
+ let Some(pat): Option<bool> = Some(true) else {
+ return None;
+ };
+}
+ "#,
+ "Replace try expression with let else",
+ );
+ }
+
+ #[test]
+ fn test_desugar_try_expr_result_let_else_with_type() {
+ check_assist_by_label(
+ desugar_try_expr,
+ r#"
+//- minicore: try, result
+fn test() {
+ let pat: bool = Ok(true)$0?;
+}
+ "#,
+ r#"
+fn test() {
+ let Ok(pat): Result<bool, _> = Ok(true) else {
+ return Err(todo!());
+ };
+}
+ "#,
+ "Replace try expression with let else",
+ );
+ }
}