Skip to content

Commit 46a214b

Browse files
Merge pull request #21389 from A4-Tacks/assist-try-enum-ref
Fix some TryEnum reference assists
2 parents 8f1f66b + bfdb0e7 commit 46a214b

5 files changed

Lines changed: 84 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,32 @@ fn foo() -> Option<i32> {
934934
None
935935
}
936936
937+
fn main() {
938+
let Some(x) = foo() else { return };
939+
}
940+
"#,
941+
);
942+
}
943+
944+
#[test]
945+
fn convert_let_ref_stmt_inside_fn() {
946+
check_assist(
947+
convert_to_guarded_return,
948+
r#"
949+
//- minicore: option
950+
fn foo() -> &'static Option<i32> {
951+
&None
952+
}
953+
954+
fn main() {
955+
let x$0 = foo();
956+
}
957+
"#,
958+
r#"
959+
fn foo() -> &'static Option<i32> {
960+
&None
961+
}
962+
937963
fn main() {
938964
let Some(x) = foo() else { return };
939965
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,31 @@ fn foo(x: Option<i32>) {
849849
);
850850
}
851851

852+
#[test]
853+
fn special_case_option_ref() {
854+
check_assist(
855+
replace_if_let_with_match,
856+
r#"
857+
//- minicore: option
858+
fn foo(x: &Option<i32>) {
859+
$0if let Some(x) = x {
860+
println!("{}", x)
861+
} else {
862+
println!("none")
863+
}
864+
}
865+
"#,
866+
r#"
867+
fn foo(x: &Option<i32>) {
868+
match x {
869+
Some(x) => println!("{}", x),
870+
None => println!("none"),
871+
}
872+
}
873+
"#,
874+
);
875+
}
876+
852877
#[test]
853878
fn special_case_inverted_option() {
854879
check_assist(

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ mod tests {
101101

102102
use super::*;
103103

104+
#[test]
105+
fn replace_let_try_enum_ref() {
106+
check_assist(
107+
replace_let_with_if_let,
108+
r"
109+
//- minicore: option
110+
fn main(action: Action) {
111+
$0let x = compute();
112+
}
113+
114+
fn compute() -> &'static Option<i32> { &None }
115+
",
116+
r"
117+
fn main(action: Action) {
118+
if let Some(x) = compute() {
119+
}
120+
}
121+
122+
fn compute() -> &'static Option<i32> { &None }
123+
",
124+
)
125+
}
126+
104127
#[test]
105128
fn replace_let_unknown_enum() {
106129
check_assist(

crates/ide-completion/src/completions/postfix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub(crate) fn complete_postfix(
110110
postfix_snippet("call", "function(expr)", &format!("${{1}}({receiver_text})"))
111111
.add_to(acc, ctx.db);
112112

113-
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
113+
let try_enum = TryEnum::from_ty(&ctx.sema, receiver_ty);
114114
let mut is_in_cond = false;
115115
if let Some(parent) = dot_receiver_including_refs.syntax().parent()
116116
&& let Some(second_ancestor) = parent.parent()

crates/ide-db/src/ty_filter.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ pub enum TryEnum {
1919
impl TryEnum {
2020
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
2121

22-
/// Returns `Some(..)` if the provided type is an enum that implements `std::ops::Try`.
22+
/// Returns `Some(..)` if the provided `ty.strip_references()` is an enum that implements `std::ops::Try`.
2323
pub fn from_ty(sema: &Semantics<'_, RootDatabase>, ty: &hir::Type<'_>) -> Option<TryEnum> {
24+
Self::from_ty_without_strip(sema, &ty.strip_references())
25+
}
26+
27+
/// Returns `Some(..)` if the provided type is an enum that implements `std::ops::Try`.
28+
pub fn from_ty_without_strip(
29+
sema: &Semantics<'_, RootDatabase>,
30+
ty: &hir::Type<'_>,
31+
) -> Option<TryEnum> {
2432
let enum_ = match ty.as_adt() {
2533
Some(hir::Adt::Enum(it)) => it,
2634
_ => return None,

0 commit comments

Comments
 (0)