mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-04 09:53:04 +03:00
Merge pull request #21389 from A4-Tacks/assist-try-enum-ref
Fix some TryEnum reference assists
This commit is contained in:
@@ -934,6 +934,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 };
|
||||
}
|
||||
|
||||
@@ -849,6 +849,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(
|
||||
|
||||
@@ -101,6 +101,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(
|
||||
|
||||
@@ -110,7 +110,7 @@ pub(crate) fn complete_postfix(
|
||||
postfix_snippet("call", "function(expr)", &format!("${{1}}({receiver_text})"))
|
||||
.add_to(acc, ctx.db);
|
||||
|
||||
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
|
||||
let try_enum = TryEnum::from_ty(&ctx.sema, receiver_ty);
|
||||
let mut is_in_cond = false;
|
||||
if let Some(parent) = dot_receiver_including_refs.syntax().parent()
|
||||
&& let Some(second_ancestor) = parent.parent()
|
||||
|
||||
@@ -19,8 +19,16 @@ pub enum TryEnum {
|
||||
impl TryEnum {
|
||||
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
|
||||
|
||||
/// Returns `Some(..)` if the provided type is an enum that implements `std::ops::Try`.
|
||||
/// Returns `Some(..)` if the provided `ty.strip_references()` is an enum that implements `std::ops::Try`.
|
||||
pub fn from_ty(sema: &Semantics<'_, RootDatabase>, ty: &hir::Type<'_>) -> Option<TryEnum> {
|
||||
Self::from_ty_without_strip(sema, &ty.strip_references())
|
||||
}
|
||||
|
||||
/// Returns `Some(..)` if the provided type is an enum that implements `std::ops::Try`.
|
||||
pub fn from_ty_without_strip(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
ty: &hir::Type<'_>,
|
||||
) -> Option<TryEnum> {
|
||||
let enum_ = match ty.as_adt() {
|
||||
Some(hir::Adt::Enum(it)) => it,
|
||||
_ => return None,
|
||||
|
||||
Reference in New Issue
Block a user