@@ -19,23 +19,21 @@ use crate::{AssistContext, AssistId, Assists};
1919// fn foo() -> Result<i32, > { Ok(42i32) }
2020// ```
2121pub ( crate ) fn change_return_type_to_result ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
22- let fn_def = ctx. find_node_at_offset :: < ast:: FnDef > ( ) ?;
23- let ret_type = & fn_def. ret_type ( ) ?. type_ref ( ) ?;
24- if ret_type. syntax ( ) . text ( ) . to_string ( ) . starts_with ( "Result<" ) {
22+ let ret_type = ctx. find_node_at_offset :: < ast:: RetType > ( ) ?;
23+ // FIXME: extend to lambdas as well
24+ let fn_def = ret_type. syntax ( ) . parent ( ) . and_then ( ast:: FnDef :: cast) ?;
25+
26+ let type_ref = & ret_type. type_ref ( ) ?;
27+ if type_ref. syntax ( ) . text ( ) . to_string ( ) . starts_with ( "Result<" ) {
2528 return None ;
2629 }
2730
2831 let block_expr = & fn_def. body ( ) ?;
29- let cursor_in_ret_type =
30- fn_def. ret_type ( ) ?. syntax ( ) . text_range ( ) . contains_range ( ctx. frange . range ) ;
31- if !cursor_in_ret_type {
32- return None ;
33- }
3432
3533 acc. add (
3634 AssistId ( "change_return_type_to_result" ) ,
3735 "Change return type to Result" ,
38- ret_type . syntax ( ) . text_range ( ) ,
36+ type_ref . syntax ( ) . text_range ( ) ,
3937 |edit| {
4038 let mut tail_return_expr_collector = TailReturnCollector :: new ( ) ;
4139 tail_return_expr_collector. collect_jump_exprs ( block_expr, false ) ;
@@ -44,10 +42,10 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
4442 for ret_expr_arg in tail_return_expr_collector. exprs_to_wrap {
4543 edit. replace_node_and_indent ( & ret_expr_arg, format ! ( "Ok({})" , ret_expr_arg) ) ;
4644 }
47- edit. replace_node_and_indent ( ret_type . syntax ( ) , format ! ( "Result<{}, >" , ret_type ) ) ;
45+ edit. replace_node_and_indent ( type_ref . syntax ( ) , format ! ( "Result<{}, >" , type_ref ) ) ;
4846
49- if let Some ( node_start) = result_insertion_offset ( & ret_type ) {
50- edit. set_cursor ( node_start + TextSize :: of ( & format ! ( "Result<{}, " , ret_type ) ) ) ;
47+ if let Some ( node_start) = result_insertion_offset ( & type_ref ) {
48+ edit. set_cursor ( node_start + TextSize :: of ( & format ! ( "Result<{}, " , type_ref ) ) ) ;
5149 }
5250 } ,
5351 )
0 commit comments