Skip to content

Commit 043421f

Browse files
committed
fix: no deref index-expr for extract_function
Example --- ```rust fn foo() { let mut arr = [1i32]; $0arr[0] = 3;$0 let _ = arr; } ``` **Before this PR** ```rust fn foo() { let mut arr = [1i32]; fun_name(&mut arr); let _ = arr; } fn $0fun_name(arr: &mut [i32; 1]) { *arr[0] = 3; } ``` **After this PR** ```rust fn foo() { let mut arr = [1i32]; fun_name(&mut arr); let _ = arr; } fn $0fun_name(arr: &mut [i32; 1]) { arr[0] = 3; } ```
1 parent 7b6e124 commit 043421f

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,11 @@ fn fix_param_usages(
20882088
for (param, usages) in usages_for_param {
20892089
for usage in usages {
20902090
match usage.syntax().ancestors().skip(1).find_map(ast::Expr::cast) {
2091-
Some(ast::Expr::MethodCallExpr(_) | ast::Expr::FieldExpr(_)) => {
2091+
Some(
2092+
ast::Expr::MethodCallExpr(_)
2093+
| ast::Expr::FieldExpr(_)
2094+
| ast::Expr::IndexExpr(_),
2095+
) => {
20922096
// do nothing
20932097
}
20942098
Some(ast::Expr::RefExpr(node))
@@ -3211,6 +3215,32 @@ fn $0fun_name(n: &mut i32) {
32113215
);
32123216
}
32133217

3218+
#[test]
3219+
fn mut_index_from_outer_scope() {
3220+
check_assist(
3221+
extract_function,
3222+
r#"
3223+
//- minicore: index
3224+
fn foo() {
3225+
let mut arr = [1i32];
3226+
$0arr[0] = 3;$0
3227+
let _ = arr;
3228+
}
3229+
"#,
3230+
r#"
3231+
fn foo() {
3232+
let mut arr = [1i32];
3233+
fun_name(&mut arr);
3234+
let _ = arr;
3235+
}
3236+
3237+
fn $0fun_name(arr: &mut [i32; 1]) {
3238+
arr[0] = 3;
3239+
}
3240+
"#,
3241+
);
3242+
}
3243+
32143244
#[test]
32153245
fn mut_field_from_outer_scope() {
32163246
check_assist(

0 commit comments

Comments
 (0)