diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_return_type.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_return_type.rs index b7f873c7798..64d5e2c9b82 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_return_type.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_return_type.rs @@ -22,9 +22,6 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // ``` // fn foo() -> i32 { 42i32 } // ``` -pub(crate) fn unwrap_option_return_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { - unwrap_return_type(acc, ctx, UnwrapperKind::Option) -} // Assist: unwrap_result_return_type // @@ -38,15 +35,8 @@ pub(crate) fn unwrap_option_return_type(acc: &mut Assists, ctx: &AssistContext<' // ``` // fn foo() -> i32 { 42i32 } // ``` -pub(crate) fn unwrap_result_return_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { - unwrap_return_type(acc, ctx, UnwrapperKind::Result) -} -fn unwrap_return_type( - acc: &mut Assists, - ctx: &AssistContext<'_>, - kind: UnwrapperKind, -) -> Option<()> { +pub(crate) fn unwrap_return_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let ret_type = ctx.find_node_at_offset::()?; let parent = ret_type.syntax().parent()?; let body = match_ast! { @@ -65,11 +55,12 @@ fn unwrap_return_type( let Some(hir::Adt::Enum(ret_enum)) = ctx.sema.resolve_type(type_ref)?.as_adt() else { return None; }; - let core_enum = - kind.core_type(FamousDefs(&ctx.sema, ctx.sema.scope(type_ref.syntax())?.krate()))?; - if ret_enum != core_enum { - return None; - } + + let famous_defs = FamousDefs(&ctx.sema, ctx.sema.scope(type_ref.syntax())?.krate()); + + let kind = UnwrapperKind::ALL + .iter() + .find(|k| matches!(k.core_type(&famous_defs), Some(core_type) if ret_enum == core_type))?; let happy_type = extract_wrapped_type(type_ref)?; @@ -149,6 +140,8 @@ enum UnwrapperKind { } impl UnwrapperKind { + const ALL: &'static [UnwrapperKind] = &[UnwrapperKind::Option, UnwrapperKind::Result]; + fn assist_id(&self) -> AssistId { let s = match self { UnwrapperKind::Option => "unwrap_option_return_type", @@ -165,7 +158,7 @@ impl UnwrapperKind { } } - fn core_type(&self, famous_defs: FamousDefs<'_, '_>) -> Option { + fn core_type(&self, famous_defs: &FamousDefs<'_, '_>) -> Option { match self { UnwrapperKind::Option => famous_defs.core_option_Option(), UnwrapperKind::Result => famous_defs.core_result_Result(), @@ -209,14 +202,14 @@ fn is_unit_type(ty: &ast::Type) -> bool { #[cfg(test)] mod tests { - use crate::tests::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist_by_label, check_assist_not_applicable_by_label}; use super::*; #[test] fn unwrap_option_return_type_simple() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -230,13 +223,14 @@ fn foo() -> i32 { return 42i32; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_unit_type() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option<()$0> { @@ -247,11 +241,12 @@ fn foo() -> Option<()$0> { fn foo() { } "#, + "Unwrap Option return type", ); // Unformatted return type - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option<()$0>{ @@ -262,13 +257,14 @@ fn foo() -> Option<()$0>{ fn foo() { } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_none() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -288,13 +284,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_ending_with_parent() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -314,13 +311,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_break_split_tail() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -344,13 +342,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_closure() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() { @@ -368,13 +367,14 @@ fn foo() { }; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_return_type_bad_cursor() { - check_assist_not_applicable( - unwrap_option_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> i32 { @@ -382,13 +382,14 @@ fn foo() -> i32 { return 42i32; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_return_type_bad_cursor_closure() { - check_assist_not_applicable( - unwrap_option_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() { @@ -398,24 +399,26 @@ fn foo() { }; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_closure_non_block() { - check_assist_not_applicable( - unwrap_option_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() { || -> i$032 3; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_return_type_already_not_option_std() { - check_assist_not_applicable( - unwrap_option_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -423,13 +426,14 @@ fn foo() -> i32$0 { return 42i32; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_return_type_already_not_option_closure() { - check_assist_not_applicable( - unwrap_option_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() { @@ -439,13 +443,14 @@ fn foo() { }; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() ->$0 Option { @@ -459,13 +464,14 @@ fn foo() -> i32 { 42i32 } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_closure() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() { @@ -483,13 +489,14 @@ fn foo() { }; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_only() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { Some(42i32) } @@ -497,13 +504,14 @@ fn foo() -> Option { Some(42i32) } r#" fn foo() -> i32 { 42i32 } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_block_like() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option$0 { @@ -523,13 +531,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_without_block_closure() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() { @@ -553,13 +562,14 @@ fn foo() { }; } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_nested_if() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option$0 { @@ -587,13 +597,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_await() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option async fn foo() -> Option { @@ -621,13 +632,14 @@ async fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_array() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option<[i32; 3]$0> { Some([1, 2, 3]) } @@ -635,13 +647,14 @@ fn foo() -> Option<[i32; 3]$0> { Some([1, 2, 3]) } r#" fn foo() -> [i32; 3] { [1, 2, 3] } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_cast() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -$0> Option { @@ -669,13 +682,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_block_like_match() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -695,13 +709,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_loop_with_tail() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -723,13 +738,14 @@ fn foo() -> i32 { my_var } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_loop_in_let_stmt() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -747,13 +763,14 @@ fn foo() -> i32 { my_var } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_block_like_match_return_expr() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option$0 { @@ -775,10 +792,11 @@ fn foo() -> i32 { res } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -802,13 +820,14 @@ fn foo() -> i32 { res } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_block_like_match_deeper() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -852,13 +871,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_tail_block_like_early_return() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -878,13 +898,14 @@ fn foo() -> i32 { 53i32 } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_in_tail_position() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(num: i32) -> $0Option { @@ -896,13 +917,14 @@ fn foo(num: i32) -> i32 { return num } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_closure() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> Option { @@ -932,10 +954,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> Option { @@ -977,13 +1000,14 @@ fn foo(the_field: u32) -> u32 { t.unwrap_or_else(|| the_field) } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_simple_with_weird_forms() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -1015,10 +1039,11 @@ fn foo() -> i32 { } } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> Option { @@ -1056,10 +1081,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> Option { @@ -1085,10 +1111,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> Option { @@ -1116,10 +1143,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> Option { @@ -1147,13 +1175,14 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_option_return_type_nested_type() { - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option, result fn foo() -> Option> { @@ -1165,10 +1194,11 @@ fn foo() -> Result { Ok(42) } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option, result fn foo() -> Option, ()>> { @@ -1180,10 +1210,11 @@ fn foo() -> Result, ()> { Err() } "#, + "Unwrap Option return type", ); - check_assist( - unwrap_option_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: option, result, iterators fn foo() -> Option$0> { @@ -1195,13 +1226,14 @@ fn foo() -> impl Iterator { Some(42).into_iter() } "#, + "Unwrap Option return type", ); } #[test] fn unwrap_result_return_type_simple() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1215,13 +1247,14 @@ fn foo() -> i32 { return 42i32; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_unit_type() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result<(), Box> { @@ -1232,11 +1265,12 @@ fn foo() -> Result<(), Box> { fn foo() { } "#, + "Unwrap Result return type", ); // Unformatted return type - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result<(), Box>{ @@ -1247,13 +1281,14 @@ fn foo() -> Result<(), Box>{ fn foo() { } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_ending_with_parent() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result> { @@ -1273,13 +1308,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_break_split_tail() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1303,13 +1339,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_closure() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() { @@ -1327,13 +1364,14 @@ fn foo() { }; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_return_type_bad_cursor() { - check_assist_not_applicable( - unwrap_result_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> i32 { @@ -1341,13 +1379,14 @@ fn foo() -> i32 { return 42i32; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_return_type_bad_cursor_closure() { - check_assist_not_applicable( - unwrap_result_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() { @@ -1357,24 +1396,26 @@ fn foo() { }; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_closure_non_block() { - check_assist_not_applicable( - unwrap_result_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() { || -> i$032 3; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_return_type_already_not_result_std() { - check_assist_not_applicable( - unwrap_result_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1382,13 +1423,14 @@ fn foo() -> i32$0 { return 42i32; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_return_type_already_not_result_closure() { - check_assist_not_applicable( - unwrap_result_return_type, + check_assist_not_applicable_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() { @@ -1398,13 +1440,14 @@ fn foo() { }; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() ->$0 Result { @@ -1418,13 +1461,14 @@ fn foo() -> i32 { 42i32 } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_closure() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() { @@ -1442,13 +1486,14 @@ fn foo() { }; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_only() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { Ok(42i32) } @@ -1456,13 +1501,14 @@ fn foo() -> Result { Ok(42i32) } r#" fn foo() -> i32 { 42i32 } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_block_like() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result$0 { @@ -1482,13 +1528,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_without_block_closure() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() { @@ -1512,13 +1559,14 @@ fn foo() { }; } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_nested_if() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result$0 { @@ -1546,13 +1594,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_await() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result async fn foo() -> Result { @@ -1580,13 +1629,14 @@ async fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_array() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result<[i32; 3]$0> { Ok([1, 2, 3]) } @@ -1594,13 +1644,14 @@ fn foo() -> Result<[i32; 3]$0> { Ok([1, 2, 3]) } r#" fn foo() -> [i32; 3] { [1, 2, 3] } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_cast() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -$0> Result { @@ -1628,13 +1679,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_block_like_match() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1654,13 +1706,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_loop_with_tail() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1682,13 +1735,14 @@ fn foo() -> i32 { my_var } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_loop_in_let_stmt() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1706,13 +1760,14 @@ fn foo() -> i32 { my_var } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_block_like_match_return_expr() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result$0 { @@ -1734,10 +1789,11 @@ fn foo() -> i32 { res } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1761,13 +1817,14 @@ fn foo() -> i32 { res } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_block_like_match_deeper() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1811,13 +1868,14 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_tail_block_like_early_return() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1837,13 +1895,14 @@ fn foo() -> i32 { 53i32 } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_in_tail_position() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(num: i32) -> $0Result { @@ -1855,13 +1914,14 @@ fn foo(num: i32) -> i32 { return num } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_closure() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> Result { @@ -1891,10 +1951,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> Result { @@ -1936,13 +1997,14 @@ fn foo(the_field: u32) -> u32 { t.unwrap_or_else(|| the_field) } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_simple_with_weird_forms() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1974,10 +2036,11 @@ fn foo() -> i32 { } } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> Result { @@ -2015,10 +2078,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> Result { @@ -2044,10 +2108,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> Result { @@ -2075,10 +2140,11 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> Result { @@ -2106,13 +2172,14 @@ fn foo(the_field: u32) -> u32 { the_field } "#, + "Unwrap Result return type", ); } #[test] fn unwrap_result_return_type_nested_type() { - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result, option fn foo() -> Result, ()> { @@ -2124,10 +2191,11 @@ fn foo() -> Option { Some(42) } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result, option fn foo() -> Result>, ()> { @@ -2139,10 +2207,11 @@ fn foo() -> Option> { None } "#, + "Unwrap Result return type", ); - check_assist( - unwrap_result_return_type, + check_assist_by_label( + unwrap_return_type, r#" //- minicore: result, option, iterators fn foo() -> Result$0, ()> { @@ -2154,6 +2223,7 @@ fn foo() -> impl Iterator { Some(42).into_iter() } "#, + "Unwrap Result return type", ); } } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/wrap_return_type.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/wrap_return_type.rs index e6e1857a3f7..2d918a5b1c1 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/wrap_return_type.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/wrap_return_type.rs @@ -2,6 +2,7 @@ use std::iter; use hir::HasSource; use ide_db::{ + assists::GroupLabel, famous_defs::FamousDefs, syntax_helpers::node_ext::{for_each_tail_expr, walk_expr}, }; @@ -25,9 +26,6 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // ``` // fn foo() -> Option { Some(42i32) } // ``` -pub(crate) fn wrap_return_type_in_option(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { - wrap_return_type(acc, ctx, WrapperKind::Option) -} // Assist: wrap_return_type_in_result // @@ -41,8 +39,97 @@ pub(crate) fn wrap_return_type_in_option(acc: &mut Assists, ctx: &AssistContext< // ``` // fn foo() -> Result { Ok(42i32) } // ``` -pub(crate) fn wrap_return_type_in_result(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { - wrap_return_type(acc, ctx, WrapperKind::Result) + +pub(crate) fn wrap_return_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { + let ret_type = ctx.find_node_at_offset::()?; + let parent = ret_type.syntax().parent()?; + let body = match_ast! { + match parent { + ast::Fn(func) => func.body()?, + ast::ClosureExpr(closure) => match closure.body()? { + Expr::BlockExpr(block) => block, + // closures require a block when a return type is specified + _ => return None, + }, + _ => return None, + } + }; + + let type_ref = &ret_type.ty()?; + let ty = ctx.sema.resolve_type(type_ref)?.as_adt(); + let famous_defs = FamousDefs(&ctx.sema, ctx.sema.scope(type_ref.syntax())?.krate()); + + for kind in WrapperKind::ALL { + let Some(core_wrapper) = kind.core_type(&famous_defs) else { + continue; + }; + + if matches!(ty, Some(hir::Adt::Enum(ret_type)) if ret_type == core_wrapper) { + // The return type is already wrapped + cov_mark::hit!(wrap_return_type_simple_return_type_already_wrapped); + continue; + } + + acc.add_group( + &GroupLabel("Wrap return type in...".into()), + kind.assist_id(), + kind.label(), + type_ref.syntax().text_range(), + |edit| { + let alias = wrapper_alias(ctx, &core_wrapper, type_ref, kind.symbol()); + let new_return_ty = + alias.unwrap_or_else(|| kind.wrap_type(type_ref)).clone_for_update(); + + let body = edit.make_mut(ast::Expr::BlockExpr(body.clone())); + + let mut exprs_to_wrap = Vec::new(); + let tail_cb = &mut |e: &_| tail_cb_impl(&mut exprs_to_wrap, e); + walk_expr(&body, &mut |expr| { + if let Expr::ReturnExpr(ret_expr) = expr { + if let Some(ret_expr_arg) = &ret_expr.expr() { + for_each_tail_expr(ret_expr_arg, tail_cb); + } + } + }); + for_each_tail_expr(&body, tail_cb); + + for ret_expr_arg in exprs_to_wrap { + let happy_wrapped = make::expr_call( + make::expr_path(make::ext::ident_path(kind.happy_ident())), + make::arg_list(iter::once(ret_expr_arg.clone())), + ) + .clone_for_update(); + ted::replace(ret_expr_arg.syntax(), happy_wrapped.syntax()); + } + + let old_return_ty = edit.make_mut(type_ref.clone()); + ted::replace(old_return_ty.syntax(), new_return_ty.syntax()); + + if let WrapperKind::Result = kind { + // Add a placeholder snippet at the first generic argument that doesn't equal the return type. + // This is normally the error type, but that may not be the case when we inserted a type alias. + let args = + new_return_ty.syntax().descendants().find_map(ast::GenericArgList::cast); + let error_type_arg = args.and_then(|list| { + list.generic_args().find(|arg| match arg { + ast::GenericArg::TypeArg(_) => { + arg.syntax().text() != type_ref.syntax().text() + } + ast::GenericArg::LifetimeArg(_) => false, + _ => true, + }) + }); + if let Some(error_type_arg) = error_type_arg { + if let Some(cap) = ctx.config.snippet_cap { + edit.add_placeholder_snippet(cap, error_type_arg); + } + } + } + }, + ); + } + + Some(()) } enum WrapperKind { @@ -51,6 +138,8 @@ enum WrapperKind { } impl WrapperKind { + const ALL: &'static [WrapperKind] = &[WrapperKind::Option, WrapperKind::Result]; + fn assist_id(&self) -> AssistId { let s = match self { WrapperKind::Option => "wrap_return_type_in_option", @@ -74,7 +163,7 @@ impl WrapperKind { } } - fn core_type(&self, famous_defs: FamousDefs<'_, '_>) -> Option { + fn core_type(&self, famous_defs: &FamousDefs<'_, '_>) -> Option { match self { WrapperKind::Option => famous_defs.core_option_Option(), WrapperKind::Result => famous_defs.core_result_Result(), @@ -96,81 +185,6 @@ impl WrapperKind { } } -fn wrap_return_type(acc: &mut Assists, ctx: &AssistContext<'_>, kind: WrapperKind) -> Option<()> { - let ret_type = ctx.find_node_at_offset::()?; - let parent = ret_type.syntax().parent()?; - let body = match_ast! { - match parent { - ast::Fn(func) => func.body()?, - ast::ClosureExpr(closure) => match closure.body()? { - Expr::BlockExpr(block) => block, - // closures require a block when a return type is specified - _ => return None, - }, - _ => return None, - } - }; - - let type_ref = &ret_type.ty()?; - let core_wrapper = - kind.core_type(FamousDefs(&ctx.sema, ctx.sema.scope(type_ref.syntax())?.krate()))?; - - let ty = ctx.sema.resolve_type(type_ref)?.as_adt(); - if matches!(ty, Some(hir::Adt::Enum(ret_type)) if ret_type == core_wrapper) { - // The return type is already wrapped - cov_mark::hit!(wrap_return_type_simple_return_type_already_wrapped); - return None; - } - - acc.add(kind.assist_id(), kind.label(), type_ref.syntax().text_range(), |edit| { - let alias = wrapper_alias(ctx, &core_wrapper, type_ref, kind.symbol()); - let new_return_ty = alias.unwrap_or_else(|| kind.wrap_type(type_ref)).clone_for_update(); - - let body = edit.make_mut(ast::Expr::BlockExpr(body)); - - let mut exprs_to_wrap = Vec::new(); - let tail_cb = &mut |e: &_| tail_cb_impl(&mut exprs_to_wrap, e); - walk_expr(&body, &mut |expr| { - if let Expr::ReturnExpr(ret_expr) = expr { - if let Some(ret_expr_arg) = &ret_expr.expr() { - for_each_tail_expr(ret_expr_arg, tail_cb); - } - } - }); - for_each_tail_expr(&body, tail_cb); - - for ret_expr_arg in exprs_to_wrap { - let happy_wrapped = make::expr_call( - make::expr_path(make::ext::ident_path(kind.happy_ident())), - make::arg_list(iter::once(ret_expr_arg.clone())), - ) - .clone_for_update(); - ted::replace(ret_expr_arg.syntax(), happy_wrapped.syntax()); - } - - let old_return_ty = edit.make_mut(type_ref.clone()); - ted::replace(old_return_ty.syntax(), new_return_ty.syntax()); - - if let WrapperKind::Result = kind { - // Add a placeholder snippet at the first generic argument that doesn't equal the return type. - // This is normally the error type, but that may not be the case when we inserted a type alias. - let args = new_return_ty.syntax().descendants().find_map(ast::GenericArgList::cast); - let error_type_arg = args.and_then(|list| { - list.generic_args().find(|arg| match arg { - ast::GenericArg::TypeArg(_) => arg.syntax().text() != type_ref.syntax().text(), - ast::GenericArg::LifetimeArg(_) => false, - _ => true, - }) - }); - if let Some(error_type_arg) = error_type_arg { - if let Some(cap) = ctx.config.snippet_cap { - edit.add_placeholder_snippet(cap, error_type_arg); - } - } - } - }) -} - // Try to find an wrapper type alias in the current scope (shadowing the default). fn wrapper_alias( ctx: &AssistContext<'_>, @@ -232,14 +246,14 @@ fn tail_cb_impl(acc: &mut Vec, e: &ast::Expr) { #[cfg(test)] mod tests { - use crate::tests::{check_assist, check_assist_not_applicable}; + use crate::tests::{check_assist_by_label, check_assist_not_applicable_by_label}; use super::*; #[test] fn wrap_return_type_in_option_simple() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i3$02 { @@ -253,13 +267,14 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_break_split_tail() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i3$02 { @@ -283,13 +298,14 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_closure() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() { @@ -307,13 +323,14 @@ fn foo() { }; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_return_type_bad_cursor() { - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32 { @@ -321,13 +338,14 @@ fn foo() -> i32 { return 42i32; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_return_type_bad_cursor_closure() { - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option fn foo() { @@ -337,24 +355,26 @@ fn foo() { }; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_closure_non_block() { - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option fn foo() { || -> i$032 3; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_return_type_already_option_std() { - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> core::option::Option { @@ -362,14 +382,15 @@ fn foo() -> core::option::Option { return 42i32; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_return_type_already_option() { cov_mark::check!(wrap_return_type_simple_return_type_already_wrapped); - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> Option { @@ -377,13 +398,14 @@ fn foo() -> Option { return 42i32; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_return_type_already_option_closure() { - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option fn foo() { @@ -393,13 +415,14 @@ fn foo() { }; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_cursor() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> $0i32 { @@ -413,13 +436,14 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() ->$0 i32 { @@ -433,13 +457,14 @@ fn foo() -> Option { Some(42i32) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_closure() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() { @@ -457,13 +482,14 @@ fn foo() { }; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_only() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { 42i32 } @@ -471,13 +497,14 @@ fn foo() -> i32$0 { 42i32 } r#" fn foo() -> Option { Some(42i32) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_block_like() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -497,13 +524,14 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_without_block_closure() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() { @@ -527,13 +555,14 @@ fn foo() { }; } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_nested_if() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -561,13 +590,14 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_await() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option async fn foo() -> i$032 { @@ -595,13 +625,14 @@ async fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_array() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> [i32;$0 3] { [1, 2, 3] } @@ -609,13 +640,14 @@ fn foo() -> [i32;$0 3] { [1, 2, 3] } r#" fn foo() -> Option<[i32; 3]> { Some([1, 2, 3]) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_cast() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -$0> i32 { @@ -643,13 +675,14 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_block_like_match() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -669,13 +702,14 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_loop_with_tail() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -697,13 +731,14 @@ fn foo() -> Option { Some(my_var) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_loop_in_let_stmt() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -721,13 +756,14 @@ fn foo() -> Option { Some(my_var) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_block_like_match_return_expr() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -749,10 +785,11 @@ fn foo() -> Option { Some(res) } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -776,13 +813,14 @@ fn foo() -> Option { Some(res) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_block_like_match_deeper() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -826,13 +864,14 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_tail_block_like_early_return() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i$032 { @@ -852,13 +891,14 @@ fn foo() -> Option { Some(53i32) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_in_option_tail_position() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(num: i32) -> $0i32 { @@ -870,13 +910,14 @@ fn foo(num: i32) -> Option { return Some(num) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_closure() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(the_field: u32) ->$0 u32 { @@ -906,10 +947,11 @@ fn foo(the_field: u32) -> Option { Some(the_field) } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> u32$0 { @@ -951,13 +993,14 @@ fn foo(the_field: u32) -> Option { Some(t.unwrap_or_else(|| the_field)) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_option_simple_with_weird_forms() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i32$0 { @@ -989,10 +1032,11 @@ fn foo() -> Option { } } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> u32$0 { @@ -1030,10 +1074,11 @@ fn foo(the_field: u32) -> Option { Some(the_field) } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> u3$02 { @@ -1059,10 +1104,11 @@ fn foo(the_field: u32) -> Option { Some(the_field) } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> u32$0 { @@ -1090,10 +1136,11 @@ fn foo(the_field: u32) -> Option { Some(the_field) } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo(the_field: u32) -> $0u32 { @@ -1121,13 +1168,14 @@ fn foo(the_field: u32) -> Option { Some(the_field) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_local_option_type() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option type Option = core::option::Option; @@ -1143,10 +1191,11 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option type Option2 = core::option::Option; @@ -1162,13 +1211,14 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_imported_local_option_type() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option mod some_module { @@ -1192,10 +1242,11 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option mod some_module { @@ -1219,13 +1270,14 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_local_option_type_from_function_body() { - check_assist( - wrap_return_type_in_option, + check_assist_by_label( + wrap_return_type, r#" //- minicore: option fn foo() -> i3$02 { @@ -1239,13 +1291,14 @@ fn foo() -> Option { Some(0) } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_local_option_type_already_using_alias() { - check_assist_not_applicable( - wrap_return_type_in_option, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: option pub type Option = core::option::Option; @@ -1254,13 +1307,14 @@ fn foo() -> Option { return Some(42i32); } "#, + WrapperKind::Option.label(), ); } #[test] fn wrap_return_type_in_result_simple() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i3$02 { @@ -1274,13 +1328,14 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_break_split_tail() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i3$02 { @@ -1304,13 +1359,14 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_closure() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() { @@ -1328,13 +1384,14 @@ fn foo() { }; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_return_type_bad_cursor() { - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32 { @@ -1342,13 +1399,14 @@ fn foo() -> i32 { return 42i32; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_return_type_bad_cursor_closure() { - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result fn foo() { @@ -1358,24 +1416,26 @@ fn foo() { }; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_closure_non_block() { - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result fn foo() { || -> i$032 3; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_return_type_already_result_std() { - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> core::result::Result { @@ -1383,14 +1443,15 @@ fn foo() -> core::result::Result { return 42i32; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_return_type_already_result() { cov_mark::check!(wrap_return_type_simple_return_type_already_wrapped); - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> Result { @@ -1398,13 +1459,14 @@ fn foo() -> Result { return 42i32; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_return_type_already_result_closure() { - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result fn foo() { @@ -1414,13 +1476,14 @@ fn foo() { }; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_cursor() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> $0i32 { @@ -1434,13 +1497,14 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() ->$0 i32 { @@ -1454,13 +1518,14 @@ fn foo() -> Result { Ok(42i32) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_closure() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() { @@ -1478,13 +1543,14 @@ fn foo() { }; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_only() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { 42i32 } @@ -1492,13 +1558,14 @@ fn foo() -> i32$0 { 42i32 } r#" fn foo() -> Result { Ok(42i32) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_block_like() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1518,13 +1585,14 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_without_block_closure() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() { @@ -1548,13 +1616,14 @@ fn foo() { }; } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_nested_if() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1582,13 +1651,14 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_await() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result async fn foo() -> i$032 { @@ -1616,13 +1686,14 @@ async fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_array() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> [i32;$0 3] { [1, 2, 3] } @@ -1630,13 +1701,14 @@ fn foo() -> [i32;$0 3] { [1, 2, 3] } r#" fn foo() -> Result<[i32; 3], ${0:_}> { Ok([1, 2, 3]) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_cast() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -$0> i32 { @@ -1664,13 +1736,14 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_block_like_match() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1690,13 +1763,14 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_loop_with_tail() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1718,13 +1792,14 @@ fn foo() -> Result { Ok(my_var) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_loop_in_let_stmt() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1742,13 +1817,14 @@ fn foo() -> Result { Ok(my_var) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_block_like_match_return_expr() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1770,10 +1846,11 @@ fn foo() -> Result { Ok(res) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1797,13 +1874,14 @@ fn foo() -> Result { Ok(res) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_block_like_match_deeper() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -1847,13 +1925,14 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_tail_block_like_early_return() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i$032 { @@ -1873,13 +1952,14 @@ fn foo() -> Result { Ok(53i32) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_in_result_tail_position() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(num: i32) -> $0i32 { @@ -1891,13 +1971,14 @@ fn foo(num: i32) -> Result { return Ok(num) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_closure() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(the_field: u32) ->$0 u32 { @@ -1927,10 +2008,11 @@ fn foo(the_field: u32) -> Result { Ok(the_field) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> u32$0 { @@ -1972,13 +2054,14 @@ fn foo(the_field: u32) -> Result { Ok(t.unwrap_or_else(|| the_field)) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_result_simple_with_weird_forms() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i32$0 { @@ -2010,10 +2093,11 @@ fn foo() -> Result { } } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> u32$0 { @@ -2051,10 +2135,11 @@ fn foo(the_field: u32) -> Result { Ok(the_field) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> u3$02 { @@ -2080,10 +2165,11 @@ fn foo(the_field: u32) -> Result { Ok(the_field) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> u32$0 { @@ -2111,10 +2197,11 @@ fn foo(the_field: u32) -> Result { Ok(the_field) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo(the_field: u32) -> $0u32 { @@ -2142,13 +2229,14 @@ fn foo(the_field: u32) -> Result { Ok(the_field) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_local_result_type() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result type Result = core::result::Result; @@ -2164,10 +2252,11 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result type Result2 = core::result::Result; @@ -2183,13 +2272,14 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_imported_local_result_type() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result mod some_module { @@ -2213,10 +2303,11 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result mod some_module { @@ -2240,13 +2331,14 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_local_result_type_from_function_body() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result fn foo() -> i3$02 { @@ -2260,13 +2352,14 @@ fn foo() -> Result { Ok(0) } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_local_result_type_already_using_alias() { - check_assist_not_applicable( - wrap_return_type_in_result, + check_assist_not_applicable_by_label( + wrap_return_type, r#" //- minicore: result pub type Result = core::result::Result; @@ -2275,13 +2368,14 @@ fn foo() -> Result { return Ok(42i32); } "#, + WrapperKind::Result.label(), ); } #[test] fn wrap_return_type_in_local_result_type_multiple_generics() { - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result type Result = core::result::Result; @@ -2297,10 +2391,11 @@ fn foo() -> Result { Ok(0) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result type Result = core::result::Result, ()>; @@ -2316,10 +2411,11 @@ fn foo() -> Result { Ok(0) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result type Result<'a, T, E> = core::result::Result, &'a ()>; @@ -2335,10 +2431,11 @@ fn foo() -> Result<'_, i32, ${0:_}> { Ok(0) } "#, + WrapperKind::Result.label(), ); - check_assist( - wrap_return_type_in_result, + check_assist_by_label( + wrap_return_type, r#" //- minicore: result type Result = core::result::Result, Bar>; @@ -2354,6 +2451,7 @@ fn foo() -> Result { Ok(0) } "#, + WrapperKind::Result.label(), ); } } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs b/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs index 8114888d6c3..22620816d50 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs @@ -355,12 +355,10 @@ mod handlers { unmerge_use::unmerge_use, unnecessary_async::unnecessary_async, unwrap_block::unwrap_block, - unwrap_return_type::unwrap_option_return_type, - unwrap_return_type::unwrap_result_return_type, + unwrap_return_type::unwrap_return_type, unwrap_tuple::unwrap_tuple, unqualify_method_call::unqualify_method_call, - wrap_return_type::wrap_return_type_in_option, - wrap_return_type::wrap_return_type_in_result, + wrap_return_type::wrap_return_type, wrap_unwrap_cfg_attr::wrap_unwrap_cfg_attr, // These are manually sorted for better priorities. By default,