aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs121
-rw-r--r--crates/ide_db/src/helpers/famous_defs_fixture.rs70
2 files changed, 37 insertions, 154 deletions
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 4e75a7b14..7fd73d4c7 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
@@ -77,9 +77,11 @@ fn validate_method_call_expr(
77 expr: ast::MethodCallExpr, 77 expr: ast::MethodCallExpr,
78) -> Option<(ast::Expr, ast::Expr)> { 78) -> Option<(ast::Expr, ast::Expr)> {
79 let name_ref = expr.name_ref()?; 79 let name_ref = expr.name_ref()?;
80 if name_ref.syntax().text_range().intersect(ctx.frange.range).is_none() 80 if name_ref.syntax().text_range().intersect(ctx.frange.range).is_none() {
81 || name_ref.text() != "for_each" 81 cov_mark::hit!(test_for_each_not_applicable_invalid_cursor_pos);
82 { 82 return None;
83 }
84 if name_ref.text() != "for_each" {
83 return None; 85 return None;
84 } 86 }
85 87
@@ -98,59 +100,27 @@ fn validate_method_call_expr(
98 100
99#[cfg(test)] 101#[cfg(test)]
100mod tests { 102mod tests {
101 use crate::tests::{self, check_assist}; 103 use crate::tests::{check_assist, check_assist_not_applicable};
102 104
103 use super::*; 105 use super::*;
104 106
105 const EMPTY_ITER_FIXTURE: &'static str = r"
106//- /lib.rs deps:core crate:empty_iter
107pub struct EmptyIter;
108impl Iterator for EmptyIter {
109 type Item = usize;
110 fn next(&mut self) -> Option<Self::Item> { None }
111}
112pub struct Empty;
113impl Empty {
114 pub fn iter(&self) -> EmptyIter { EmptyIter }
115}
116";
117
118 fn check_assist_with_fixtures(before: &str, after: &str) {
119 let before = &format!(
120 "//- /main.rs crate:main deps:core,empty_iter{}{}{}",
121 before,
122 EMPTY_ITER_FIXTURE,
123 FamousDefs::FIXTURE,
124 );
125 check_assist(convert_iter_for_each_to_for, before, after);
126 }
127
128 fn check_assist_not_applicable(before: &str) {
129 let before = &format!(
130 "//- /main.rs crate:main deps:core,empty_iter{}{}{}",
131 before,
132 EMPTY_ITER_FIXTURE,
133 FamousDefs::FIXTURE,
134 );
135 tests::check_assist_not_applicable(convert_iter_for_each_to_for, before);
136 }
137
138 #[test] 107 #[test]
139 fn test_for_each_in_method_stmt() { 108 fn test_for_each_in_method_stmt() {
140 check_assist_with_fixtures( 109 check_assist(
110 convert_iter_for_each_to_for,
141 r#" 111 r#"
142use empty_iter::*; 112//- minicore: iterators
143fn main() { 113fn main() {
144 let x = Empty; 114 let it = core::iter::repeat(92);
145 x.iter().$0for_each(|(x, y)| { 115 it.$0for_each(|(x, y)| {
146 println!("x: {}, y: {}", x, y); 116 println!("x: {}, y: {}", x, y);
147 }); 117 });
148}"#, 118}
119"#,
149 r#" 120 r#"
150use empty_iter::*;
151fn main() { 121fn main() {
152 let x = Empty; 122 let it = core::iter::repeat(92);
153 for (x, y) in x.iter() { 123 for (x, y) in it {
154 println!("x: {}, y: {}", x, y); 124 println!("x: {}, y: {}", x, y);
155 } 125 }
156} 126}
@@ -160,43 +130,21 @@ fn main() {
160 130
161 #[test] 131 #[test]
162 fn test_for_each_in_method() { 132 fn test_for_each_in_method() {
163 check_assist_with_fixtures( 133 check_assist(
134 convert_iter_for_each_to_for,
164 r#" 135 r#"
165use empty_iter::*; 136//- minicore: iterators
166fn main() { 137fn main() {
167 let x = Empty; 138 let it = core::iter::repeat(92);
168 x.iter().$0for_each(|(x, y)| { 139 it.$0for_each(|(x, y)| {
169 println!("x: {}, y: {}", x, y); 140 println!("x: {}, y: {}", x, y);
170 }) 141 })
171}"#,
172 r#"
173use empty_iter::*;
174fn main() {
175 let x = Empty;
176 for (x, y) in x.iter() {
177 println!("x: {}, y: {}", x, y);
178 }
179} 142}
180"#, 143"#,
181 )
182 }
183
184 #[test]
185 fn test_for_each_in_iter_stmt() {
186 check_assist_with_fixtures(
187 r#"
188use empty_iter::*;
189fn main() {
190 let x = Empty.iter();
191 x.$0for_each(|(x, y)| {
192 println!("x: {}, y: {}", x, y);
193 });
194}"#,
195 r#" 144 r#"
196use empty_iter::*;
197fn main() { 145fn main() {
198 let x = Empty.iter(); 146 let it = core::iter::repeat(92);
199 for (x, y) in x { 147 for (x, y) in it {
200 println!("x: {}, y: {}", x, y); 148 println!("x: {}, y: {}", x, y);
201 } 149 }
202} 150}
@@ -206,18 +154,19 @@ fn main() {
206 154
207 #[test] 155 #[test]
208 fn test_for_each_without_braces_stmt() { 156 fn test_for_each_without_braces_stmt() {
209 check_assist_with_fixtures( 157 check_assist(
158 convert_iter_for_each_to_for,
210 r#" 159 r#"
211use empty_iter::*; 160//- minicore: iterators
212fn main() { 161fn main() {
213 let x = Empty; 162 let it = core::iter::repeat(92);
214 x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y)); 163 it.$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
215}"#, 164}
165"#,
216 r#" 166 r#"
217use empty_iter::*;
218fn main() { 167fn main() {
219 let x = Empty; 168 let it = core::iter::repeat(92);
220 for (x, y) in x.iter() { 169 for (x, y) in it {
221 println!("x: {}, y: {}", x, y) 170 println!("x: {}, y: {}", x, y)
222 } 171 }
223} 172}
@@ -228,7 +177,9 @@ fn main() {
228 #[test] 177 #[test]
229 fn test_for_each_not_applicable() { 178 fn test_for_each_not_applicable() {
230 check_assist_not_applicable( 179 check_assist_not_applicable(
180 convert_iter_for_each_to_for,
231 r#" 181 r#"
182//- minicore: iterators
232fn main() { 183fn main() {
233 ().$0for_each(|x| println!("{}", x)); 184 ().$0for_each(|x| println!("{}", x));
234}"#, 185}"#,
@@ -237,11 +188,13 @@ fn main() {
237 188
238 #[test] 189 #[test]
239 fn test_for_each_not_applicable_invalid_cursor_pos() { 190 fn test_for_each_not_applicable_invalid_cursor_pos() {
191 cov_mark::check!(test_for_each_not_applicable_invalid_cursor_pos);
240 check_assist_not_applicable( 192 check_assist_not_applicable(
193 convert_iter_for_each_to_for,
241 r#" 194 r#"
242use empty_iter::*; 195//- minicore: iterators
243fn main() { 196fn main() {
244 Empty.iter().for_each(|(x, y)| $0println!("x: {}, y: {}", x, y)); 197 core::iter::repeat(92).for_each(|(x, y)| $0println!("x: {}, y: {}", x, y));
245}"#, 198}"#,
246 ) 199 )
247 } 200 }
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs
index 312851966..5aa77a24d 100644
--- a/crates/ide_db/src/helpers/famous_defs_fixture.rs
+++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs
@@ -26,76 +26,6 @@ pub mod default {
26 } 26 }
27} 27}
28 28
29pub mod iter {
30 pub use self::traits::{collect::IntoIterator, iterator::Iterator};
31 mod traits {
32 pub(crate) mod iterator {
33 use crate::option::Option;
34 pub trait Iterator {
35 type Item;
36 fn next(&mut self) -> Option<Self::Item>;
37 fn by_ref(&mut self) -> &mut Self {
38 self
39 }
40 fn take(self, n: usize) -> crate::iter::Take<Self> {
41 crate::iter::Take { inner: self }
42 }
43 }
44
45 impl<I: Iterator> Iterator for &mut I {
46 type Item = I::Item;
47 fn next(&mut self) -> Option<I::Item> {
48 (**self).next()
49 }
50 }
51 }
52 pub(crate) mod collect {
53 pub trait IntoIterator {
54 type Item;
55 }
56 }
57 }
58
59 pub use self::sources::*;
60 pub(crate) mod sources {
61 use super::Iterator;
62 use crate::option::Option::{self, *};
63 pub struct Repeat<A> {
64 element: A,
65 }
66
67 pub fn repeat<T>(elt: T) -> Repeat<T> {
68 Repeat { element: elt }
69 }
70
71 impl<A> Iterator for Repeat<A> {
72 type Item = A;
73
74 fn next(&mut self) -> Option<A> {
75 None
76 }
77 }
78 }
79
80 pub use self::adapters::*;
81 pub(crate) mod adapters {
82 use super::Iterator;
83 use crate::option::Option::{self, *};
84 pub struct Take<I> {
85 pub(crate) inner: I,
86 }
87 impl<I> Iterator for Take<I>
88 where
89 I: Iterator,
90 {
91 type Item = <I as Iterator>::Item;
92 fn next(&mut self) -> Option<<I as Iterator>::Item> {
93 None
94 }
95 }
96 }
97}
98
99pub mod ops { 29pub mod ops {
100 #[lang = "fn"] 30 #[lang = "fn"]
101 pub trait Fn<Args>: FnMut<Args> { 31 pub trait Fn<Args>: FnMut<Args> {