diff options
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs | 121 |
1 files changed, 37 insertions, 84 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)] |
100 | mod tests { | 102 | mod 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 | ||
107 | pub struct EmptyIter; | ||
108 | impl Iterator for EmptyIter { | ||
109 | type Item = usize; | ||
110 | fn next(&mut self) -> Option<Self::Item> { None } | ||
111 | } | ||
112 | pub struct Empty; | ||
113 | impl 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#" |
142 | use empty_iter::*; | 112 | //- minicore: iterators |
143 | fn main() { | 113 | fn 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#" |
150 | use empty_iter::*; | ||
151 | fn main() { | 121 | fn 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#" |
165 | use empty_iter::*; | 136 | //- minicore: iterators |
166 | fn main() { | 137 | fn 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#" | ||
173 | use empty_iter::*; | ||
174 | fn 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#" | ||
188 | use empty_iter::*; | ||
189 | fn main() { | ||
190 | let x = Empty.iter(); | ||
191 | x.$0for_each(|(x, y)| { | ||
192 | println!("x: {}, y: {}", x, y); | ||
193 | }); | ||
194 | }"#, | ||
195 | r#" | 144 | r#" |
196 | use empty_iter::*; | ||
197 | fn main() { | 145 | fn 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#" |
211 | use empty_iter::*; | 160 | //- minicore: iterators |
212 | fn main() { | 161 | fn 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#" |
217 | use empty_iter::*; | ||
218 | fn main() { | 167 | fn 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 | ||
232 | fn main() { | 183 | fn 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#" |
242 | use empty_iter::*; | 195 | //- minicore: iterators |
243 | fn main() { | 196 | fn 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 | } |