aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists')
-rw-r--r--crates/ide_assists/src/handlers/apply_demorgan.rs114
-rw-r--r--crates/ide_assists/src/handlers/convert_into_to_from.rs6
-rw-r--r--crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs14
-rw-r--r--crates/ide_assists/src/handlers/extract_function.rs719
-rw-r--r--crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs27
-rw-r--r--crates/ide_assists/src/handlers/fill_match_arms.rs11
-rw-r--r--crates/ide_assists/src/handlers/remove_dbg.rs4
-rw-r--r--crates/ide_assists/src/handlers/replace_if_let_with_match.rs66
-rw-r--r--crates/ide_assists/src/handlers/replace_unwrap_with_match.rs60
-rw-r--r--crates/ide_assists/src/lib.rs1
-rw-r--r--crates/ide_assists/src/path_transform.rs160
-rw-r--r--crates/ide_assists/src/tests.rs1
-rw-r--r--crates/ide_assists/src/tests/generated.rs27
-rw-r--r--crates/ide_assists/src/utils.rs6
14 files changed, 555 insertions, 661 deletions
diff --git a/crates/ide_assists/src/handlers/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs
index c93959e66..e2bd6e456 100644
--- a/crates/ide_assists/src/handlers/apply_demorgan.rs
+++ b/crates/ide_assists/src/handlers/apply_demorgan.rs
@@ -147,74 +147,92 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
147 147
148#[cfg(test)] 148#[cfg(test)]
149mod tests { 149mod tests {
150 use ide_db::helpers::FamousDefs;
151
152 use super::*;
153
154 use crate::tests::{check_assist, check_assist_not_applicable}; 150 use crate::tests::{check_assist, check_assist_not_applicable};
155 151
156 const ORDABLE_FIXTURE: &'static str = r" 152 use super::*;
157//- /lib.rs deps:core crate:ordable
158struct NonOrderable;
159struct Orderable;
160impl core::cmp::Ord for Orderable {}
161";
162
163 fn check(ra_fixture_before: &str, ra_fixture_after: &str) {
164 let before = &format!(
165 "//- /main.rs crate:main deps:core,ordable\n{}\n{}{}",
166 ra_fixture_before,
167 FamousDefs::FIXTURE,
168 ORDABLE_FIXTURE
169 );
170 check_assist(apply_demorgan, before, &format!("{}\n", ra_fixture_after));
171 }
172 153
173 #[test] 154 #[test]
174 fn demorgan_handles_leq() { 155 fn demorgan_handles_leq() {
175 check( 156 check_assist(
176 r"use ordable::Orderable; 157 apply_demorgan,
158 r#"
159//- minicore: ord, derive
160#[derive(PartialEq, Eq, PartialOrd, Ord)]
161struct S;
162
177fn f() { 163fn f() {
178 Orderable < Orderable &&$0 Orderable <= Orderable 164 S < S &&$0 S <= S
179}", 165}
180 r"use ordable::Orderable; 166"#,
167 r#"
168#[derive(PartialEq, Eq, PartialOrd, Ord)]
169struct S;
170
181fn f() { 171fn f() {
182 !(Orderable >= Orderable || Orderable > Orderable) 172 !(S >= S || S > S)
183}", 173}
174"#,
184 ); 175 );
185 check( 176
186 r"use ordable::NonOrderable; 177 check_assist(
178 apply_demorgan,
179 r#"
180//- minicore: ord, derive
181struct S;
182
187fn f() { 183fn f() {
188 NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable 184 S < S &&$0 S <= S
189}", 185}
190 r"use ordable::NonOrderable; 186"#,
187 r#"
188struct S;
189
191fn f() { 190fn f() {
192 !(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable)) 191 !(!(S < S) || !(S <= S))
193}", 192}
193"#,
194 ); 194 );
195 } 195 }
196 196
197 #[test] 197 #[test]
198 fn demorgan_handles_geq() { 198 fn demorgan_handles_geq() {
199 check( 199 check_assist(
200 r"use ordable::Orderable; 200 apply_demorgan,
201 r#"
202//- minicore: ord, derive
203#[derive(PartialEq, Eq, PartialOrd, Ord)]
204struct S;
205
201fn f() { 206fn f() {
202 Orderable > Orderable &&$0 Orderable >= Orderable 207 S > S &&$0 S >= S
203}", 208}
204 r"use ordable::Orderable; 209"#,
210 r#"
211#[derive(PartialEq, Eq, PartialOrd, Ord)]
212struct S;
213
205fn f() { 214fn f() {
206 !(Orderable <= Orderable || Orderable < Orderable) 215 !(S <= S || S < S)
207}", 216}
217"#,
208 ); 218 );
209 check( 219 check_assist(
210 r"use ordable::NonOrderable; 220 apply_demorgan,
221 r#"
222//- minicore: ord, derive
223struct S;
224
211fn f() { 225fn f() {
212 Orderable > Orderable &&$0 Orderable >= Orderable 226 S > S &&$0 S >= S
213}", 227}
214 r"use ordable::NonOrderable; 228"#,
229 r#"
230struct S;
231
215fn f() { 232fn f() {
216 !(!(Orderable > Orderable) || !(Orderable >= Orderable)) 233 !(!(S > S) || !(S >= S))
217}", 234}
235"#,
218 ); 236 );
219 } 237 }
220 238
diff --git a/crates/ide_assists/src/handlers/convert_into_to_from.rs b/crates/ide_assists/src/handlers/convert_into_to_from.rs
index 79a0c4879..2d8b936cd 100644
--- a/crates/ide_assists/src/handlers/convert_into_to_from.rs
+++ b/crates/ide_assists/src/handlers/convert_into_to_from.rs
@@ -13,10 +13,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
13// Converts an Into impl to an equivalent From impl. 13// Converts an Into impl to an equivalent From impl.
14// 14//
15// ``` 15// ```
16// # //- /lib.rs crate:core 16// # //- minicore: from
17// # pub mod convert { pub trait Into<T> { pub fn into(self) -> T; } }
18// # //- /lib.rs crate:main deps:core
19// # use core::convert::Into;
20// impl $0Into<Thing> for usize { 17// impl $0Into<Thing> for usize {
21// fn into(self) -> Thing { 18// fn into(self) -> Thing {
22// Thing { 19// Thing {
@@ -28,7 +25,6 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
28// ``` 25// ```
29// -> 26// ->
30// ``` 27// ```
31// # use core::convert::Into;
32// impl From<usize> for Thing { 28// impl From<usize> for Thing {
33// fn from(val: usize) -> Self { 29// fn from(val: usize) -> Self {
34// Thing { 30// Thing {
diff --git a/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs b/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs
index 7fd73d4c7..70754adf9 100644
--- a/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs
+++ b/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs
@@ -11,14 +11,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
11// Converts an Iterator::for_each function into a for loop. 11// Converts an Iterator::for_each function into a for loop.
12// 12//
13// ``` 13// ```
14// # //- /lib.rs crate:core 14// # //- minicore: iterators
15// # pub mod iter { pub mod traits { pub mod iterator { pub trait Iterator {} } } } 15// # use core::iter;
16// # pub struct SomeIter;
17// # impl self::iter::traits::iterator::Iterator for SomeIter {}
18// # //- /lib.rs crate:main deps:core
19// # use core::SomeIter;
20// fn main() { 16// fn main() {
21// let iter = SomeIter; 17// let iter = iter::repeat((9, 2));
22// iter.for_each$0(|(x, y)| { 18// iter.for_each$0(|(x, y)| {
23// println!("x: {}, y: {}", x, y); 19// println!("x: {}, y: {}", x, y);
24// }); 20// });
@@ -26,9 +22,9 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
26// ``` 22// ```
27// -> 23// ->
28// ``` 24// ```
29// # use core::SomeIter; 25// # use core::iter;
30// fn main() { 26// fn main() {
31// let iter = SomeIter; 27// let iter = iter::repeat((9, 2));
32// for (x, y) in iter { 28// for (x, y) in iter {
33// println!("x: {}, y: {}", x, y); 29// println!("x: {}, y: {}", x, y);
34// } 30// }
diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs
index 7085a0c48..ac7f0959b 100644
--- a/crates/ide_assists/src/handlers/extract_function.rs
+++ b/crates/ide_assists/src/handlers/extract_function.rs
@@ -1501,7 +1501,8 @@ mod tests {
1501 r#" 1501 r#"
1502fn foo() { 1502fn foo() {
1503 foo($01 + 1$0); 1503 foo($01 + 1$0);
1504}"#, 1504}
1505"#,
1505 r#" 1506 r#"
1506fn foo() { 1507fn foo() {
1507 foo(fun_name()); 1508 foo(fun_name());
@@ -1509,7 +1510,8 @@ fn foo() {
1509 1510
1510fn $0fun_name() -> i32 { 1511fn $0fun_name() -> i32 {
1511 1 + 1 1512 1 + 1
1512}"#, 1513}
1514"#,
1513 ); 1515 );
1514 } 1516 }
1515 1517
@@ -1522,7 +1524,8 @@ mod bar {
1522 fn foo() { 1524 fn foo() {
1523 foo($01 + 1$0); 1525 foo($01 + 1$0);
1524 } 1526 }
1525}"#, 1527}
1528"#,
1526 r#" 1529 r#"
1527mod bar { 1530mod bar {
1528 fn foo() { 1531 fn foo() {
@@ -1532,7 +1535,8 @@ mod bar {
1532 fn $0fun_name() -> i32 { 1535 fn $0fun_name() -> i32 {
1533 1 + 1 1536 1 + 1
1534 } 1537 }
1535}"#, 1538}
1539"#,
1536 ); 1540 );
1537 } 1541 }
1538 1542
@@ -1543,7 +1547,8 @@ mod bar {
1543 r#" 1547 r#"
1544fn foo() { 1548fn foo() {
1545 $0{ 1 + 1 }$0; 1549 $0{ 1 + 1 }$0;
1546}"#, 1550}
1551"#,
1547 r#" 1552 r#"
1548fn foo() { 1553fn foo() {
1549 fun_name(); 1554 fun_name();
@@ -1551,7 +1556,8 @@ fn foo() {
1551 1556
1552fn $0fun_name() -> i32 { 1557fn $0fun_name() -> i32 {
1553 1 + 1 1558 1 + 1
1554}"#, 1559}
1560"#,
1555 ); 1561 );
1556 } 1562 }
1557 1563
@@ -1564,7 +1570,8 @@ fn foo() -> i32 {
1564 let k = 1; 1570 let k = 1;
1565 $0let m = 1; 1571 $0let m = 1;
1566 m + 1$0 1572 m + 1$0
1567}"#, 1573}
1574"#,
1568 r#" 1575 r#"
1569fn foo() -> i32 { 1576fn foo() -> i32 {
1570 let k = 1; 1577 let k = 1;
@@ -1574,7 +1581,8 @@ fn foo() -> i32 {
1574fn $0fun_name() -> i32 { 1581fn $0fun_name() -> i32 {
1575 let m = 1; 1582 let m = 1;
1576 m + 1 1583 m + 1
1577}"#, 1584}
1585"#,
1578 ); 1586 );
1579 } 1587 }
1580 1588
@@ -1588,7 +1596,8 @@ fn foo() {
1588 $0let m = 1; 1596 $0let m = 1;
1589 let n = m + 1;$0 1597 let n = m + 1;$0
1590 let g = 5; 1598 let g = 5;
1591}"#, 1599}
1600"#,
1592 r#" 1601 r#"
1593fn foo() { 1602fn foo() {
1594 let k = 3; 1603 let k = 3;
@@ -1599,7 +1608,8 @@ fn foo() {
1599fn $0fun_name() { 1608fn $0fun_name() {
1600 let m = 1; 1609 let m = 1;
1601 let n = m + 1; 1610 let n = m + 1;
1602}"#, 1611}
1612"#,
1603 ); 1613 );
1604 } 1614 }
1605 1615
@@ -1610,7 +1620,8 @@ fn $0fun_name() {
1610 r#" 1620 r#"
1611fn foo() { 1621fn foo() {
1612 $0if true { }$0 1622 $0if true { }$0
1613}"#, 1623}
1624"#,
1614 r#" 1625 r#"
1615fn foo() { 1626fn foo() {
1616 fun_name(); 1627 fun_name();
@@ -1618,7 +1629,8 @@ fn foo() {
1618 1629
1619fn $0fun_name() { 1630fn $0fun_name() {
1620 if true { } 1631 if true { }
1621}"#, 1632}
1633"#,
1622 ); 1634 );
1623 } 1635 }
1624 1636
@@ -1629,7 +1641,8 @@ fn $0fun_name() {
1629 r#" 1641 r#"
1630fn foo() -> i32 { 1642fn foo() -> i32 {
1631 $0if true { 1 } else { 2 }$0 1643 $0if true { 1 } else { 2 }$0
1632}"#, 1644}
1645"#,
1633 r#" 1646 r#"
1634fn foo() -> i32 { 1647fn foo() -> i32 {
1635 fun_name() 1648 fun_name()
@@ -1637,7 +1650,8 @@ fn foo() -> i32 {
1637 1650
1638fn $0fun_name() -> i32 { 1651fn $0fun_name() -> i32 {
1639 if true { 1 } else { 2 } 1652 if true { 1 } else { 2 }
1640}"#, 1653}
1654"#,
1641 ); 1655 );
1642 } 1656 }
1643 1657
@@ -1648,7 +1662,8 @@ fn $0fun_name() -> i32 {
1648 r#" 1662 r#"
1649fn foo() -> i32 { 1663fn foo() -> i32 {
1650 $0if let true = false { 1 } else { 2 }$0 1664 $0if let true = false { 1 } else { 2 }$0
1651}"#, 1665}
1666"#,
1652 r#" 1667 r#"
1653fn foo() -> i32 { 1668fn foo() -> i32 {
1654 fun_name() 1669 fun_name()
@@ -1656,7 +1671,8 @@ fn foo() -> i32 {
1656 1671
1657fn $0fun_name() -> i32 { 1672fn $0fun_name() -> i32 {
1658 if let true = false { 1 } else { 2 } 1673 if let true = false { 1 } else { 2 }
1659}"#, 1674}
1675"#,
1660 ); 1676 );
1661 } 1677 }
1662 1678
@@ -1670,7 +1686,8 @@ fn foo() -> i32 {
1670 true => 1, 1686 true => 1,
1671 false => 2, 1687 false => 2,
1672 }$0 1688 }$0
1673}"#, 1689}
1690"#,
1674 r#" 1691 r#"
1675fn foo() -> i32 { 1692fn foo() -> i32 {
1676 fun_name() 1693 fun_name()
@@ -1681,7 +1698,8 @@ fn $0fun_name() -> i32 {
1681 true => 1, 1698 true => 1,
1682 false => 2, 1699 false => 2,
1683 } 1700 }
1684}"#, 1701}
1702"#,
1685 ); 1703 );
1686 } 1704 }
1687 1705
@@ -1692,7 +1710,8 @@ fn $0fun_name() -> i32 {
1692 r#" 1710 r#"
1693fn foo() { 1711fn foo() {
1694 $0while true { }$0 1712 $0while true { }$0
1695}"#, 1713}
1714"#,
1696 r#" 1715 r#"
1697fn foo() { 1716fn foo() {
1698 fun_name(); 1717 fun_name();
@@ -1700,7 +1719,8 @@ fn foo() {
1700 1719
1701fn $0fun_name() { 1720fn $0fun_name() {
1702 while true { } 1721 while true { }
1703}"#, 1722}
1723"#,
1704 ); 1724 );
1705 } 1725 }
1706 1726
@@ -1711,7 +1731,8 @@ fn $0fun_name() {
1711 r#" 1731 r#"
1712fn foo() { 1732fn foo() {
1713 $0for v in &[0, 1] { }$0 1733 $0for v in &[0, 1] { }$0
1714}"#, 1734}
1735"#,
1715 r#" 1736 r#"
1716fn foo() { 1737fn foo() {
1717 fun_name(); 1738 fun_name();
@@ -1719,7 +1740,8 @@ fn foo() {
1719 1740
1720fn $0fun_name() { 1741fn $0fun_name() {
1721 for v in &[0, 1] { } 1742 for v in &[0, 1] { }
1722}"#, 1743}
1744"#,
1723 ); 1745 );
1724 } 1746 }
1725 1747
@@ -1732,7 +1754,8 @@ fn foo() {
1732 $0loop { 1754 $0loop {
1733 let m = 1; 1755 let m = 1;
1734 }$0 1756 }$0
1735}"#, 1757}
1758"#,
1736 r#" 1759 r#"
1737fn foo() { 1760fn foo() {
1738 fun_name() 1761 fun_name()
@@ -1742,7 +1765,8 @@ fn $0fun_name() -> ! {
1742 loop { 1765 loop {
1743 let m = 1; 1766 let m = 1;
1744 } 1767 }
1745}"#, 1768}
1769"#,
1746 ); 1770 );
1747 } 1771 }
1748 1772
@@ -1756,7 +1780,8 @@ fn foo() {
1756 let m = 1; 1780 let m = 1;
1757 break m; 1781 break m;
1758 }$0; 1782 }$0;
1759}"#, 1783}
1784"#,
1760 r#" 1785 r#"
1761fn foo() { 1786fn foo() {
1762 let v = fun_name(); 1787 let v = fun_name();
@@ -1767,7 +1792,8 @@ fn $0fun_name() -> i32 {
1767 let m = 1; 1792 let m = 1;
1768 break m; 1793 break m;
1769 } 1794 }
1770}"#, 1795}
1796"#,
1771 ); 1797 );
1772 } 1798 }
1773 1799
@@ -1781,7 +1807,8 @@ fn foo() {
1781 Some(x) => x, 1807 Some(x) => x,
1782 None => 0, 1808 None => 0,
1783 }$0; 1809 }$0;
1784}"#, 1810}
1811"#,
1785 r#" 1812 r#"
1786fn foo() { 1813fn foo() {
1787 let v: i32 = fun_name(); 1814 let v: i32 = fun_name();
@@ -1792,7 +1819,8 @@ fn $0fun_name() -> i32 {
1792 Some(x) => x, 1819 Some(x) => x,
1793 None => 0, 1820 None => 0,
1794 } 1821 }
1795}"#, 1822}
1823"#,
1796 ); 1824 );
1797 } 1825 }
1798 1826
@@ -1805,7 +1833,8 @@ fn foo() {
1805 let n = 1; 1833 let n = 1;
1806 let mut v = $0n * n;$0 1834 let mut v = $0n * n;$0
1807 v += 1; 1835 v += 1;
1808}"#, 1836}
1837"#,
1809 r#" 1838 r#"
1810fn foo() { 1839fn foo() {
1811 let n = 1; 1840 let n = 1;
@@ -1816,7 +1845,8 @@ fn foo() {
1816fn $0fun_name(n: i32) -> i32 { 1845fn $0fun_name(n: i32) -> i32 {
1817 let mut v = n * n; 1846 let mut v = n * n;
1818 v 1847 v
1819}"#, 1848}
1849"#,
1820 ); 1850 );
1821 } 1851 }
1822 1852
@@ -1832,7 +1862,8 @@ fn foo() {
1832 let mut w = 3;$0 1862 let mut w = 3;$0
1833 v += 1; 1863 v += 1;
1834 w += 1; 1864 w += 1;
1835}"#, 1865}
1866"#,
1836 r#" 1867 r#"
1837fn foo() { 1868fn foo() {
1838 let m = 2; 1869 let m = 2;
@@ -1846,7 +1877,8 @@ fn $0fun_name(m: i32, n: i32) -> (i32, i32) {
1846 let mut v = m * n; 1877 let mut v = m * n;
1847 let mut w = 3; 1878 let mut w = 3;
1848 (v, w) 1879 (v, w)
1849}"#, 1880}
1881"#,
1850 ); 1882 );
1851 } 1883 }
1852 1884
@@ -1854,12 +1886,13 @@ fn $0fun_name(m: i32, n: i32) -> (i32, i32) {
1854 fn argument_form_expr() { 1886 fn argument_form_expr() {
1855 check_assist( 1887 check_assist(
1856 extract_function, 1888 extract_function,
1857 r" 1889 r#"
1858fn foo() -> u32 { 1890fn foo() -> u32 {
1859 let n = 2; 1891 let n = 2;
1860 $0n+2$0 1892 $0n+2$0
1861}", 1893}
1862 r" 1894"#,
1895 r#"
1863fn foo() -> u32 { 1896fn foo() -> u32 {
1864 let n = 2; 1897 let n = 2;
1865 fun_name(n) 1898 fun_name(n)
@@ -1867,7 +1900,8 @@ fn foo() -> u32 {
1867 1900
1868fn $0fun_name(n: u32) -> u32 { 1901fn $0fun_name(n: u32) -> u32 {
1869 n+2 1902 n+2
1870}", 1903}
1904"#,
1871 ) 1905 )
1872 } 1906 }
1873 1907
@@ -1875,12 +1909,13 @@ fn $0fun_name(n: u32) -> u32 {
1875 fn argument_used_twice_form_expr() { 1909 fn argument_used_twice_form_expr() {
1876 check_assist( 1910 check_assist(
1877 extract_function, 1911 extract_function,
1878 r" 1912 r#"
1879fn foo() -> u32 { 1913fn foo() -> u32 {
1880 let n = 2; 1914 let n = 2;
1881 $0n+n$0 1915 $0n+n$0
1882}", 1916}
1883 r" 1917"#,
1918 r#"
1884fn foo() -> u32 { 1919fn foo() -> u32 {
1885 let n = 2; 1920 let n = 2;
1886 fun_name(n) 1921 fun_name(n)
@@ -1888,7 +1923,8 @@ fn foo() -> u32 {
1888 1923
1889fn $0fun_name(n: u32) -> u32 { 1924fn $0fun_name(n: u32) -> u32 {
1890 n+n 1925 n+n
1891}", 1926}
1927"#,
1892 ) 1928 )
1893 } 1929 }
1894 1930
@@ -1896,13 +1932,14 @@ fn $0fun_name(n: u32) -> u32 {
1896 fn two_arguments_form_expr() { 1932 fn two_arguments_form_expr() {
1897 check_assist( 1933 check_assist(
1898 extract_function, 1934 extract_function,
1899 r" 1935 r#"
1900fn foo() -> u32 { 1936fn foo() -> u32 {
1901 let n = 2; 1937 let n = 2;
1902 let m = 3; 1938 let m = 3;
1903 $0n+n*m$0 1939 $0n+n*m$0
1904}", 1940}
1905 r" 1941"#,
1942 r#"
1906fn foo() -> u32 { 1943fn foo() -> u32 {
1907 let n = 2; 1944 let n = 2;
1908 let m = 3; 1945 let m = 3;
@@ -1911,7 +1948,8 @@ fn foo() -> u32 {
1911 1948
1912fn $0fun_name(n: u32, m: u32) -> u32 { 1949fn $0fun_name(n: u32, m: u32) -> u32 {
1913 n+n*m 1950 n+n*m
1914}", 1951}
1952"#,
1915 ) 1953 )
1916 } 1954 }
1917 1955
@@ -1919,13 +1957,14 @@ fn $0fun_name(n: u32, m: u32) -> u32 {
1919 fn argument_and_locals() { 1957 fn argument_and_locals() {
1920 check_assist( 1958 check_assist(
1921 extract_function, 1959 extract_function,
1922 r" 1960 r#"
1923fn foo() -> u32 { 1961fn foo() -> u32 {
1924 let n = 2; 1962 let n = 2;
1925 $0let m = 1; 1963 $0let m = 1;
1926 n + m$0 1964 n + m$0
1927}", 1965}
1928 r" 1966"#,
1967 r#"
1929fn foo() -> u32 { 1968fn foo() -> u32 {
1930 let n = 2; 1969 let n = 2;
1931 fun_name(n) 1970 fun_name(n)
@@ -1934,7 +1973,8 @@ fn foo() -> u32 {
1934fn $0fun_name(n: u32) -> u32 { 1973fn $0fun_name(n: u32) -> u32 {
1935 let m = 1; 1974 let m = 1;
1936 n + m 1975 n + m
1937}", 1976}
1977"#,
1938 ) 1978 )
1939 } 1979 }
1940 1980
@@ -1948,18 +1988,20 @@ fn $0fun_name(n: u32) -> u32 {
1948 fn part_of_expr_stmt() { 1988 fn part_of_expr_stmt() {
1949 check_assist( 1989 check_assist(
1950 extract_function, 1990 extract_function,
1951 " 1991 r#"
1952fn foo() { 1992fn foo() {
1953 $01$0 + 1; 1993 $01$0 + 1;
1954}", 1994}
1955 " 1995"#,
1996 r#"
1956fn foo() { 1997fn foo() {
1957 fun_name() + 1; 1998 fun_name() + 1;
1958} 1999}
1959 2000
1960fn $0fun_name() -> i32 { 2001fn $0fun_name() -> i32 {
1961 1 2002 1
1962}", 2003}
2004"#,
1963 ); 2005 );
1964 } 2006 }
1965 2007
@@ -1970,7 +2012,8 @@ fn $0fun_name() -> i32 {
1970 r#" 2012 r#"
1971fn foo() { 2013fn foo() {
1972 $0bar(1 + 1)$0 2014 $0bar(1 + 1)$0
1973}"#, 2015}
2016"#,
1974 r#" 2017 r#"
1975fn foo() { 2018fn foo() {
1976 fun_name(); 2019 fun_name();
@@ -1978,7 +2021,8 @@ fn foo() {
1978 2021
1979fn $0fun_name() { 2022fn $0fun_name() {
1980 bar(1 + 1) 2023 bar(1 + 1)
1981}"#, 2024}
2025"#,
1982 ) 2026 )
1983 } 2027 }
1984 2028
@@ -1986,15 +2030,16 @@ fn $0fun_name() {
1986 fn extract_from_nested() { 2030 fn extract_from_nested() {
1987 check_assist( 2031 check_assist(
1988 extract_function, 2032 extract_function,
1989 r" 2033 r#"
1990fn main() { 2034fn main() {
1991 let x = true; 2035 let x = true;
1992 let tuple = match x { 2036 let tuple = match x {
1993 true => ($02 + 2$0, true) 2037 true => ($02 + 2$0, true)
1994 _ => (0, false) 2038 _ => (0, false)
1995 }; 2039 };
1996}", 2040}
1997 r" 2041"#,
2042 r#"
1998fn main() { 2043fn main() {
1999 let x = true; 2044 let x = true;
2000 let tuple = match x { 2045 let tuple = match x {
@@ -2005,7 +2050,8 @@ fn main() {
2005 2050
2006fn $0fun_name() -> i32 { 2051fn $0fun_name() -> i32 {
2007 2 + 2 2052 2 + 2
2008}", 2053}
2054"#,
2009 ); 2055 );
2010 } 2056 }
2011 2057
@@ -2013,18 +2059,20 @@ fn $0fun_name() -> i32 {
2013 fn param_from_closure() { 2059 fn param_from_closure() {
2014 check_assist( 2060 check_assist(
2015 extract_function, 2061 extract_function,
2016 r" 2062 r#"
2017fn main() { 2063fn main() {
2018 let lambda = |x: u32| $0x * 2$0; 2064 let lambda = |x: u32| $0x * 2$0;
2019}", 2065}
2020 r" 2066"#,
2067 r#"
2021fn main() { 2068fn main() {
2022 let lambda = |x: u32| fun_name(x); 2069 let lambda = |x: u32| fun_name(x);
2023} 2070}
2024 2071
2025fn $0fun_name(x: u32) -> u32 { 2072fn $0fun_name(x: u32) -> u32 {
2026 x * 2 2073 x * 2
2027}", 2074}
2075"#,
2028 ); 2076 );
2029 } 2077 }
2030 2078
@@ -2032,18 +2080,20 @@ fn $0fun_name(x: u32) -> u32 {
2032 fn extract_return_stmt() { 2080 fn extract_return_stmt() {
2033 check_assist( 2081 check_assist(
2034 extract_function, 2082 extract_function,
2035 r" 2083 r#"
2036fn foo() -> u32 { 2084fn foo() -> u32 {
2037 $0return 2 + 2$0; 2085 $0return 2 + 2$0;
2038}", 2086}
2039 r" 2087"#,
2088 r#"
2040fn foo() -> u32 { 2089fn foo() -> u32 {
2041 return fun_name(); 2090 return fun_name();
2042} 2091}
2043 2092
2044fn $0fun_name() -> u32 { 2093fn $0fun_name() -> u32 {
2045 2 + 2 2094 2 + 2
2046}", 2095}
2096"#,
2047 ); 2097 );
2048 } 2098 }
2049 2099
@@ -2051,13 +2101,14 @@ fn $0fun_name() -> u32 {
2051 fn does_not_add_extra_whitespace() { 2101 fn does_not_add_extra_whitespace() {
2052 check_assist( 2102 check_assist(
2053 extract_function, 2103 extract_function,
2054 r" 2104 r#"
2055fn foo() -> u32 { 2105fn foo() -> u32 {
2056 2106
2057 2107
2058 $0return 2 + 2$0; 2108 $0return 2 + 2$0;
2059}", 2109}
2060 r" 2110"#,
2111 r#"
2061fn foo() -> u32 { 2112fn foo() -> u32 {
2062 2113
2063 2114
@@ -2066,7 +2117,8 @@ fn foo() -> u32 {
2066 2117
2067fn $0fun_name() -> u32 { 2118fn $0fun_name() -> u32 {
2068 2 + 2 2119 2 + 2
2069}", 2120}
2121"#,
2070 ); 2122 );
2071 } 2123 }
2072 2124
@@ -2074,13 +2126,14 @@ fn $0fun_name() -> u32 {
2074 fn break_stmt() { 2126 fn break_stmt() {
2075 check_assist( 2127 check_assist(
2076 extract_function, 2128 extract_function,
2077 r" 2129 r#"
2078fn main() { 2130fn main() {
2079 let result = loop { 2131 let result = loop {
2080 $0break 2 + 2$0; 2132 $0break 2 + 2$0;
2081 }; 2133 };
2082}", 2134}
2083 r" 2135"#,
2136 r#"
2084fn main() { 2137fn main() {
2085 let result = loop { 2138 let result = loop {
2086 break fun_name(); 2139 break fun_name();
@@ -2089,7 +2142,8 @@ fn main() {
2089 2142
2090fn $0fun_name() -> i32 { 2143fn $0fun_name() -> i32 {
2091 2 + 2 2144 2 + 2
2092}", 2145}
2146"#,
2093 ); 2147 );
2094 } 2148 }
2095 2149
@@ -2097,18 +2151,20 @@ fn $0fun_name() -> i32 {
2097 fn extract_cast() { 2151 fn extract_cast() {
2098 check_assist( 2152 check_assist(
2099 extract_function, 2153 extract_function,
2100 r" 2154 r#"
2101fn main() { 2155fn main() {
2102 let v = $00f32 as u32$0; 2156 let v = $00f32 as u32$0;
2103}", 2157}
2104 r" 2158"#,
2159 r#"
2105fn main() { 2160fn main() {
2106 let v = fun_name(); 2161 let v = fun_name();
2107} 2162}
2108 2163
2109fn $0fun_name() -> u32 { 2164fn $0fun_name() -> u32 {
2110 0f32 as u32 2165 0f32 as u32
2111}", 2166}
2167"#,
2112 ); 2168 );
2113 } 2169 }
2114 2170
@@ -2121,15 +2177,16 @@ fn $0fun_name() -> u32 {
2121 fn method_to_freestanding() { 2177 fn method_to_freestanding() {
2122 check_assist( 2178 check_assist(
2123 extract_function, 2179 extract_function,
2124 r" 2180 r#"
2125struct S; 2181struct S;
2126 2182
2127impl S { 2183impl S {
2128 fn foo(&self) -> i32 { 2184 fn foo(&self) -> i32 {
2129 $01+1$0 2185 $01+1$0
2130 } 2186 }
2131}", 2187}
2132 r" 2188"#,
2189 r#"
2133struct S; 2190struct S;
2134 2191
2135impl S { 2192impl S {
@@ -2140,7 +2197,8 @@ impl S {
2140 2197
2141fn $0fun_name() -> i32 { 2198fn $0fun_name() -> i32 {
2142 1+1 2199 1+1
2143}", 2200}
2201"#,
2144 ); 2202 );
2145 } 2203 }
2146 2204
@@ -2148,15 +2206,16 @@ fn $0fun_name() -> i32 {
2148 fn method_with_reference() { 2206 fn method_with_reference() {
2149 check_assist( 2207 check_assist(
2150 extract_function, 2208 extract_function,
2151 r" 2209 r#"
2152struct S { f: i32 }; 2210struct S { f: i32 };
2153 2211
2154impl S { 2212impl S {
2155 fn foo(&self) -> i32 { 2213 fn foo(&self) -> i32 {
2156 $01+self.f$0 2214 $01+self.f$0
2157 } 2215 }
2158}", 2216}
2159 r" 2217"#,
2218 r#"
2160struct S { f: i32 }; 2219struct S { f: i32 };
2161 2220
2162impl S { 2221impl S {
@@ -2167,7 +2226,8 @@ impl S {
2167 fn $0fun_name(&self) -> i32 { 2226 fn $0fun_name(&self) -> i32 {
2168 1+self.f 2227 1+self.f
2169 } 2228 }
2170}", 2229}
2230"#,
2171 ); 2231 );
2172 } 2232 }
2173 2233
@@ -2175,15 +2235,16 @@ impl S {
2175 fn method_with_mut() { 2235 fn method_with_mut() {
2176 check_assist( 2236 check_assist(
2177 extract_function, 2237 extract_function,
2178 r" 2238 r#"
2179struct S { f: i32 }; 2239struct S { f: i32 };
2180 2240
2181impl S { 2241impl S {
2182 fn foo(&mut self) { 2242 fn foo(&mut self) {
2183 $0self.f += 1;$0 2243 $0self.f += 1;$0
2184 } 2244 }
2185}", 2245}
2186 r" 2246"#,
2247 r#"
2187struct S { f: i32 }; 2248struct S { f: i32 };
2188 2249
2189impl S { 2250impl S {
@@ -2194,7 +2255,8 @@ impl S {
2194 fn $0fun_name(&mut self) { 2255 fn $0fun_name(&mut self) {
2195 self.f += 1; 2256 self.f += 1;
2196 } 2257 }
2197}", 2258}
2259"#,
2198 ); 2260 );
2199 } 2261 }
2200 2262
@@ -2202,13 +2264,14 @@ impl S {
2202 fn variable_defined_inside_and_used_after_no_ret() { 2264 fn variable_defined_inside_and_used_after_no_ret() {
2203 check_assist( 2265 check_assist(
2204 extract_function, 2266 extract_function,
2205 r" 2267 r#"
2206fn foo() { 2268fn foo() {
2207 let n = 1; 2269 let n = 1;
2208 $0let k = n * n;$0 2270 $0let k = n * n;$0
2209 let m = k + 1; 2271 let m = k + 1;
2210}", 2272}
2211 r" 2273"#,
2274 r#"
2212fn foo() { 2275fn foo() {
2213 let n = 1; 2276 let n = 1;
2214 let k = fun_name(n); 2277 let k = fun_name(n);
@@ -2218,7 +2281,8 @@ fn foo() {
2218fn $0fun_name(n: i32) -> i32 { 2281fn $0fun_name(n: i32) -> i32 {
2219 let k = n * n; 2282 let k = n * n;
2220 k 2283 k
2221}", 2284}
2285"#,
2222 ); 2286 );
2223 } 2287 }
2224 2288
@@ -2226,13 +2290,14 @@ fn $0fun_name(n: i32) -> i32 {
2226 fn variable_defined_inside_and_used_after_mutably_no_ret() { 2290 fn variable_defined_inside_and_used_after_mutably_no_ret() {
2227 check_assist( 2291 check_assist(
2228 extract_function, 2292 extract_function,
2229 r" 2293 r#"
2230fn foo() { 2294fn foo() {
2231 let n = 1; 2295 let n = 1;
2232 $0let mut k = n * n;$0 2296 $0let mut k = n * n;$0
2233 k += 1; 2297 k += 1;
2234}", 2298}
2235 r" 2299"#,
2300 r#"
2236fn foo() { 2301fn foo() {
2237 let n = 1; 2302 let n = 1;
2238 let mut k = fun_name(n); 2303 let mut k = fun_name(n);
@@ -2242,7 +2307,8 @@ fn foo() {
2242fn $0fun_name(n: i32) -> i32 { 2307fn $0fun_name(n: i32) -> i32 {
2243 let mut k = n * n; 2308 let mut k = n * n;
2244 k 2309 k
2245}", 2310}
2311"#,
2246 ); 2312 );
2247 } 2313 }
2248 2314
@@ -2250,14 +2316,15 @@ fn $0fun_name(n: i32) -> i32 {
2250 fn two_variables_defined_inside_and_used_after_no_ret() { 2316 fn two_variables_defined_inside_and_used_after_no_ret() {
2251 check_assist( 2317 check_assist(
2252 extract_function, 2318 extract_function,
2253 r" 2319 r#"
2254fn foo() { 2320fn foo() {
2255 let n = 1; 2321 let n = 1;
2256 $0let k = n * n; 2322 $0let k = n * n;
2257 let m = k + 2;$0 2323 let m = k + 2;$0
2258 let h = k + m; 2324 let h = k + m;
2259}", 2325}
2260 r" 2326"#,
2327 r#"
2261fn foo() { 2328fn foo() {
2262 let n = 1; 2329 let n = 1;
2263 let (k, m) = fun_name(n); 2330 let (k, m) = fun_name(n);
@@ -2268,7 +2335,8 @@ fn $0fun_name(n: i32) -> (i32, i32) {
2268 let k = n * n; 2335 let k = n * n;
2269 let m = k + 2; 2336 let m = k + 2;
2270 (k, m) 2337 (k, m)
2271}", 2338}
2339"#,
2272 ); 2340 );
2273 } 2341 }
2274 2342
@@ -2276,7 +2344,7 @@ fn $0fun_name(n: i32) -> (i32, i32) {
2276 fn multi_variables_defined_inside_and_used_after_mutably_no_ret() { 2344 fn multi_variables_defined_inside_and_used_after_mutably_no_ret() {
2277 check_assist( 2345 check_assist(
2278 extract_function, 2346 extract_function,
2279 r" 2347 r#"
2280fn foo() { 2348fn foo() {
2281 let n = 1; 2349 let n = 1;
2282 $0let mut k = n * n; 2350 $0let mut k = n * n;
@@ -2285,8 +2353,9 @@ fn foo() {
2285 o += 1;$0 2353 o += 1;$0
2286 k += o; 2354 k += o;
2287 m = 1; 2355 m = 1;
2288}", 2356}
2289 r" 2357"#,
2358 r#"
2290fn foo() { 2359fn foo() {
2291 let n = 1; 2360 let n = 1;
2292 let (mut k, mut m, o) = fun_name(n); 2361 let (mut k, mut m, o) = fun_name(n);
@@ -2300,7 +2369,8 @@ fn $0fun_name(n: i32) -> (i32, i32, i32) {
2300 let mut o = m + 3; 2369 let mut o = m + 3;
2301 o += 1; 2370 o += 1;
2302 (k, m, o) 2371 (k, m, o)
2303}", 2372}
2373"#,
2304 ); 2374 );
2305 } 2375 }
2306 2376
@@ -2308,13 +2378,14 @@ fn $0fun_name(n: i32) -> (i32, i32, i32) {
2308 fn nontrivial_patterns_define_variables() { 2378 fn nontrivial_patterns_define_variables() {
2309 check_assist( 2379 check_assist(
2310 extract_function, 2380 extract_function,
2311 r" 2381 r#"
2312struct Counter(i32); 2382struct Counter(i32);
2313fn foo() { 2383fn foo() {
2314 $0let Counter(n) = Counter(0);$0 2384 $0let Counter(n) = Counter(0);$0
2315 let m = n; 2385 let m = n;
2316}", 2386}
2317 r" 2387"#,
2388 r#"
2318struct Counter(i32); 2389struct Counter(i32);
2319fn foo() { 2390fn foo() {
2320 let n = fun_name(); 2391 let n = fun_name();
@@ -2324,7 +2395,8 @@ fn foo() {
2324fn $0fun_name() -> i32 { 2395fn $0fun_name() -> i32 {
2325 let Counter(n) = Counter(0); 2396 let Counter(n) = Counter(0);
2326 n 2397 n
2327}", 2398}
2399"#,
2328 ); 2400 );
2329 } 2401 }
2330 2402
@@ -2332,13 +2404,14 @@ fn $0fun_name() -> i32 {
2332 fn struct_with_two_fields_pattern_define_variables() { 2404 fn struct_with_two_fields_pattern_define_variables() {
2333 check_assist( 2405 check_assist(
2334 extract_function, 2406 extract_function,
2335 r" 2407 r#"
2336struct Counter { n: i32, m: i32 }; 2408struct Counter { n: i32, m: i32 };
2337fn foo() { 2409fn foo() {
2338 $0let Counter { n, m: k } = Counter { n: 1, m: 2 };$0 2410 $0let Counter { n, m: k } = Counter { n: 1, m: 2 };$0
2339 let h = n + k; 2411 let h = n + k;
2340}", 2412}
2341 r" 2413"#,
2414 r#"
2342struct Counter { n: i32, m: i32 }; 2415struct Counter { n: i32, m: i32 };
2343fn foo() { 2416fn foo() {
2344 let (n, k) = fun_name(); 2417 let (n, k) = fun_name();
@@ -2348,7 +2421,8 @@ fn foo() {
2348fn $0fun_name() -> (i32, i32) { 2421fn $0fun_name() -> (i32, i32) {
2349 let Counter { n, m: k } = Counter { n: 1, m: 2 }; 2422 let Counter { n, m: k } = Counter { n: 1, m: 2 };
2350 (n, k) 2423 (n, k)
2351}", 2424}
2425"#,
2352 ); 2426 );
2353 } 2427 }
2354 2428
@@ -2356,13 +2430,14 @@ fn $0fun_name() -> (i32, i32) {
2356 fn mut_var_from_outer_scope() { 2430 fn mut_var_from_outer_scope() {
2357 check_assist( 2431 check_assist(
2358 extract_function, 2432 extract_function,
2359 r" 2433 r#"
2360fn foo() { 2434fn foo() {
2361 let mut n = 1; 2435 let mut n = 1;
2362 $0n += 1;$0 2436 $0n += 1;$0
2363 let m = n + 1; 2437 let m = n + 1;
2364}", 2438}
2365 r" 2439"#,
2440 r#"
2366fn foo() { 2441fn foo() {
2367 let mut n = 1; 2442 let mut n = 1;
2368 fun_name(&mut n); 2443 fun_name(&mut n);
@@ -2371,7 +2446,8 @@ fn foo() {
2371 2446
2372fn $0fun_name(n: &mut i32) { 2447fn $0fun_name(n: &mut i32) {
2373 *n += 1; 2448 *n += 1;
2374}", 2449}
2450"#,
2375 ); 2451 );
2376 } 2452 }
2377 2453
@@ -2379,14 +2455,15 @@ fn $0fun_name(n: &mut i32) {
2379 fn mut_field_from_outer_scope() { 2455 fn mut_field_from_outer_scope() {
2380 check_assist( 2456 check_assist(
2381 extract_function, 2457 extract_function,
2382 r" 2458 r#"
2383struct C { n: i32 } 2459struct C { n: i32 }
2384fn foo() { 2460fn foo() {
2385 let mut c = C { n: 0 }; 2461 let mut c = C { n: 0 };
2386 $0c.n += 1;$0 2462 $0c.n += 1;$0
2387 let m = c.n + 1; 2463 let m = c.n + 1;
2388}", 2464}
2389 r" 2465"#,
2466 r#"
2390struct C { n: i32 } 2467struct C { n: i32 }
2391fn foo() { 2468fn foo() {
2392 let mut c = C { n: 0 }; 2469 let mut c = C { n: 0 };
@@ -2396,7 +2473,8 @@ fn foo() {
2396 2473
2397fn $0fun_name(c: &mut C) { 2474fn $0fun_name(c: &mut C) {
2398 c.n += 1; 2475 c.n += 1;
2399}", 2476}
2477"#,
2400 ); 2478 );
2401 } 2479 }
2402 2480
@@ -2404,7 +2482,7 @@ fn $0fun_name(c: &mut C) {
2404 fn mut_nested_field_from_outer_scope() { 2482 fn mut_nested_field_from_outer_scope() {
2405 check_assist( 2483 check_assist(
2406 extract_function, 2484 extract_function,
2407 r" 2485 r#"
2408struct P { n: i32} 2486struct P { n: i32}
2409struct C { p: P } 2487struct C { p: P }
2410fn foo() { 2488fn foo() {
@@ -2414,8 +2492,9 @@ fn foo() {
2414 $0c.p.n += u.p.n; 2492 $0c.p.n += u.p.n;
2415 let r = &mut v.p.n;$0 2493 let r = &mut v.p.n;$0
2416 let m = c.p.n + v.p.n + u.p.n; 2494 let m = c.p.n + v.p.n + u.p.n;
2417}", 2495}
2418 r" 2496"#,
2497 r#"
2419struct P { n: i32} 2498struct P { n: i32}
2420struct C { p: P } 2499struct C { p: P }
2421fn foo() { 2500fn foo() {
@@ -2429,7 +2508,8 @@ fn foo() {
2429fn $0fun_name(c: &mut C, u: &C, v: &mut C) { 2508fn $0fun_name(c: &mut C, u: &C, v: &mut C) {
2430 c.p.n += u.p.n; 2509 c.p.n += u.p.n;
2431 let r = &mut v.p.n; 2510 let r = &mut v.p.n;
2432}", 2511}
2512"#,
2433 ); 2513 );
2434 } 2514 }
2435 2515
@@ -2437,7 +2517,7 @@ fn $0fun_name(c: &mut C, u: &C, v: &mut C) {
2437 fn mut_param_many_usages_stmt() { 2517 fn mut_param_many_usages_stmt() {
2438 check_assist( 2518 check_assist(
2439 extract_function, 2519 extract_function,
2440 r" 2520 r#"
2441fn bar(k: i32) {} 2521fn bar(k: i32) {}
2442trait I: Copy { 2522trait I: Copy {
2443 fn succ(&self) -> Self; 2523 fn succ(&self) -> Self;
@@ -2458,8 +2538,9 @@ fn foo() {
2458 *v = v.succ(); 2538 *v = v.succ();
2459 n.succ();$0 2539 n.succ();$0
2460 let m = n + 1; 2540 let m = n + 1;
2461}", 2541}
2462 r" 2542"#,
2543 r#"
2463fn bar(k: i32) {} 2544fn bar(k: i32) {}
2464trait I: Copy { 2545trait I: Copy {
2465 fn succ(&self) -> Self; 2546 fn succ(&self) -> Self;
@@ -2484,7 +2565,8 @@ fn $0fun_name(n: &mut i32) {
2484 let v = n; 2565 let v = n;
2485 *v = v.succ(); 2566 *v = v.succ();
2486 n.succ(); 2567 n.succ();
2487}", 2568}
2569"#,
2488 ); 2570 );
2489 } 2571 }
2490 2572
@@ -2492,7 +2574,7 @@ fn $0fun_name(n: &mut i32) {
2492 fn mut_param_many_usages_expr() { 2574 fn mut_param_many_usages_expr() {
2493 check_assist( 2575 check_assist(
2494 extract_function, 2576 extract_function,
2495 r" 2577 r#"
2496fn bar(k: i32) {} 2578fn bar(k: i32) {}
2497trait I: Copy { 2579trait I: Copy {
2498 fn succ(&self) -> Self; 2580 fn succ(&self) -> Self;
@@ -2515,8 +2597,9 @@ fn foo() {
2515 n.succ(); 2597 n.succ();
2516 }$0 2598 }$0
2517 let m = n + 1; 2599 let m = n + 1;
2518}", 2600}
2519 r" 2601"#,
2602 r#"
2520fn bar(k: i32) {} 2603fn bar(k: i32) {}
2521trait I: Copy { 2604trait I: Copy {
2522 fn succ(&self) -> Self; 2605 fn succ(&self) -> Self;
@@ -2541,7 +2624,8 @@ fn $0fun_name(n: &mut i32) {
2541 let v = n; 2624 let v = n;
2542 *v = v.succ(); 2625 *v = v.succ();
2543 n.succ(); 2626 n.succ();
2544}", 2627}
2628"#,
2545 ); 2629 );
2546 } 2630 }
2547 2631
@@ -2549,11 +2633,12 @@ fn $0fun_name(n: &mut i32) {
2549 fn mut_param_by_value() { 2633 fn mut_param_by_value() {
2550 check_assist( 2634 check_assist(
2551 extract_function, 2635 extract_function,
2552 r" 2636 r#"
2553fn foo() { 2637fn foo() {
2554 let mut n = 1; 2638 let mut n = 1;
2555 $0n += 1;$0 2639 $0n += 1;$0
2556}", 2640}
2641"#,
2557 r" 2642 r"
2558fn foo() { 2643fn foo() {
2559 let mut n = 1; 2644 let mut n = 1;
@@ -2562,7 +2647,8 @@ fn foo() {
2562 2647
2563fn $0fun_name(mut n: i32) { 2648fn $0fun_name(mut n: i32) {
2564 n += 1; 2649 n += 1;
2565}", 2650}
2651",
2566 ); 2652 );
2567 } 2653 }
2568 2654
@@ -2570,14 +2656,15 @@ fn $0fun_name(mut n: i32) {
2570 fn mut_param_because_of_mut_ref() { 2656 fn mut_param_because_of_mut_ref() {
2571 check_assist( 2657 check_assist(
2572 extract_function, 2658 extract_function,
2573 r" 2659 r#"
2574fn foo() { 2660fn foo() {
2575 let mut n = 1; 2661 let mut n = 1;
2576 $0let v = &mut n; 2662 $0let v = &mut n;
2577 *v += 1;$0 2663 *v += 1;$0
2578 let k = n; 2664 let k = n;
2579}", 2665}
2580 r" 2666"#,
2667 r#"
2581fn foo() { 2668fn foo() {
2582 let mut n = 1; 2669 let mut n = 1;
2583 fun_name(&mut n); 2670 fun_name(&mut n);
@@ -2587,7 +2674,8 @@ fn foo() {
2587fn $0fun_name(n: &mut i32) { 2674fn $0fun_name(n: &mut i32) {
2588 let v = n; 2675 let v = n;
2589 *v += 1; 2676 *v += 1;
2590}", 2677}
2678"#,
2591 ); 2679 );
2592 } 2680 }
2593 2681
@@ -2600,8 +2688,9 @@ fn foo() {
2600 let mut n = 1; 2688 let mut n = 1;
2601 $0let v = &mut n; 2689 $0let v = &mut n;
2602 *v += 1;$0 2690 *v += 1;$0
2603}", 2691}
2604 r" 2692",
2693 r#"
2605fn foo() { 2694fn foo() {
2606 let mut n = 1; 2695 let mut n = 1;
2607 fun_name(n); 2696 fun_name(n);
@@ -2610,7 +2699,8 @@ fn foo() {
2610fn $0fun_name(mut n: i32) { 2699fn $0fun_name(mut n: i32) {
2611 let v = &mut n; 2700 let v = &mut n;
2612 *v += 1; 2701 *v += 1;
2613}", 2702}
2703"#,
2614 ); 2704 );
2615 } 2705 }
2616 2706
@@ -2618,7 +2708,7 @@ fn $0fun_name(mut n: i32) {
2618 fn mut_method_call() { 2708 fn mut_method_call() {
2619 check_assist( 2709 check_assist(
2620 extract_function, 2710 extract_function,
2621 r" 2711 r#"
2622trait I { 2712trait I {
2623 fn inc(&mut self); 2713 fn inc(&mut self);
2624} 2714}
@@ -2628,8 +2718,9 @@ impl I for i32 {
2628fn foo() { 2718fn foo() {
2629 let mut n = 1; 2719 let mut n = 1;
2630 $0n.inc();$0 2720 $0n.inc();$0
2631}", 2721}
2632 r" 2722"#,
2723 r#"
2633trait I { 2724trait I {
2634 fn inc(&mut self); 2725 fn inc(&mut self);
2635} 2726}
@@ -2643,7 +2734,8 @@ fn foo() {
2643 2734
2644fn $0fun_name(mut n: i32) { 2735fn $0fun_name(mut n: i32) {
2645 n.inc(); 2736 n.inc();
2646}", 2737}
2738"#,
2647 ); 2739 );
2648 } 2740 }
2649 2741
@@ -2651,7 +2743,7 @@ fn $0fun_name(mut n: i32) {
2651 fn shared_method_call() { 2743 fn shared_method_call() {
2652 check_assist( 2744 check_assist(
2653 extract_function, 2745 extract_function,
2654 r" 2746 r#"
2655trait I { 2747trait I {
2656 fn succ(&self); 2748 fn succ(&self);
2657} 2749}
@@ -2661,7 +2753,8 @@ impl I for i32 {
2661fn foo() { 2753fn foo() {
2662 let mut n = 1; 2754 let mut n = 1;
2663 $0n.succ();$0 2755 $0n.succ();$0
2664}", 2756}
2757"#,
2665 r" 2758 r"
2666trait I { 2759trait I {
2667 fn succ(&self); 2760 fn succ(&self);
@@ -2676,7 +2769,8 @@ fn foo() {
2676 2769
2677fn $0fun_name(n: i32) { 2770fn $0fun_name(n: i32) {
2678 n.succ(); 2771 n.succ();
2679}", 2772}
2773",
2680 ); 2774 );
2681 } 2775 }
2682 2776
@@ -2684,7 +2778,7 @@ fn $0fun_name(n: i32) {
2684 fn mut_method_call_with_other_receiver() { 2778 fn mut_method_call_with_other_receiver() {
2685 check_assist( 2779 check_assist(
2686 extract_function, 2780 extract_function,
2687 r" 2781 r#"
2688trait I { 2782trait I {
2689 fn inc(&mut self, n: i32); 2783 fn inc(&mut self, n: i32);
2690} 2784}
@@ -2695,7 +2789,8 @@ fn foo() {
2695 let mut n = 1; 2789 let mut n = 1;
2696 $0let mut m = 2; 2790 $0let mut m = 2;
2697 m.inc(n);$0 2791 m.inc(n);$0
2698}", 2792}
2793"#,
2699 r" 2794 r"
2700trait I { 2795trait I {
2701 fn inc(&mut self, n: i32); 2796 fn inc(&mut self, n: i32);
@@ -2711,7 +2806,8 @@ fn foo() {
2711fn $0fun_name(n: i32) { 2806fn $0fun_name(n: i32) {
2712 let mut m = 2; 2807 let mut m = 2;
2713 m.inc(n); 2808 m.inc(n);
2714}", 2809}
2810",
2715 ); 2811 );
2716 } 2812 }
2717 2813
@@ -2719,12 +2815,13 @@ fn $0fun_name(n: i32) {
2719 fn non_copy_without_usages_after() { 2815 fn non_copy_without_usages_after() {
2720 check_assist( 2816 check_assist(
2721 extract_function, 2817 extract_function,
2722 r" 2818 r#"
2723struct Counter(i32); 2819struct Counter(i32);
2724fn foo() { 2820fn foo() {
2725 let c = Counter(0); 2821 let c = Counter(0);
2726 $0let n = c.0;$0 2822 $0let n = c.0;$0
2727}", 2823}
2824"#,
2728 r" 2825 r"
2729struct Counter(i32); 2826struct Counter(i32);
2730fn foo() { 2827fn foo() {
@@ -2734,7 +2831,8 @@ fn foo() {
2734 2831
2735fn $0fun_name(c: Counter) { 2832fn $0fun_name(c: Counter) {
2736 let n = c.0; 2833 let n = c.0;
2737}", 2834}
2835",
2738 ); 2836 );
2739 } 2837 }
2740 2838
@@ -2748,8 +2846,9 @@ fn foo() {
2748 let c = Counter(0); 2846 let c = Counter(0);
2749 $0let n = c.0;$0 2847 $0let n = c.0;$0
2750 let m = c.0; 2848 let m = c.0;
2751}", 2849}
2752 r" 2850",
2851 r#"
2753struct Counter(i32); 2852struct Counter(i32);
2754fn foo() { 2853fn foo() {
2755 let c = Counter(0); 2854 let c = Counter(0);
@@ -2759,7 +2858,8 @@ fn foo() {
2759 2858
2760fn $0fun_name(c: &Counter) { 2859fn $0fun_name(c: &Counter) {
2761 let n = c.0; 2860 let n = c.0;
2762}", 2861}
2862"#,
2763 ); 2863 );
2764 } 2864 }
2765 2865
@@ -2767,19 +2867,15 @@ fn $0fun_name(c: &Counter) {
2767 fn copy_used_after() { 2867 fn copy_used_after() {
2768 check_assist( 2868 check_assist(
2769 extract_function, 2869 extract_function,
2770 r##" 2870 r#"
2771#[lang = "copy"] 2871//- minicore: copy
2772pub trait Copy {}
2773impl Copy for i32 {}
2774fn foo() { 2872fn foo() {
2775 let n = 0; 2873 let n = 0;
2776 $0let m = n;$0 2874 $0let m = n;$0
2777 let k = n; 2875 let k = n;
2778}"##, 2876}
2779 r##" 2877"#,
2780#[lang = "copy"] 2878 r#"
2781pub trait Copy {}
2782impl Copy for i32 {}
2783fn foo() { 2879fn foo() {
2784 let n = 0; 2880 let n = 0;
2785 fun_name(n); 2881 fun_name(n);
@@ -2788,7 +2884,8 @@ fn foo() {
2788 2884
2789fn $0fun_name(n: i32) { 2885fn $0fun_name(n: i32) {
2790 let m = n; 2886 let m = n;
2791}"##, 2887}
2888"#,
2792 ) 2889 )
2793 } 2890 }
2794 2891
@@ -2796,21 +2893,19 @@ fn $0fun_name(n: i32) {
2796 fn copy_custom_used_after() { 2893 fn copy_custom_used_after() {
2797 check_assist( 2894 check_assist(
2798 extract_function, 2895 extract_function,
2799 r##" 2896 r#"
2800#[lang = "copy"] 2897//- minicore: copy, derive
2801pub trait Copy {} 2898#[derive(Clone, Copy)]
2802struct Counter(i32); 2899struct Counter(i32);
2803impl Copy for Counter {}
2804fn foo() { 2900fn foo() {
2805 let c = Counter(0); 2901 let c = Counter(0);
2806 $0let n = c.0;$0 2902 $0let n = c.0;$0
2807 let m = c.0; 2903 let m = c.0;
2808}"##, 2904}
2809 r##" 2905"#,
2810#[lang = "copy"] 2906 r#"
2811pub trait Copy {} 2907#[derive(Clone, Copy)]
2812struct Counter(i32); 2908struct Counter(i32);
2813impl Copy for Counter {}
2814fn foo() { 2909fn foo() {
2815 let c = Counter(0); 2910 let c = Counter(0);
2816 fun_name(c); 2911 fun_name(c);
@@ -2819,7 +2914,8 @@ fn foo() {
2819 2914
2820fn $0fun_name(c: Counter) { 2915fn $0fun_name(c: Counter) {
2821 let n = c.0; 2916 let n = c.0;
2822}"##, 2917}
2918"#,
2823 ); 2919 );
2824 } 2920 }
2825 2921
@@ -2827,7 +2923,7 @@ fn $0fun_name(c: Counter) {
2827 fn indented_stmts() { 2923 fn indented_stmts() {
2828 check_assist( 2924 check_assist(
2829 extract_function, 2925 extract_function,
2830 r" 2926 r#"
2831fn foo() { 2927fn foo() {
2832 if true { 2928 if true {
2833 loop { 2929 loop {
@@ -2835,8 +2931,9 @@ fn foo() {
2835 let m = 2;$0 2931 let m = 2;$0
2836 } 2932 }
2837 } 2933 }
2838}", 2934}
2839 r" 2935"#,
2936 r#"
2840fn foo() { 2937fn foo() {
2841 if true { 2938 if true {
2842 loop { 2939 loop {
@@ -2848,7 +2945,8 @@ fn foo() {
2848fn $0fun_name() { 2945fn $0fun_name() {
2849 let n = 1; 2946 let n = 1;
2850 let m = 2; 2947 let m = 2;
2851}", 2948}
2949"#,
2852 ); 2950 );
2853 } 2951 }
2854 2952
@@ -2856,7 +2954,7 @@ fn $0fun_name() {
2856 fn indented_stmts_inside_mod() { 2954 fn indented_stmts_inside_mod() {
2857 check_assist( 2955 check_assist(
2858 extract_function, 2956 extract_function,
2859 r" 2957 r#"
2860mod bar { 2958mod bar {
2861 fn foo() { 2959 fn foo() {
2862 if true { 2960 if true {
@@ -2866,8 +2964,9 @@ mod bar {
2866 } 2964 }
2867 } 2965 }
2868 } 2966 }
2869}", 2967}
2870 r" 2968"#,
2969 r#"
2871mod bar { 2970mod bar {
2872 fn foo() { 2971 fn foo() {
2873 if true { 2972 if true {
@@ -2881,7 +2980,8 @@ mod bar {
2881 let n = 1; 2980 let n = 1;
2882 let m = 2; 2981 let m = 2;
2883 } 2982 }
2884}", 2983}
2984"#,
2885 ); 2985 );
2886 } 2986 }
2887 2987
@@ -2889,12 +2989,8 @@ mod bar {
2889 fn break_loop() { 2989 fn break_loop() {
2890 check_assist( 2990 check_assist(
2891 extract_function, 2991 extract_function,
2892 r##" 2992 r#"
2893enum Option<T> { 2993//- minicore: option
2894 #[lang = "None"] None,
2895 #[lang = "Some"] Some(T),
2896}
2897use Option::*;
2898fn foo() { 2994fn foo() {
2899 loop { 2995 loop {
2900 let n = 1; 2996 let n = 1;
@@ -2903,13 +2999,9 @@ fn foo() {
2903 let k = 2;$0 2999 let k = 2;$0
2904 let h = 1 + k; 3000 let h = 1 + k;
2905 } 3001 }
2906}"##,
2907 r##"
2908enum Option<T> {
2909 #[lang = "None"] None,
2910 #[lang = "Some"] Some(T),
2911} 3002}
2912use Option::*; 3003"#,
3004 r#"
2913fn foo() { 3005fn foo() {
2914 loop { 3006 loop {
2915 let n = 1; 3007 let n = 1;
@@ -2926,7 +3018,8 @@ fn $0fun_name(n: i32) -> Option<i32> {
2926 return None; 3018 return None;
2927 let k = 2; 3019 let k = 2;
2928 Some(k) 3020 Some(k)
2929}"##, 3021}
3022"#,
2930 ); 3023 );
2931 } 3024 }
2932 3025
@@ -2934,31 +3027,17 @@ fn $0fun_name(n: i32) -> Option<i32> {
2934 fn return_to_parent() { 3027 fn return_to_parent() {
2935 check_assist( 3028 check_assist(
2936 extract_function, 3029 extract_function,
2937 r##" 3030 r#"
2938#[lang = "copy"] 3031//- minicore: copy, result
2939pub trait Copy {}
2940impl Copy for i32 {}
2941enum Result<T, E> {
2942 #[lang = "Ok"] Ok(T),
2943 #[lang = "Err"] Err(E),
2944}
2945use Result::*;
2946fn foo() -> i64 { 3032fn foo() -> i64 {
2947 let n = 1; 3033 let n = 1;
2948 $0let m = n + 1; 3034 $0let m = n + 1;
2949 return 1; 3035 return 1;
2950 let k = 2;$0 3036 let k = 2;$0
2951 (n + k) as i64 3037 (n + k) as i64
2952}"##, 3038}
2953 r##" 3039"#,
2954#[lang = "copy"] 3040 r#"
2955pub trait Copy {}
2956impl Copy for i32 {}
2957enum Result<T, E> {
2958 #[lang = "Ok"] Ok(T),
2959 #[lang = "Err"] Err(E),
2960}
2961use Result::*;
2962fn foo() -> i64 { 3041fn foo() -> i64 {
2963 let n = 1; 3042 let n = 1;
2964 let k = match fun_name(n) { 3043 let k = match fun_name(n) {
@@ -2973,7 +3052,8 @@ fn $0fun_name(n: i32) -> Result<i32, i64> {
2973 return Err(1); 3052 return Err(1);
2974 let k = 2; 3053 let k = 2;
2975 Ok(k) 3054 Ok(k)
2976}"##, 3055}
3056"#,
2977 ); 3057 );
2978 } 3058 }
2979 3059
@@ -2982,7 +3062,7 @@ fn $0fun_name(n: i32) -> Result<i32, i64> {
2982 cov_mark::check!(external_control_flow_break_and_continue); 3062 cov_mark::check!(external_control_flow_break_and_continue);
2983 check_assist_not_applicable( 3063 check_assist_not_applicable(
2984 extract_function, 3064 extract_function,
2985 r##" 3065 r#"
2986fn foo() { 3066fn foo() {
2987 loop { 3067 loop {
2988 let n = 1; 3068 let n = 1;
@@ -2993,7 +3073,8 @@ fn foo() {
2993 let k = k + 1;$0 3073 let k = k + 1;$0
2994 let r = n + k; 3074 let r = n + k;
2995 } 3075 }
2996}"##, 3076}
3077"#,
2997 ); 3078 );
2998 } 3079 }
2999 3080
@@ -3002,7 +3083,7 @@ fn foo() {
3002 cov_mark::check!(external_control_flow_return_and_bc); 3083 cov_mark::check!(external_control_flow_return_and_bc);
3003 check_assist_not_applicable( 3084 check_assist_not_applicable(
3004 extract_function, 3085 extract_function,
3005 r##" 3086 r#"
3006fn foo() { 3087fn foo() {
3007 loop { 3088 loop {
3008 let n = 1; 3089 let n = 1;
@@ -3013,7 +3094,8 @@ fn foo() {
3013 let k = k + 1;$0 3094 let k = k + 1;$0
3014 let r = n + k; 3095 let r = n + k;
3015 } 3096 }
3016}"##, 3097}
3098"#,
3017 ); 3099 );
3018 } 3100 }
3019 3101
@@ -3021,7 +3103,7 @@ fn foo() {
3021 fn break_loop_with_if() { 3103 fn break_loop_with_if() {
3022 check_assist( 3104 check_assist(
3023 extract_function, 3105 extract_function,
3024 r##" 3106 r#"
3025fn foo() { 3107fn foo() {
3026 loop { 3108 loop {
3027 let mut n = 1; 3109 let mut n = 1;
@@ -3030,8 +3112,9 @@ fn foo() {
3030 n += m;$0 3112 n += m;$0
3031 let h = 1 + n; 3113 let h = 1 + n;
3032 } 3114 }
3033}"##, 3115}
3034 r##" 3116"#,
3117 r#"
3035fn foo() { 3118fn foo() {
3036 loop { 3119 loop {
3037 let mut n = 1; 3120 let mut n = 1;
@@ -3047,7 +3130,8 @@ fn $0fun_name(n: &mut i32) -> bool {
3047 return true; 3130 return true;
3048 *n += m; 3131 *n += m;
3049 false 3132 false
3050}"##, 3133}
3134"#,
3051 ); 3135 );
3052 } 3136 }
3053 3137
@@ -3055,7 +3139,7 @@ fn $0fun_name(n: &mut i32) -> bool {
3055 fn break_loop_nested() { 3139 fn break_loop_nested() {
3056 check_assist( 3140 check_assist(
3057 extract_function, 3141 extract_function,
3058 r##" 3142 r#"
3059fn foo() { 3143fn foo() {
3060 loop { 3144 loop {
3061 let mut n = 1; 3145 let mut n = 1;
@@ -3065,8 +3149,9 @@ fn foo() {
3065 }$0 3149 }$0
3066 let h = 1; 3150 let h = 1;
3067 } 3151 }
3068}"##, 3152}
3069 r##" 3153"#,
3154 r#"
3070fn foo() { 3155fn foo() {
3071 loop { 3156 loop {
3072 let mut n = 1; 3157 let mut n = 1;
@@ -3083,7 +3168,8 @@ fn $0fun_name(n: i32) -> bool {
3083 return true; 3168 return true;
3084 } 3169 }
3085 false 3170 false
3086}"##, 3171}
3172"#,
3087 ); 3173 );
3088 } 3174 }
3089 3175
@@ -3091,7 +3177,7 @@ fn $0fun_name(n: i32) -> bool {
3091 fn return_from_nested_loop() { 3177 fn return_from_nested_loop() {
3092 check_assist( 3178 check_assist(
3093 extract_function, 3179 extract_function,
3094 r##" 3180 r#"
3095fn foo() { 3181fn foo() {
3096 loop { 3182 loop {
3097 let n = 1; 3183 let n = 1;
@@ -3103,8 +3189,9 @@ fn foo() {
3103 let m = k + 1;$0 3189 let m = k + 1;$0
3104 let h = 1 + m; 3190 let h = 1 + m;
3105 } 3191 }
3106}"##, 3192}
3107 r##" 3193"#,
3194 r#"
3108fn foo() { 3195fn foo() {
3109 loop { 3196 loop {
3110 let n = 1; 3197 let n = 1;
@@ -3123,7 +3210,8 @@ fn $0fun_name() -> Option<i32> {
3123 } 3210 }
3124 let m = k + 1; 3211 let m = k + 1;
3125 Some(m) 3212 Some(m)
3126}"##, 3213}
3214"#,
3127 ); 3215 );
3128 } 3216 }
3129 3217
@@ -3131,7 +3219,7 @@ fn $0fun_name() -> Option<i32> {
3131 fn break_from_nested_loop() { 3219 fn break_from_nested_loop() {
3132 check_assist( 3220 check_assist(
3133 extract_function, 3221 extract_function,
3134 r##" 3222 r#"
3135fn foo() { 3223fn foo() {
3136 loop { 3224 loop {
3137 let n = 1; 3225 let n = 1;
@@ -3142,8 +3230,9 @@ fn foo() {
3142 let m = k + 1;$0 3230 let m = k + 1;$0
3143 let h = 1 + m; 3231 let h = 1 + m;
3144 } 3232 }
3145}"##, 3233}
3146 r##" 3234"#,
3235 r#"
3147fn foo() { 3236fn foo() {
3148 loop { 3237 loop {
3149 let n = 1; 3238 let n = 1;
@@ -3159,7 +3248,8 @@ fn $0fun_name() -> i32 {
3159 } 3248 }
3160 let m = k + 1; 3249 let m = k + 1;
3161 m 3250 m
3162}"##, 3251}
3252"#,
3163 ); 3253 );
3164 } 3254 }
3165 3255
@@ -3167,7 +3257,7 @@ fn $0fun_name() -> i32 {
3167 fn break_from_nested_and_outer_loops() { 3257 fn break_from_nested_and_outer_loops() {
3168 check_assist( 3258 check_assist(
3169 extract_function, 3259 extract_function,
3170 r##" 3260 r#"
3171fn foo() { 3261fn foo() {
3172 loop { 3262 loop {
3173 let n = 1; 3263 let n = 1;
@@ -3181,8 +3271,9 @@ fn foo() {
3181 let m = k + 1;$0 3271 let m = k + 1;$0
3182 let h = 1 + m; 3272 let h = 1 + m;
3183 } 3273 }
3184}"##, 3274}
3185 r##" 3275"#,
3276 r#"
3186fn foo() { 3277fn foo() {
3187 loop { 3278 loop {
3188 let n = 1; 3279 let n = 1;
@@ -3204,7 +3295,8 @@ fn $0fun_name() -> Option<i32> {
3204 } 3295 }
3205 let m = k + 1; 3296 let m = k + 1;
3206 Some(m) 3297 Some(m)
3207}"##, 3298}
3299"#,
3208 ); 3300 );
3209 } 3301 }
3210 3302
@@ -3212,7 +3304,7 @@ fn $0fun_name() -> Option<i32> {
3212 fn return_from_nested_fn() { 3304 fn return_from_nested_fn() {
3213 check_assist( 3305 check_assist(
3214 extract_function, 3306 extract_function,
3215 r##" 3307 r#"
3216fn foo() { 3308fn foo() {
3217 loop { 3309 loop {
3218 let n = 1; 3310 let n = 1;
@@ -3223,8 +3315,9 @@ fn foo() {
3223 let m = k + 1;$0 3315 let m = k + 1;$0
3224 let h = 1 + m; 3316 let h = 1 + m;
3225 } 3317 }
3226}"##, 3318}
3227 r##" 3319"#,
3320 r#"
3228fn foo() { 3321fn foo() {
3229 loop { 3322 loop {
3230 let n = 1; 3323 let n = 1;
@@ -3240,7 +3333,8 @@ fn $0fun_name() -> i32 {
3240 } 3333 }
3241 let m = k + 1; 3334 let m = k + 1;
3242 m 3335 m
3243}"##, 3336}
3337"#,
3244 ); 3338 );
3245 } 3339 }
3246 3340
@@ -3248,7 +3342,7 @@ fn $0fun_name() -> i32 {
3248 fn break_with_value() { 3342 fn break_with_value() {
3249 check_assist( 3343 check_assist(
3250 extract_function, 3344 extract_function,
3251 r##" 3345 r#"
3252fn foo() -> i32 { 3346fn foo() -> i32 {
3253 loop { 3347 loop {
3254 let n = 1; 3348 let n = 1;
@@ -3259,8 +3353,9 @@ fn foo() -> i32 {
3259 let m = k + 1;$0 3353 let m = k + 1;$0
3260 let h = 1; 3354 let h = 1;
3261 } 3355 }
3262}"##, 3356}
3263 r##" 3357"#,
3358 r#"
3264fn foo() -> i32 { 3359fn foo() -> i32 {
3265 loop { 3360 loop {
3266 let n = 1; 3361 let n = 1;
@@ -3278,7 +3373,8 @@ fn $0fun_name() -> Option<i32> {
3278 } 3373 }
3279 let m = k + 1; 3374 let m = k + 1;
3280 None 3375 None
3281}"##, 3376}
3377"#,
3282 ); 3378 );
3283 } 3379 }
3284 3380
@@ -3286,7 +3382,7 @@ fn $0fun_name() -> Option<i32> {
3286 fn break_with_value_and_return() { 3382 fn break_with_value_and_return() {
3287 check_assist( 3383 check_assist(
3288 extract_function, 3384 extract_function,
3289 r##" 3385 r#"
3290fn foo() -> i64 { 3386fn foo() -> i64 {
3291 loop { 3387 loop {
3292 let n = 1; 3388 let n = 1;
@@ -3298,8 +3394,9 @@ fn foo() -> i64 {
3298 let m = k + 1;$0 3394 let m = k + 1;$0
3299 let h = 1 + m; 3395 let h = 1 + m;
3300 } 3396 }
3301}"##, 3397}
3302 r##" 3398"#,
3399 r#"
3303fn foo() -> i64 { 3400fn foo() -> i64 {
3304 loop { 3401 loop {
3305 let n = 1; 3402 let n = 1;
@@ -3318,7 +3415,8 @@ fn $0fun_name() -> Result<i32, i64> {
3318 } 3415 }
3319 let m = k + 1; 3416 let m = k + 1;
3320 Ok(m) 3417 Ok(m)
3321}"##, 3418}
3419"#,
3322 ); 3420 );
3323 } 3421 }
3324 3422
@@ -3326,9 +3424,8 @@ fn $0fun_name() -> Result<i32, i64> {
3326 fn try_option() { 3424 fn try_option() {
3327 check_assist( 3425 check_assist(
3328 extract_function, 3426 extract_function,
3329 r##" 3427 r#"
3330enum Option<T> { None, Some(T), } 3428//- minicore: option
3331use Option::*;
3332fn bar() -> Option<i32> { None } 3429fn bar() -> Option<i32> { None }
3333fn foo() -> Option<()> { 3430fn foo() -> Option<()> {
3334 let n = bar()?; 3431 let n = bar()?;
@@ -3336,10 +3433,9 @@ fn foo() -> Option<()> {
3336 let m = k + 1;$0 3433 let m = k + 1;$0
3337 let h = 1 + m; 3434 let h = 1 + m;
3338 Some(()) 3435 Some(())
3339}"##, 3436}
3340 r##" 3437"#,
3341enum Option<T> { None, Some(T), } 3438 r#"
3342use Option::*;
3343fn bar() -> Option<i32> { None } 3439fn bar() -> Option<i32> { None }
3344fn foo() -> Option<()> { 3440fn foo() -> Option<()> {
3345 let n = bar()?; 3441 let n = bar()?;
@@ -3352,7 +3448,8 @@ fn $0fun_name() -> Option<i32> {
3352 let k = foo()?; 3448 let k = foo()?;
3353 let m = k + 1; 3449 let m = k + 1;
3354 Some(m) 3450 Some(m)
3355}"##, 3451}
3452"#,
3356 ); 3453 );
3357 } 3454 }
3358 3455
@@ -3360,19 +3457,17 @@ fn $0fun_name() -> Option<i32> {
3360 fn try_option_unit() { 3457 fn try_option_unit() {
3361 check_assist( 3458 check_assist(
3362 extract_function, 3459 extract_function,
3363 r##" 3460 r#"
3364enum Option<T> { None, Some(T), } 3461//- minicore: option
3365use Option::*;
3366fn foo() -> Option<()> { 3462fn foo() -> Option<()> {
3367 let n = 1; 3463 let n = 1;
3368 $0let k = foo()?; 3464 $0let k = foo()?;
3369 let m = k + 1;$0 3465 let m = k + 1;$0
3370 let h = 1 + n; 3466 let h = 1 + n;
3371 Some(()) 3467 Some(())
3372}"##, 3468}
3373 r##" 3469"#,
3374enum Option<T> { None, Some(T), } 3470 r#"
3375use Option::*;
3376fn foo() -> Option<()> { 3471fn foo() -> Option<()> {
3377 let n = 1; 3472 let n = 1;
3378 fun_name()?; 3473 fun_name()?;
@@ -3384,7 +3479,8 @@ fn $0fun_name() -> Option<()> {
3384 let k = foo()?; 3479 let k = foo()?;
3385 let m = k + 1; 3480 let m = k + 1;
3386 Some(()) 3481 Some(())
3387}"##, 3482}
3483"#,
3388 ); 3484 );
3389 } 3485 }
3390 3486
@@ -3392,19 +3488,17 @@ fn $0fun_name() -> Option<()> {
3392 fn try_result() { 3488 fn try_result() {
3393 check_assist( 3489 check_assist(
3394 extract_function, 3490 extract_function,
3395 r##" 3491 r#"
3396enum Result<T, E> { Ok(T), Err(E), } 3492//- minicore: result
3397use Result::*;
3398fn foo() -> Result<(), i64> { 3493fn foo() -> Result<(), i64> {
3399 let n = 1; 3494 let n = 1;
3400 $0let k = foo()?; 3495 $0let k = foo()?;
3401 let m = k + 1;$0 3496 let m = k + 1;$0
3402 let h = 1 + m; 3497 let h = 1 + m;
3403 Ok(()) 3498 Ok(())
3404}"##, 3499}
3405 r##" 3500"#,
3406enum Result<T, E> { Ok(T), Err(E), } 3501 r#"
3407use Result::*;
3408fn foo() -> Result<(), i64> { 3502fn foo() -> Result<(), i64> {
3409 let n = 1; 3503 let n = 1;
3410 let m = fun_name()?; 3504 let m = fun_name()?;
@@ -3416,7 +3510,8 @@ fn $0fun_name() -> Result<i32, i64> {
3416 let k = foo()?; 3510 let k = foo()?;
3417 let m = k + 1; 3511 let m = k + 1;
3418 Ok(m) 3512 Ok(m)
3419}"##, 3513}
3514"#,
3420 ); 3515 );
3421 } 3516 }
3422 3517
@@ -3424,9 +3519,8 @@ fn $0fun_name() -> Result<i32, i64> {
3424 fn try_option_with_return() { 3519 fn try_option_with_return() {
3425 check_assist( 3520 check_assist(
3426 extract_function, 3521 extract_function,
3427 r##" 3522 r#"
3428enum Option<T> { None, Some(T) } 3523//- minicore: option
3429use Option::*;
3430fn foo() -> Option<()> { 3524fn foo() -> Option<()> {
3431 let n = 1; 3525 let n = 1;
3432 $0let k = foo()?; 3526 $0let k = foo()?;
@@ -3436,10 +3530,9 @@ fn foo() -> Option<()> {
3436 let m = k + 1;$0 3530 let m = k + 1;$0
3437 let h = 1 + m; 3531 let h = 1 + m;
3438 Some(()) 3532 Some(())
3439}"##, 3533}
3440 r##" 3534"#,
3441enum Option<T> { None, Some(T) } 3535 r#"
3442use Option::*;
3443fn foo() -> Option<()> { 3536fn foo() -> Option<()> {
3444 let n = 1; 3537 let n = 1;
3445 let m = fun_name()?; 3538 let m = fun_name()?;
@@ -3454,7 +3547,8 @@ fn $0fun_name() -> Option<i32> {
3454 } 3547 }
3455 let m = k + 1; 3548 let m = k + 1;
3456 Some(m) 3549 Some(m)
3457}"##, 3550}
3551"#,
3458 ); 3552 );
3459 } 3553 }
3460 3554
@@ -3462,9 +3556,8 @@ fn $0fun_name() -> Option<i32> {
3462 fn try_result_with_return() { 3556 fn try_result_with_return() {
3463 check_assist( 3557 check_assist(
3464 extract_function, 3558 extract_function,
3465 r##" 3559 r#"
3466enum Result<T, E> { Ok(T), Err(E), } 3560//- minicore: result
3467use Result::*;
3468fn foo() -> Result<(), i64> { 3561fn foo() -> Result<(), i64> {
3469 let n = 1; 3562 let n = 1;
3470 $0let k = foo()?; 3563 $0let k = foo()?;
@@ -3474,10 +3567,9 @@ fn foo() -> Result<(), i64> {
3474 let m = k + 1;$0 3567 let m = k + 1;$0
3475 let h = 1 + m; 3568 let h = 1 + m;
3476 Ok(()) 3569 Ok(())
3477}"##, 3570}
3478 r##" 3571"#,
3479enum Result<T, E> { Ok(T), Err(E), } 3572 r#"
3480use Result::*;
3481fn foo() -> Result<(), i64> { 3573fn foo() -> Result<(), i64> {
3482 let n = 1; 3574 let n = 1;
3483 let m = fun_name()?; 3575 let m = fun_name()?;
@@ -3492,7 +3584,8 @@ fn $0fun_name() -> Result<i32, i64> {
3492 } 3584 }
3493 let m = k + 1; 3585 let m = k + 1;
3494 Ok(m) 3586 Ok(m)
3495}"##, 3587}
3588"#,
3496 ); 3589 );
3497 } 3590 }
3498 3591
@@ -3501,9 +3594,8 @@ fn $0fun_name() -> Result<i32, i64> {
3501 cov_mark::check!(external_control_flow_try_and_bc); 3594 cov_mark::check!(external_control_flow_try_and_bc);
3502 check_assist_not_applicable( 3595 check_assist_not_applicable(
3503 extract_function, 3596 extract_function,
3504 r##" 3597 r#"
3505enum Option<T> { None, Some(T) } 3598//- minicore: option
3506use Option::*;
3507fn foo() -> Option<()> { 3599fn foo() -> Option<()> {
3508 loop { 3600 loop {
3509 let n = Some(1); 3601 let n = Some(1);
@@ -3514,7 +3606,8 @@ fn foo() -> Option<()> {
3514 let r = n + k; 3606 let r = n + k;
3515 } 3607 }
3516 Some(()) 3608 Some(())
3517}"##, 3609}
3610"#,
3518 ); 3611 );
3519 } 3612 }
3520 3613
@@ -3523,9 +3616,8 @@ fn foo() -> Option<()> {
3523 cov_mark::check!(external_control_flow_try_and_return_non_err); 3616 cov_mark::check!(external_control_flow_try_and_return_non_err);
3524 check_assist_not_applicable( 3617 check_assist_not_applicable(
3525 extract_function, 3618 extract_function,
3526 r##" 3619 r#"
3527enum Result<T, E> { Ok(T), Err(E), } 3620//- minicore: result
3528use Result::*;
3529fn foo() -> Result<(), i64> { 3621fn foo() -> Result<(), i64> {
3530 let n = 1; 3622 let n = 1;
3531 $0let k = foo()?; 3623 $0let k = foo()?;
@@ -3535,7 +3627,8 @@ fn foo() -> Result<(), i64> {
3535 let m = k + 1;$0 3627 let m = k + 1;$0
3536 let h = 1 + m; 3628 let h = 1 + m;
3537 Ok(()) 3629 Ok(())
3538}"##, 3630}
3631"#,
3539 ); 3632 );
3540 } 3633 }
3541 3634
@@ -3543,7 +3636,7 @@ fn foo() -> Result<(), i64> {
3543 fn param_usage_in_macro() { 3636 fn param_usage_in_macro() {
3544 check_assist( 3637 check_assist(
3545 extract_function, 3638 extract_function,
3546 r" 3639 r#"
3547macro_rules! m { 3640macro_rules! m {
3548 ($val:expr) => { $val }; 3641 ($val:expr) => { $val };
3549} 3642}
@@ -3552,8 +3645,9 @@ fn foo() {
3552 let n = 1; 3645 let n = 1;
3553 $0let k = n * m!(n);$0 3646 $0let k = n * m!(n);$0
3554 let m = k + 1; 3647 let m = k + 1;
3555}", 3648}
3556 r" 3649"#,
3650 r#"
3557macro_rules! m { 3651macro_rules! m {
3558 ($val:expr) => { $val }; 3652 ($val:expr) => { $val };
3559} 3653}
@@ -3567,7 +3661,8 @@ fn foo() {
3567fn $0fun_name(n: i32) -> i32 { 3661fn $0fun_name(n: i32) -> i32 {
3568 let k = n * m!(n); 3662 let k = n * m!(n);
3569 k 3663 k
3570}", 3664}
3665"#,
3571 ); 3666 );
3572 } 3667 }
3573 3668
@@ -3575,7 +3670,8 @@ fn $0fun_name(n: i32) -> i32 {
3575 fn extract_with_await() { 3670 fn extract_with_await() {
3576 check_assist( 3671 check_assist(
3577 extract_function, 3672 extract_function,
3578 r#"fn main() { 3673 r#"
3674fn main() {
3579 $0some_function().await;$0 3675 $0some_function().await;$0
3580} 3676}
3581 3677
@@ -3603,7 +3699,8 @@ async fn some_function() {
3603 fn extract_with_await_in_args() { 3699 fn extract_with_await_in_args() {
3604 check_assist( 3700 check_assist(
3605 extract_function, 3701 extract_function,
3606 r#"fn main() { 3702 r#"
3703fn main() {
3607 $0function_call("a", some_function().await);$0 3704 $0function_call("a", some_function().await);$0
3608} 3705}
3609 3706
diff --git a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
index da6df9106..430710448 100644
--- a/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
+++ b/crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
@@ -48,6 +48,7 @@ pub(crate) fn extract_struct_from_enum_variant(
48 let variant_name = variant.name()?; 48 let variant_name = variant.name()?;
49 let variant_hir = ctx.sema.to_def(&variant)?; 49 let variant_hir = ctx.sema.to_def(&variant)?;
50 if existing_definition(ctx.db(), &variant_name, &variant_hir) { 50 if existing_definition(ctx.db(), &variant_name, &variant_hir) {
51 cov_mark::hit!(test_extract_enum_not_applicable_if_struct_exists);
51 return None; 52 return None;
52 } 53 }
53 54
@@ -300,18 +301,10 @@ fn reference_to_node(
300 301
301#[cfg(test)] 302#[cfg(test)]
302mod tests { 303mod tests {
303 use ide_db::helpers::FamousDefs;
304
305 use crate::tests::{check_assist, check_assist_not_applicable}; 304 use crate::tests::{check_assist, check_assist_not_applicable};
306 305
307 use super::*; 306 use super::*;
308 307
309 fn check_not_applicable(ra_fixture: &str) {
310 let fixture =
311 format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
312 check_assist_not_applicable(extract_struct_from_enum_variant, &fixture)
313 }
314
315 #[test] 308 #[test]
316 fn test_extract_struct_several_fields_tuple() { 309 fn test_extract_struct_several_fields_tuple() {
317 check_assist( 310 check_assist(
@@ -699,29 +692,33 @@ fn foo() {
699 692
700 #[test] 693 #[test]
701 fn test_extract_enum_not_applicable_for_element_with_no_fields() { 694 fn test_extract_enum_not_applicable_for_element_with_no_fields() {
702 check_not_applicable("enum A { $0One }"); 695 check_assist_not_applicable(extract_struct_from_enum_variant, r#"enum A { $0One }"#);
703 } 696 }
704 697
705 #[test] 698 #[test]
706 fn test_extract_enum_not_applicable_if_struct_exists() { 699 fn test_extract_enum_not_applicable_if_struct_exists() {
707 check_not_applicable( 700 cov_mark::check!(test_extract_enum_not_applicable_if_struct_exists);
708 r#"struct One; 701 check_assist_not_applicable(
709 enum A { $0One(u8, u32) }"#, 702 extract_struct_from_enum_variant,
703 r#"
704struct One;
705enum A { $0One(u8, u32) }
706"#,
710 ); 707 );
711 } 708 }
712 709
713 #[test] 710 #[test]
714 fn test_extract_not_applicable_one_field() { 711 fn test_extract_not_applicable_one_field() {
715 check_not_applicable(r"enum A { $0One(u32) }"); 712 check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0One(u32) }");
716 } 713 }
717 714
718 #[test] 715 #[test]
719 fn test_extract_not_applicable_no_field_tuple() { 716 fn test_extract_not_applicable_no_field_tuple() {
720 check_not_applicable(r"enum A { $0None() }"); 717 check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0None() }");
721 } 718 }
722 719
723 #[test] 720 #[test]
724 fn test_extract_not_applicable_no_field_named() { 721 fn test_extract_not_applicable_no_field_named() {
725 check_not_applicable(r"enum A { $0None {} }"); 722 check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0None {} }");
726 } 723 }
727} 724}
diff --git a/crates/ide_assists/src/handlers/fill_match_arms.rs b/crates/ide_assists/src/handlers/fill_match_arms.rs
index cd0f6dba9..318faa0fc 100644
--- a/crates/ide_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ide_assists/src/handlers/fill_match_arms.rs
@@ -481,26 +481,21 @@ fn main() {
481 check_assist( 481 check_assist(
482 fill_match_arms, 482 fill_match_arms,
483 r#" 483 r#"
484enum Option<T> { Some(T), None } 484//- minicore: option
485use Option::*;
486
487fn main() { 485fn main() {
488 match None$0 { 486 match None$0 {
489 None => {} 487 None => {}
490 } 488 }
491} 489}
492 "#, 490"#,
493 r#" 491 r#"
494enum Option<T> { Some(T), None }
495use Option::*;
496
497fn main() { 492fn main() {
498 match None { 493 match None {
499 None => {} 494 None => {}
500 Some(${0:_}) => todo!(), 495 Some(${0:_}) => todo!(),
501 } 496 }
502} 497}
503 "#, 498"#,
504 ); 499 );
505 } 500 }
506 501
diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs
index b20fe992d..5866d8974 100644
--- a/crates/ide_assists/src/handlers/remove_dbg.rs
+++ b/crates/ide_assists/src/handlers/remove_dbg.rs
@@ -35,14 +35,14 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
35 .prev_sibling_or_token() 35 .prev_sibling_or_token()
36 .and_then(whitespace_start) 36 .and_then(whitespace_start)
37 .map(|start| TextRange::new(start, macro_call.syntax().text_range().end())) 37 .map(|start| TextRange::new(start, macro_call.syntax().text_range().end()))
38 .unwrap_or(macro_call.syntax().text_range()) 38 .unwrap_or_else(|| macro_call.syntax().text_range())
39 }, 39 },
40 ast::ExprStmt(it) => { 40 ast::ExprStmt(it) => {
41 let start = it 41 let start = it
42 .syntax() 42 .syntax()
43 .prev_sibling_or_token() 43 .prev_sibling_or_token()
44 .and_then(whitespace_start) 44 .and_then(whitespace_start)
45 .unwrap_or(it.syntax().text_range().start()); 45 .unwrap_or_else(|| it.syntax().text_range().start());
46 let end = it.syntax().text_range().end(); 46 let end = it.syntax().text_range().end();
47 47
48 TextRange::new(start, end) 48 TextRange::new(start, end)
diff --git a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs
index 9404aa26d..f37aa0d53 100644
--- a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs
+++ b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs
@@ -262,9 +262,7 @@ impl VariantData {
262 check_assist( 262 check_assist(
263 replace_if_let_with_match, 263 replace_if_let_with_match,
264 r#" 264 r#"
265enum Option<T> { Some(T), None } 265//- minicore: option
266use Option::*;
267
268fn foo(x: Option<i32>) { 266fn foo(x: Option<i32>) {
269 $0if let Some(x) = x { 267 $0if let Some(x) = x {
270 println!("{}", x) 268 println!("{}", x)
@@ -272,18 +270,15 @@ fn foo(x: Option<i32>) {
272 println!("none") 270 println!("none")
273 } 271 }
274} 272}
275 "#, 273"#,
276 r#" 274 r#"
277enum Option<T> { Some(T), None }
278use Option::*;
279
280fn foo(x: Option<i32>) { 275fn foo(x: Option<i32>) {
281 match x { 276 match x {
282 Some(x) => println!("{}", x), 277 Some(x) => println!("{}", x),
283 None => println!("none"), 278 None => println!("none"),
284 } 279 }
285} 280}
286 "#, 281"#,
287 ); 282 );
288 } 283 }
289 284
@@ -292,9 +287,7 @@ fn foo(x: Option<i32>) {
292 check_assist( 287 check_assist(
293 replace_if_let_with_match, 288 replace_if_let_with_match,
294 r#" 289 r#"
295enum Option<T> { Some(T), None } 290//- minicore: option
296use Option::*;
297
298fn foo(x: Option<i32>) { 291fn foo(x: Option<i32>) {
299 $0if let None = x { 292 $0if let None = x {
300 println!("none") 293 println!("none")
@@ -302,18 +295,15 @@ fn foo(x: Option<i32>) {
302 println!("some") 295 println!("some")
303 } 296 }
304} 297}
305 "#, 298"#,
306 r#" 299 r#"
307enum Option<T> { Some(T), None }
308use Option::*;
309
310fn foo(x: Option<i32>) { 300fn foo(x: Option<i32>) {
311 match x { 301 match x {
312 None => println!("none"), 302 None => println!("none"),
313 Some(_) => println!("some"), 303 Some(_) => println!("some"),
314 } 304 }
315} 305}
316 "#, 306"#,
317 ); 307 );
318 } 308 }
319 309
@@ -322,9 +312,7 @@ fn foo(x: Option<i32>) {
322 check_assist( 312 check_assist(
323 replace_if_let_with_match, 313 replace_if_let_with_match,
324 r#" 314 r#"
325enum Result<T, E> { Ok(T), Err(E) } 315//- minicore: result
326use Result::*;
327
328fn foo(x: Result<i32, ()>) { 316fn foo(x: Result<i32, ()>) {
329 $0if let Ok(x) = x { 317 $0if let Ok(x) = x {
330 println!("{}", x) 318 println!("{}", x)
@@ -332,18 +320,15 @@ fn foo(x: Result<i32, ()>) {
332 println!("none") 320 println!("none")
333 } 321 }
334} 322}
335 "#, 323"#,
336 r#" 324 r#"
337enum Result<T, E> { Ok(T), Err(E) }
338use Result::*;
339
340fn foo(x: Result<i32, ()>) { 325fn foo(x: Result<i32, ()>) {
341 match x { 326 match x {
342 Ok(x) => println!("{}", x), 327 Ok(x) => println!("{}", x),
343 Err(_) => println!("none"), 328 Err(_) => println!("none"),
344 } 329 }
345} 330}
346 "#, 331"#,
347 ); 332 );
348 } 333 }
349 334
@@ -352,9 +337,7 @@ fn foo(x: Result<i32, ()>) {
352 check_assist( 337 check_assist(
353 replace_if_let_with_match, 338 replace_if_let_with_match,
354 r#" 339 r#"
355enum Result<T, E> { Ok(T), Err(E) } 340//- minicore: result
356use Result::*;
357
358fn foo(x: Result<i32, ()>) { 341fn foo(x: Result<i32, ()>) {
359 $0if let Err(x) = x { 342 $0if let Err(x) = x {
360 println!("{}", x) 343 println!("{}", x)
@@ -362,18 +345,15 @@ fn foo(x: Result<i32, ()>) {
362 println!("ok") 345 println!("ok")
363 } 346 }
364} 347}
365 "#, 348"#,
366 r#" 349 r#"
367enum Result<T, E> { Ok(T), Err(E) }
368use Result::*;
369
370fn foo(x: Result<i32, ()>) { 350fn foo(x: Result<i32, ()>) {
371 match x { 351 match x {
372 Err(x) => println!("{}", x), 352 Err(x) => println!("{}", x),
373 Ok(_) => println!("ok"), 353 Ok(_) => println!("ok"),
374 } 354 }
375} 355}
376 "#, 356"#,
377 ); 357 );
378 } 358 }
379 359
@@ -488,20 +468,15 @@ impl VariantData {
488 check_assist( 468 check_assist(
489 replace_match_with_if_let, 469 replace_match_with_if_let,
490 r#" 470 r#"
491enum Option<T> { Some(T), None } 471//- minicore: option
492use Option::*;
493
494fn foo(x: Option<i32>) { 472fn foo(x: Option<i32>) {
495 $0match x { 473 $0match x {
496 Some(x) => println!("{}", x), 474 Some(x) => println!("{}", x),
497 None => println!("none"), 475 None => println!("none"),
498 } 476 }
499} 477}
500 "#, 478"#,
501 r#" 479 r#"
502enum Option<T> { Some(T), None }
503use Option::*;
504
505fn foo(x: Option<i32>) { 480fn foo(x: Option<i32>) {
506 if let Some(x) = x { 481 if let Some(x) = x {
507 println!("{}", x) 482 println!("{}", x)
@@ -509,7 +484,7 @@ fn foo(x: Option<i32>) {
509 println!("none") 484 println!("none")
510 } 485 }
511} 486}
512 "#, 487"#,
513 ); 488 );
514 } 489 }
515 490
@@ -518,20 +493,15 @@ fn foo(x: Option<i32>) {
518 check_assist( 493 check_assist(
519 replace_match_with_if_let, 494 replace_match_with_if_let,
520 r#" 495 r#"
521enum Result<T, E> { Ok(T), Err(E) } 496//- minicore: result
522use Result::*;
523
524fn foo(x: Result<i32, ()>) { 497fn foo(x: Result<i32, ()>) {
525 $0match x { 498 $0match x {
526 Ok(x) => println!("{}", x), 499 Ok(x) => println!("{}", x),
527 Err(_) => println!("none"), 500 Err(_) => println!("none"),
528 } 501 }
529} 502}
530 "#, 503"#,
531 r#" 504 r#"
532enum Result<T, E> { Ok(T), Err(E) }
533use Result::*;
534
535fn foo(x: Result<i32, ()>) { 505fn foo(x: Result<i32, ()>) {
536 if let Ok(x) = x { 506 if let Ok(x) = x {
537 println!("{}", x) 507 println!("{}", x)
@@ -539,7 +509,7 @@ fn foo(x: Result<i32, ()>) {
539 println!("none") 509 println!("none")
540 } 510 }
541} 511}
542 "#, 512"#,
543 ); 513 );
544 } 514 }
545 515
diff --git a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs
index a3bfa221c..f39c48d8f 100644
--- a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs
+++ b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs
@@ -20,17 +20,16 @@ use ide_db::ty_filter::TryEnum;
20// Replaces `unwrap` with a `match` expression. Works for Result and Option. 20// Replaces `unwrap` with a `match` expression. Works for Result and Option.
21// 21//
22// ``` 22// ```
23// enum Result<T, E> { Ok(T), Err(E) } 23// # //- minicore: result
24// fn main() { 24// fn main() {
25// let x: Result<i32, i32> = Result::Ok(92); 25// let x: Result<i32, i32> = Ok(92);
26// let y = x.$0unwrap(); 26// let y = x.$0unwrap();
27// } 27// }
28// ``` 28// ```
29// -> 29// ->
30// ``` 30// ```
31// enum Result<T, E> { Ok(T), Err(E) }
32// fn main() { 31// fn main() {
33// let x: Result<i32, i32> = Result::Ok(92); 32// let x: Result<i32, i32> = Ok(92);
34// let y = match x { 33// let y = match x {
35// Ok(it) => it, 34// Ok(it) => it,
36// $0_ => unreachable!(), 35// $0_ => unreachable!(),
@@ -97,25 +96,24 @@ mod tests {
97 fn test_replace_result_unwrap_with_match() { 96 fn test_replace_result_unwrap_with_match() {
98 check_assist( 97 check_assist(
99 replace_unwrap_with_match, 98 replace_unwrap_with_match,
100 r" 99 r#"
101enum Result<T, E> { Ok(T), Err(E) } 100//- minicore: result
102fn i<T>(a: T) -> T { a } 101fn i<T>(a: T) -> T { a }
103fn main() { 102fn main() {
104 let x: Result<i32, i32> = Result::Ok(92); 103 let x: Result<i32, i32> = Ok(92);
105 let y = i(x).$0unwrap(); 104 let y = i(x).$0unwrap();
106} 105}
107 ", 106"#,
108 r" 107 r#"
109enum Result<T, E> { Ok(T), Err(E) }
110fn i<T>(a: T) -> T { a } 108fn i<T>(a: T) -> T { a }
111fn main() { 109fn main() {
112 let x: Result<i32, i32> = Result::Ok(92); 110 let x: Result<i32, i32> = Ok(92);
113 let y = match i(x) { 111 let y = match i(x) {
114 Ok(it) => it, 112 Ok(it) => it,
115 $0_ => unreachable!(), 113 $0_ => unreachable!(),
116 }; 114 };
117} 115}
118 ", 116"#,
119 ) 117 )
120 } 118 }
121 119
@@ -123,25 +121,24 @@ fn main() {
123 fn test_replace_option_unwrap_with_match() { 121 fn test_replace_option_unwrap_with_match() {
124 check_assist( 122 check_assist(
125 replace_unwrap_with_match, 123 replace_unwrap_with_match,
126 r" 124 r#"
127enum Option<T> { Some(T), None } 125//- minicore: option
128fn i<T>(a: T) -> T { a } 126fn i<T>(a: T) -> T { a }
129fn main() { 127fn main() {
130 let x = Option::Some(92); 128 let x = Some(92);
131 let y = i(x).$0unwrap(); 129 let y = i(x).$0unwrap();
132} 130}
133 ", 131"#,
134 r" 132 r#"
135enum Option<T> { Some(T), None }
136fn i<T>(a: T) -> T { a } 133fn i<T>(a: T) -> T { a }
137fn main() { 134fn main() {
138 let x = Option::Some(92); 135 let x = Some(92);
139 let y = match i(x) { 136 let y = match i(x) {
140 Some(it) => it, 137 Some(it) => it,
141 $0_ => unreachable!(), 138 $0_ => unreachable!(),
142 }; 139 };
143} 140}
144 ", 141"#,
145 ); 142 );
146 } 143 }
147 144
@@ -149,25 +146,24 @@ fn main() {
149 fn test_replace_result_unwrap_with_match_chaining() { 146 fn test_replace_result_unwrap_with_match_chaining() {
150 check_assist( 147 check_assist(
151 replace_unwrap_with_match, 148 replace_unwrap_with_match,
152 r" 149 r#"
153enum Result<T, E> { Ok(T), Err(E) } 150//- minicore: result
154fn i<T>(a: T) -> T { a } 151fn i<T>(a: T) -> T { a }
155fn main() { 152fn main() {
156 let x: Result<i32, i32> = Result::Ok(92); 153 let x: Result<i32, i32> = Ok(92);
157 let y = i(x).$0unwrap().count_zeroes(); 154 let y = i(x).$0unwrap().count_zeroes();
158} 155}
159 ", 156"#,
160 r" 157 r#"
161enum Result<T, E> { Ok(T), Err(E) }
162fn i<T>(a: T) -> T { a } 158fn i<T>(a: T) -> T { a }
163fn main() { 159fn main() {
164 let x: Result<i32, i32> = Result::Ok(92); 160 let x: Result<i32, i32> = Ok(92);
165 let y = match i(x) { 161 let y = match i(x) {
166 Ok(it) => it, 162 Ok(it) => it,
167 $0_ => unreachable!(), 163 $0_ => unreachable!(),
168 }.count_zeroes(); 164 }.count_zeroes();
169} 165}
170 ", 166"#,
171 ) 167 )
172 } 168 }
173 169
@@ -175,14 +171,14 @@ fn main() {
175 fn replace_unwrap_with_match_target() { 171 fn replace_unwrap_with_match_target() {
176 check_assist_target( 172 check_assist_target(
177 replace_unwrap_with_match, 173 replace_unwrap_with_match,
178 r" 174 r#"
179enum Option<T> { Some(T), None } 175//- minicore: option
180fn i<T>(a: T) -> T { a } 176fn i<T>(a: T) -> T { a }
181fn main() { 177fn main() {
182 let x = Option::Some(92); 178 let x = Some(92);
183 let y = i(x).$0unwrap(); 179 let y = i(x).$0unwrap();
184} 180}
185 ", 181"#,
186 r"i(x).unwrap()", 182 r"i(x).unwrap()",
187 ); 183 );
188 } 184 }
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs
index fa378a622..86a57ce5d 100644
--- a/crates/ide_assists/src/lib.rs
+++ b/crates/ide_assists/src/lib.rs
@@ -15,7 +15,6 @@ mod assist_context;
15#[cfg(test)] 15#[cfg(test)]
16mod tests; 16mod tests;
17pub mod utils; 17pub mod utils;
18pub mod path_transform;
19 18
20use hir::Semantics; 19use hir::Semantics;
21use ide_db::{base_db::FileRange, RootDatabase}; 20use ide_db::{base_db::FileRange, RootDatabase};
diff --git a/crates/ide_assists/src/path_transform.rs b/crates/ide_assists/src/path_transform.rs
deleted file mode 100644
index 48a7fa06a..000000000
--- a/crates/ide_assists/src/path_transform.rs
+++ /dev/null
@@ -1,160 +0,0 @@
1//! See [`PathTransform`].
2
3use hir::{HirDisplay, SemanticsScope};
4use ide_db::helpers::mod_path_to_ast;
5use rustc_hash::FxHashMap;
6use syntax::{
7 ast::{self, AstNode},
8 ted,
9};
10
11/// `PathTransform` substitutes path in SyntaxNodes in bulk.
12///
13/// This is mostly useful for IDE code generation. If you paste some existing
14/// code into a new context (for example, to add method overrides to an `impl`
15/// block), you generally want to appropriately qualify the names, and sometimes
16/// you might want to substitute generic parameters as well:
17///
18/// ```
19/// mod x {
20/// pub struct A<V>;
21/// pub trait T<U> { fn foo(&self, _: U) -> A<U>; }
22/// }
23///
24/// mod y {
25/// use x::T;
26///
27/// impl T<()> for () {
28/// // If we invoke **Add Missing Members** here, we want to copy-paste `foo`.
29/// // But we want a slightly-modified version of it:
30/// fn foo(&self, _: ()) -> x::A<()> {}
31/// }
32/// }
33/// ```
34pub(crate) struct PathTransform<'a> {
35 pub(crate) subst: (hir::Trait, ast::Impl),
36 pub(crate) target_scope: &'a SemanticsScope<'a>,
37 pub(crate) source_scope: &'a SemanticsScope<'a>,
38}
39
40impl<'a> PathTransform<'a> {
41 pub(crate) fn apply(&self, item: ast::AssocItem) {
42 if let Some(ctx) = self.build_ctx() {
43 ctx.apply(item)
44 }
45 }
46 fn build_ctx(&self) -> Option<Ctx<'a>> {
47 let db = self.source_scope.db;
48 let target_module = self.target_scope.module()?;
49 let source_module = self.source_scope.module()?;
50
51 let substs = get_syntactic_substs(self.subst.1.clone()).unwrap_or_default();
52 let generic_def: hir::GenericDef = self.subst.0.into();
53 let substs_by_param: FxHashMap<_, _> = generic_def
54 .type_params(db)
55 .into_iter()
56 // this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
57 .skip(1)
58 // The actual list of trait type parameters may be longer than the one
59 // used in the `impl` block due to trailing default type parameters.
60 // For that case we extend the `substs` with an empty iterator so we
61 // can still hit those trailing values and check if they actually have
62 // a default type. If they do, go for that type from `hir` to `ast` so
63 // the resulting change can be applied correctly.
64 .zip(substs.into_iter().map(Some).chain(std::iter::repeat(None)))
65 .filter_map(|(k, v)| match v {
66 Some(v) => Some((k, v)),
67 None => {
68 let default = k.default(db)?;
69 Some((
70 k,
71 ast::make::ty(&default.display_source_code(db, source_module.into()).ok()?),
72 ))
73 }
74 })
75 .collect();
76
77 let res = Ctx { substs: substs_by_param, target_module, source_scope: self.source_scope };
78 Some(res)
79 }
80}
81
82struct Ctx<'a> {
83 substs: FxHashMap<hir::TypeParam, ast::Type>,
84 target_module: hir::Module,
85 source_scope: &'a SemanticsScope<'a>,
86}
87
88impl<'a> Ctx<'a> {
89 fn apply(&self, item: ast::AssocItem) {
90 for event in item.syntax().preorder() {
91 let node = match event {
92 syntax::WalkEvent::Enter(_) => continue,
93 syntax::WalkEvent::Leave(it) => it,
94 };
95 if let Some(path) = ast::Path::cast(node.clone()) {
96 self.transform_path(path);
97 }
98 }
99 }
100 fn transform_path(&self, path: ast::Path) -> Option<()> {
101 if path.qualifier().is_some() {
102 return None;
103 }
104 if path.segment().and_then(|s| s.param_list()).is_some() {
105 // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway
106 return None;
107 }
108
109 let resolution = self.source_scope.speculative_resolve(&path)?;
110
111 match resolution {
112 hir::PathResolution::TypeParam(tp) => {
113 if let Some(subst) = self.substs.get(&tp) {
114 ted::replace(path.syntax(), subst.clone_subtree().clone_for_update().syntax())
115 }
116 }
117 hir::PathResolution::Def(def) => {
118 let found_path =
119 self.target_module.find_use_path(self.source_scope.db.upcast(), def)?;
120 let res = mod_path_to_ast(&found_path).clone_for_update();
121 if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) {
122 if let Some(segment) = res.segment() {
123 let old = segment.get_or_create_generic_arg_list();
124 ted::replace(old.syntax(), args.clone_subtree().syntax().clone_for_update())
125 }
126 }
127 ted::replace(path.syntax(), res.syntax())
128 }
129 hir::PathResolution::Local(_)
130 | hir::PathResolution::ConstParam(_)
131 | hir::PathResolution::SelfType(_)
132 | hir::PathResolution::Macro(_)
133 | hir::PathResolution::AssocItem(_) => (),
134 }
135 Some(())
136 }
137}
138
139// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
140// trait ref, and then go from the types in the substs back to the syntax).
141fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Vec<ast::Type>> {
142 let target_trait = impl_def.trait_()?;
143 let path_type = match target_trait {
144 ast::Type::PathType(path) => path,
145 _ => return None,
146 };
147 let generic_arg_list = path_type.path()?.segment()?.generic_arg_list()?;
148
149 let mut result = Vec::new();
150 for generic_arg in generic_arg_list.generic_args() {
151 match generic_arg {
152 ast::GenericArg::TypeArg(type_arg) => result.push(type_arg.ty()?),
153 ast::GenericArg::AssocTypeArg(_)
154 | ast::GenericArg::LifetimeArg(_)
155 | ast::GenericArg::ConstArg(_) => (),
156 }
157 }
158
159 Some(result)
160}
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs
index b6f224b21..841537c77 100644
--- a/crates/ide_assists/src/tests.rs
+++ b/crates/ide_assists/src/tests.rs
@@ -36,6 +36,7 @@ pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
36 RootDatabase::with_single_file(text) 36 RootDatabase::with_single_file(text)
37} 37}
38 38
39#[track_caller]
39pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { 40pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
40 let ra_fixture_after = trim_indent(ra_fixture_after); 41 let ra_fixture_after = trim_indent(ra_fixture_after);
41 check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None); 42 check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None);
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index de5d9e55a..1509c3c63 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -209,10 +209,7 @@ fn doctest_convert_into_to_from() {
209 check_doc_test( 209 check_doc_test(
210 "convert_into_to_from", 210 "convert_into_to_from",
211 r#####" 211 r#####"
212//- /lib.rs crate:core 212//- minicore: from
213pub mod convert { pub trait Into<T> { pub fn into(self) -> T; } }
214//- /lib.rs crate:main deps:core
215use core::convert::Into;
216impl $0Into<Thing> for usize { 213impl $0Into<Thing> for usize {
217 fn into(self) -> Thing { 214 fn into(self) -> Thing {
218 Thing { 215 Thing {
@@ -223,7 +220,6 @@ impl $0Into<Thing> for usize {
223} 220}
224"#####, 221"#####,
225 r#####" 222 r#####"
226use core::convert::Into;
227impl From<usize> for Thing { 223impl From<usize> for Thing {
228 fn from(val: usize) -> Self { 224 fn from(val: usize) -> Self {
229 Thing { 225 Thing {
@@ -241,23 +237,19 @@ fn doctest_convert_iter_for_each_to_for() {
241 check_doc_test( 237 check_doc_test(
242 "convert_iter_for_each_to_for", 238 "convert_iter_for_each_to_for",
243 r#####" 239 r#####"
244//- /lib.rs crate:core 240//- minicore: iterators
245pub mod iter { pub mod traits { pub mod iterator { pub trait Iterator {} } } } 241use core::iter;
246pub struct SomeIter;
247impl self::iter::traits::iterator::Iterator for SomeIter {}
248//- /lib.rs crate:main deps:core
249use core::SomeIter;
250fn main() { 242fn main() {
251 let iter = SomeIter; 243 let iter = iter::repeat((9, 2));
252 iter.for_each$0(|(x, y)| { 244 iter.for_each$0(|(x, y)| {
253 println!("x: {}, y: {}", x, y); 245 println!("x: {}, y: {}", x, y);
254 }); 246 });
255} 247}
256"#####, 248"#####,
257 r#####" 249 r#####"
258use core::SomeIter; 250use core::iter;
259fn main() { 251fn main() {
260 let iter = SomeIter; 252 let iter = iter::repeat((9, 2));
261 for (x, y) in iter { 253 for (x, y) in iter {
262 println!("x: {}, y: {}", x, y); 254 println!("x: {}, y: {}", x, y);
263 } 255 }
@@ -1519,16 +1511,15 @@ fn doctest_replace_unwrap_with_match() {
1519 check_doc_test( 1511 check_doc_test(
1520 "replace_unwrap_with_match", 1512 "replace_unwrap_with_match",
1521 r#####" 1513 r#####"
1522enum Result<T, E> { Ok(T), Err(E) } 1514//- minicore: result
1523fn main() { 1515fn main() {
1524 let x: Result<i32, i32> = Result::Ok(92); 1516 let x: Result<i32, i32> = Ok(92);
1525 let y = x.$0unwrap(); 1517 let y = x.$0unwrap();
1526} 1518}
1527"#####, 1519"#####,
1528 r#####" 1520 r#####"
1529enum Result<T, E> { Ok(T), Err(E) }
1530fn main() { 1521fn main() {
1531 let x: Result<i32, i32> = Result::Ok(92); 1522 let x: Result<i32, i32> = Ok(92);
1532 let y = match x { 1523 let y = match x {
1533 Ok(it) => it, 1524 Ok(it) => it,
1534 $0_ => unreachable!(), 1525 $0_ => unreachable!(),
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index 068df005b..0ec236aa0 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -8,6 +8,7 @@ use ast::TypeBoundsOwner;
8use hir::{Adt, HasSource, Semantics}; 8use hir::{Adt, HasSource, Semantics};
9use ide_db::{ 9use ide_db::{
10 helpers::{FamousDefs, SnippetCap}, 10 helpers::{FamousDefs, SnippetCap},
11 path_transform::PathTransform,
11 RootDatabase, 12 RootDatabase,
12}; 13};
13use itertools::Itertools; 14use itertools::Itertools;
@@ -22,10 +23,7 @@ use syntax::{
22 SyntaxNode, TextSize, T, 23 SyntaxNode, TextSize, T,
23}; 24};
24 25
25use crate::{ 26use crate::assist_context::{AssistBuilder, AssistContext};
26 assist_context::{AssistBuilder, AssistContext},
27 path_transform::PathTransform,
28};
29 27
30pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { 28pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
31 extract_trivial_expression(&block) 29 extract_trivial_expression(&block)