Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bc704e1

Browse files
committed
Address another round of review comments
1 parent d01a38c commit bc704e1

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,31 @@ pub(crate) fn convert_nested_function_to_closure(
4141
let target = function.syntax().text_range();
4242
let body = function.body()?;
4343
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()?;
4845

4946
acc.add(
5047
AssistId("convert_nested_function_to_closure", AssistKind::RefactorRewrite),
5148
"Convert nested function to closure",
5249
target,
5350
|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);
5554

5655
let mut body = body.to_string();
57-
if !has_semicolon {
56+
if !has_semicolon(&function) {
5857
body.push(';');
5958
}
60-
edit.replace(target, format!("let {} = |{}| {}", name, closure_params, body));
59+
edit.replace(target, format!("let {name} = |{params}| {body}"));
6160
},
6261
)
6362
}
6463

6564
/// Returns whether the given function is nested within the body of another function.
6665
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+
})
7369
}
7470

7571
/// Returns whether the given nested function has generic parameters.

0 commit comments

Comments
 (0)