From 0ff74467c0d107a0b9472e928f9f0845f68be088 Mon Sep 17 00:00:00 2001 From: Vladyslav Katasonov Date: Fri, 5 Feb 2021 01:18:45 +0300 Subject: use `&T` for non copy params of extracted function Use shared ref if param is not `T: Copy` and is used after body --- crates/assists/src/handlers/extract_function.rs | 57 ++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/crates/assists/src/handlers/extract_function.rs b/crates/assists/src/handlers/extract_function.rs index 93ff66b24..ac2a5a674 100644 --- a/crates/assists/src/handlers/extract_function.rs +++ b/crates/assists/src/handlers/extract_function.rs @@ -124,6 +124,7 @@ struct Function { #[derive(Debug)] struct Param { var: Local, + ty: hir::Type, has_usages_afterwards: bool, has_mut_inside_body: bool, is_copy: bool, @@ -437,11 +438,14 @@ fn extracted_function_params( }) .map(|var| { let usages = LocalUsages::find(ctx, var); + let ty = var.ty(ctx.db()); + let is_copy = ty.is_copy(ctx.db()); Param { var, + ty, has_usages_afterwards: has_usages_after_body(&usages, body), has_mut_inside_body: has_exclusive_usages(ctx, &usages, body), - is_copy: true, + is_copy, } }) .collect() @@ -719,7 +723,7 @@ fn format_param_to(fn_def: &mut String, ctx: &AssistContext, module: hir::Module param.mut_pattern(), param.var.name(ctx.db()).unwrap(), param.type_prefix(), - format_type(¶m.var.ty(ctx.db()), ctx, module) + format_type(¶m.ty, ctx, module) ); } @@ -1723,6 +1727,55 @@ fn foo() { fn $0fun_name(n: i32) { let mut m = 2; m.inc(n); +}", + ); + } + + #[test] + fn non_copy_without_usages_after() { + check_assist( + extract_function, + r" +struct Counter(i32); +fn foo() { + let c = Counter(0); + $0let n = c.0;$0 +}", + r" +struct Counter(i32); +fn foo() { + let c = Counter(0); + fun_name(c); +} + +fn $0fun_name(c: Counter) { + let n = c.0; +}", + ); + } + + + #[test] + fn non_copy_used_after() { + check_assist( + extract_function, + r" +struct Counter(i32); +fn foo() { + let c = Counter(0); + $0let n = c.0;$0 + let m = c.0; +}", + r" +struct Counter(i32); +fn foo() { + let c = Counter(0); + fun_name(&c); + let m = c.0; +} + +fn $0fun_name(c: &Counter) { + let n = *c.0; }", ); } -- cgit v1.2.3