@@ -41,35 +41,31 @@ pub(crate) fn convert_nested_function_to_closure(
41
41
let target = function. syntax ( ) . text_range ( ) ;
42
42
let body = function. body ( ) ?;
43
43
let name = function. name ( ) ?;
44
- let params = function. param_list ( ) ?;
45
-
46
- let params_text = params. syntax ( ) . text ( ) . to_string ( ) ;
47
- let closure_params = params_text. strip_prefix ( "(" ) . and_then ( |p| p. strip_suffix ( ")" ) ) ?;
44
+ let param_list = function. param_list ( ) ?;
48
45
49
46
acc. add (
50
47
AssistId ( "convert_nested_function_to_closure" , AssistKind :: RefactorRewrite ) ,
51
48
"Convert nested function to closure" ,
52
49
target,
53
50
|edit| {
54
- let has_semicolon = has_semicolon ( & function) ;
51
+ let params = & param_list. syntax ( ) . text ( ) . to_string ( ) ;
52
+ let params = params. strip_prefix ( "(" ) . unwrap_or ( params) ;
53
+ let params = params. strip_suffix ( ")" ) . unwrap_or ( params) ;
55
54
56
55
let mut body = body. to_string ( ) ;
57
- if !has_semicolon {
56
+ if !has_semicolon ( & function ) {
58
57
body. push ( ';' ) ;
59
58
}
60
- edit. replace ( target, format ! ( "let {} = |{}| {}" , name , closure_params , body ) ) ;
59
+ edit. replace ( target, format ! ( "let {name } = |{params }| {body}" ) ) ;
61
60
} ,
62
61
)
63
62
}
64
63
65
64
/// Returns whether the given function is nested within the body of another function.
66
65
fn is_nested_function ( function : & ast:: Fn ) -> bool {
67
- function
68
- . syntax ( )
69
- . ancestors ( )
70
- . skip ( 1 )
71
- . find_map ( ast:: Item :: cast)
72
- . map_or ( false , |it| matches ! ( it, ast:: Item :: Fn ( _) ) )
66
+ function. syntax ( ) . ancestors ( ) . skip ( 1 ) . find_map ( ast:: Item :: cast) . map_or ( false , |it| {
67
+ matches ! ( it, ast:: Item :: Fn ( _) | ast:: Item :: Static ( _) | ast:: Item :: Const ( _) )
68
+ } )
73
69
}
74
70
75
71
/// Returns whether the given nested function has generic parameters.
0 commit comments