Skip to content

Commit 95cabfd

Browse files
committed
Correctly pass through mutable references when extracting a function
1 parent 7409880 commit 95cabfd

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ impl FunctionBody {
878878
// We can move the value into the function call if it's not used after the call,
879879
// if the var is not used but defined outside a loop we are extracting from we can't move it either
880880
// as the function will reuse it in the next iteration.
881-
let move_local = !has_usages && defined_outside_parent_loop;
881+
let move_local = (!has_usages && defined_outside_parent_loop) || ty.is_reference();
882882
Param { var, ty, move_local, requires_mut, is_copy }
883883
})
884884
.collect()
@@ -4332,6 +4332,43 @@ fn foo() {
43324332
fn $0fun_name(a: _) -> _ {
43334333
a
43344334
}
4335+
"#,
4336+
);
4337+
}
4338+
4339+
#[test]
4340+
fn test_jeroen() {
4341+
check_assist(
4342+
extract_function,
4343+
r#"
4344+
pub struct Foo {
4345+
field: u32,
4346+
}
4347+
4348+
pub fn testfn(arg: &mut Foo) {
4349+
$0arg.field = 8; // write access
4350+
println!("{}", arg.field); // read access$0
4351+
// Simulating access after the extracted portion
4352+
arg.field = 16; // write access
4353+
println!("{}", arg.field); // read access
4354+
}
4355+
"#,
4356+
r#"
4357+
pub struct Foo {
4358+
field: u32,
4359+
}
4360+
4361+
pub fn testfn(arg: &mut Foo) {
4362+
fun_name(arg); // read access
4363+
// Simulating access after the extracted portion
4364+
arg.field = 16; // write access
4365+
println!("{}", arg.field); // read access
4366+
}
4367+
4368+
fn $0fun_name(arg: &mut Foo) {
4369+
arg.field = 8;
4370+
println!("{}", arg.field);
4371+
}
43354372
"#,
43364373
);
43374374
}

0 commit comments

Comments
 (0)