aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorVladyslav Katasonov <[email protected]>2021-02-04 22:18:45 +0000
committerVladyslav Katasonov <[email protected]>2021-02-04 22:41:40 +0000
commit0ff74467c0d107a0b9472e928f9f0845f68be088 (patch)
treef44778c3e38e4520b34a4431853d974c909f8704 /crates/assists
parentd9b122858b8a454c23dbeab0971571ce0b38aeec (diff)
use `&T` for non copy params of extracted function
Use shared ref if param is not `T: Copy` and is used after body
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/extract_function.rs57
1 files 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 {
124#[derive(Debug)] 124#[derive(Debug)]
125struct Param { 125struct Param {
126 var: Local, 126 var: Local,
127 ty: hir::Type,
127 has_usages_afterwards: bool, 128 has_usages_afterwards: bool,
128 has_mut_inside_body: bool, 129 has_mut_inside_body: bool,
129 is_copy: bool, 130 is_copy: bool,
@@ -437,11 +438,14 @@ fn extracted_function_params(
437 }) 438 })
438 .map(|var| { 439 .map(|var| {
439 let usages = LocalUsages::find(ctx, var); 440 let usages = LocalUsages::find(ctx, var);
441 let ty = var.ty(ctx.db());
442 let is_copy = ty.is_copy(ctx.db());
440 Param { 443 Param {
441 var, 444 var,
445 ty,
442 has_usages_afterwards: has_usages_after_body(&usages, body), 446 has_usages_afterwards: has_usages_after_body(&usages, body),
443 has_mut_inside_body: has_exclusive_usages(ctx, &usages, body), 447 has_mut_inside_body: has_exclusive_usages(ctx, &usages, body),
444 is_copy: true, 448 is_copy,
445 } 449 }
446 }) 450 })
447 .collect() 451 .collect()
@@ -719,7 +723,7 @@ fn format_param_to(fn_def: &mut String, ctx: &AssistContext, module: hir::Module
719 param.mut_pattern(), 723 param.mut_pattern(),
720 param.var.name(ctx.db()).unwrap(), 724 param.var.name(ctx.db()).unwrap(),
721 param.type_prefix(), 725 param.type_prefix(),
722 format_type(&param.var.ty(ctx.db()), ctx, module) 726 format_type(&param.ty, ctx, module)
723 ); 727 );
724} 728}
725 729
@@ -1726,4 +1730,53 @@ fn $0fun_name(n: i32) {
1726}", 1730}",
1727 ); 1731 );
1728 } 1732 }
1733
1734 #[test]
1735 fn non_copy_without_usages_after() {
1736 check_assist(
1737 extract_function,
1738 r"
1739struct Counter(i32);
1740fn foo() {
1741 let c = Counter(0);
1742 $0let n = c.0;$0
1743}",
1744 r"
1745struct Counter(i32);
1746fn foo() {
1747 let c = Counter(0);
1748 fun_name(c);
1749}
1750
1751fn $0fun_name(c: Counter) {
1752 let n = c.0;
1753}",
1754 );
1755 }
1756
1757
1758 #[test]
1759 fn non_copy_used_after() {
1760 check_assist(
1761 extract_function,
1762 r"
1763struct Counter(i32);
1764fn foo() {
1765 let c = Counter(0);
1766 $0let n = c.0;$0
1767 let m = c.0;
1768}",
1769 r"
1770struct Counter(i32);
1771fn foo() {
1772 let c = Counter(0);
1773 fun_name(&c);
1774 let m = c.0;
1775}
1776
1777fn $0fun_name(c: &Counter) {
1778 let n = *c.0;
1779}",
1780 );
1781 }
1729} 1782}