mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +00:00
Auto merge of #12706 - DorianListens:dscheidt/closure-args, r=DorianListens
fix: Extract Function misses locals used in closures This change fixes #12705. In `FunctionBody::analyze`, we need to search any `ClosureExpr`s we encounter for any `NameRef`s, to ensure they aren't missed.
This commit is contained in:
commit
2836dd15f7
1 changed files with 30 additions and 0 deletions
|
@ -699,6 +699,11 @@ impl FunctionBody {
|
|||
ast::Expr::PathExpr(path_expr) => {
|
||||
cb(path_expr.path().and_then(|it| it.as_single_name_ref()))
|
||||
}
|
||||
ast::Expr::ClosureExpr(closure_expr) => {
|
||||
if let Some(body) = closure_expr.body() {
|
||||
body.syntax().descendants().map(ast::NameRef::cast).for_each(|it| cb(it));
|
||||
}
|
||||
}
|
||||
ast::Expr::MacroExpr(expr) => {
|
||||
if let Some(tt) = expr.macro_call().and_then(|call| call.token_tree()) {
|
||||
tt.syntax()
|
||||
|
@ -4842,6 +4847,31 @@ impl Struct {
|
|||
self.0 + 2
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closure_arguments() {
|
||||
check_assist(
|
||||
extract_function,
|
||||
r#"
|
||||
fn parent(factor: i32) {
|
||||
let v = &[1, 2, 3];
|
||||
|
||||
$0v.iter().map(|it| it * factor);$0
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
fn parent(factor: i32) {
|
||||
let v = &[1, 2, 3];
|
||||
|
||||
fun_name(v, factor);
|
||||
}
|
||||
|
||||
fn $0fun_name(v: &[i32; 3], factor: i32) {
|
||||
v.iter().map(|it| it * factor);
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue