Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions crates/ide-assists/src/handlers/convert_to_guarded_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ fn let_stmt_to_guarded_return(
return None;
}

let try_enum =
ctx.sema.type_of_expr(&expr).and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))?;
let try_enum = ctx
.sema
.type_of_expr(&expr)
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted().strip_references()))?;

let happy_pattern = try_enum.happy_pattern(pat);
let target = let_stmt.syntax().text_range();
Expand Down Expand Up @@ -241,13 +243,14 @@ fn early_expression(
};
if let Some(fn_) = ast::Fn::cast(parent_container.clone())
&& let Some(fn_def) = sema.to_def(&fn_)
&& let Some(TryEnum::Option) = TryEnum::from_ty(sema, &fn_def.ret_type(sema.db))
&& let Some(TryEnum::Option) =
TryEnum::from_ty(sema, &fn_def.ret_type(sema.db).strip_references())
{
return Some(return_none_expr());
}
if let Some(body) = ast::ClosureExpr::cast(parent_container.clone()).and_then(|it| it.body())
&& let Some(ret_ty) = sema.type_of_expr(&body).map(TypeInfo::original)
&& let Some(TryEnum::Option) = TryEnum::from_ty(sema, &ret_ty)
&& let Some(TryEnum::Option) = TryEnum::from_ty(sema, &ret_ty.strip_references())
{
return Some(return_none_expr());
}
Expand Down Expand Up @@ -902,6 +905,32 @@ fn foo() -> Option<i32> {
None
}

fn main() {
let Some(x) = foo() else { return };
}
"#,
);
}

#[test]
fn convert_let_ref_stmt_inside_fn() {
check_assist(
convert_to_guarded_return,
r#"
//- minicore: option
fn foo() -> &'static Option<i32> {
&None
}

fn main() {
let x$0 = foo();
}
"#,
r#"
fn foo() -> &'static Option<i32> {
&None
}

fn main() {
let Some(x) = foo() else { return };
}
Expand Down
29 changes: 27 additions & 2 deletions crates/ide-assists/src/handlers/replace_if_let_with_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn make_else_arm(
[(Some(pat), _, _)] => match ctx
.sema
.type_of_pat(pat)
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted().strip_references()))
{
Some(it) => {
if does_pat_match_variant(pat, &it.sad_pattern()) {
Expand Down Expand Up @@ -384,7 +384,7 @@ fn binds_name(sema: &hir::Semantics<'_, RootDatabase>, pat: &ast::Pat) -> bool {

fn is_sad_pat(sema: &hir::Semantics<'_, RootDatabase>, pat: &ast::Pat) -> bool {
sema.type_of_pat(pat)
.and_then(|ty| TryEnum::from_ty(sema, &ty.adjusted()))
.and_then(|ty| TryEnum::from_ty(sema, &ty.adjusted().strip_references()))
.is_some_and(|it| does_pat_match_variant(pat, &it.sad_pattern()))
}

Expand Down Expand Up @@ -847,6 +847,31 @@ fn foo(x: Option<i32>) {
);
}

#[test]
fn special_case_option_ref() {
check_assist(
replace_if_let_with_match,
r#"
//- minicore: option
fn foo(x: &Option<i32>) {
$0if let Some(x) = x {
println!("{}", x)
} else {
println!("none")
}
}
"#,
r#"
fn foo(x: &Option<i32>) {
match x {
Some(x) => println!("{}", x),
None => println!("none"),
}
}
"#,
);
}

#[test]
fn special_case_inverted_option() {
check_assist(
Expand Down
25 changes: 24 additions & 1 deletion crates/ide-assists/src/handlers/replace_let_with_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>
original_pat
} else {
let happy_variant = ty
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted()))
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty.adjusted().strip_references()))
.map(|it| it.happy_case());
match happy_variant {
None => original_pat,
Expand Down Expand Up @@ -97,6 +97,29 @@ mod tests {

use super::*;

#[test]
fn replace_let_try_enum_ref() {
check_assist(
replace_let_with_if_let,
r"
//- minicore: option
fn main(action: Action) {
$0let x = compute();
}

fn compute() -> &'static Option<i32> { &None }
",
r"
fn main(action: Action) {
if let Some(x) = compute() {
}
}

fn compute() -> &'static Option<i32> { &None }
",
)
}

#[test]
fn replace_let_unknown_enum() {
check_assist(
Expand Down