diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs | 55 |
1 files changed, 44 insertions, 11 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 2dc4fbcd4..7903a18fa 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 | |||
@@ -88,14 +88,28 @@ fn validate_method_call_expr( | |||
88 | 88 | ||
89 | #[cfg(test)] | 89 | #[cfg(test)] |
90 | mod tests { | 90 | mod tests { |
91 | use crate::tests::check_assist; | 91 | use crate::tests::{check_assist, check_assist_not_applicable}; |
92 | 92 | ||
93 | use super::*; | 93 | use super::*; |
94 | 94 | ||
95 | const EMPTY_ITER_FIXTURE: &'static str = r" | ||
96 | //- /lib.rs deps:core crate:empty_iter | ||
97 | pub struct EmptyIter; | ||
98 | impl Iterator for EmptyIter { | ||
99 | type Item = usize; | ||
100 | fn next(&mut self) -> Option<Self::Item> { None } | ||
101 | } | ||
102 | pub struct Empty; | ||
103 | impl Empty { | ||
104 | pub fn iter(&self) -> EmptyIter { EmptyIter } | ||
105 | } | ||
106 | "; | ||
107 | |||
95 | fn check_assist_with_fixtures(before: &str, after: &str) { | 108 | fn check_assist_with_fixtures(before: &str, after: &str) { |
96 | let before = &format!( | 109 | let before = &format!( |
97 | "//- /main.rs crate:main deps:core,empty_iter{}{}", | 110 | "//- /main.rs crate:main deps:core,empty_iter{}{}{}", |
98 | before, | 111 | before, |
112 | EMPTY_ITER_FIXTURE, | ||
99 | FamousDefs::FIXTURE, | 113 | FamousDefs::FIXTURE, |
100 | ); | 114 | ); |
101 | check_assist(convert_iter_for_each_to_for, before, after); | 115 | check_assist(convert_iter_for_each_to_for, before, after); |
@@ -105,19 +119,22 @@ mod tests { | |||
105 | fn test_for_each_in_method() { | 119 | fn test_for_each_in_method() { |
106 | check_assist_with_fixtures( | 120 | check_assist_with_fixtures( |
107 | r#" | 121 | r#" |
122 | use empty_iter::*; | ||
108 | fn main() { | 123 | fn main() { |
109 | let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)]; | 124 | let x = Empty; |
110 | x.iter().$0for_each(|(x, y)| { | 125 | x.iter().$0for_each(|(x, y)| { |
111 | println!("x: {}, y: {}", x, y); | 126 | println!("x: {}, y: {}", x, y); |
112 | }); | 127 | }); |
113 | }"#, | 128 | }"#, |
114 | r#" | 129 | r#" |
130 | use empty_iter::*; | ||
115 | fn main() { | 131 | fn main() { |
116 | let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)]; | 132 | let x = Empty; |
117 | for (x, y) in x.iter() { | 133 | for (x, y) in x.iter() { |
118 | println!("x: {}, y: {}", x, y); | 134 | println!("x: {}, y: {}", x, y); |
119 | }; | 135 | }; |
120 | }"#, | 136 | } |
137 | "#, | ||
121 | ) | 138 | ) |
122 | } | 139 | } |
123 | 140 | ||
@@ -125,17 +142,20 @@ fn main() { | |||
125 | fn test_for_each_without_braces() { | 142 | fn test_for_each_without_braces() { |
126 | check_assist_with_fixtures( | 143 | check_assist_with_fixtures( |
127 | r#" | 144 | r#" |
145 | use empty_iter::*; | ||
128 | fn main() { | 146 | fn main() { |
129 | let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)]; | 147 | let x = Empty; |
130 | x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y)); | 148 | x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y)); |
131 | }"#, | 149 | }"#, |
132 | r#" | 150 | r#" |
151 | use empty_iter::*; | ||
133 | fn main() { | 152 | fn main() { |
134 | let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)]; | 153 | let x = Empty; |
135 | for (x, y) in x.iter() { | 154 | for (x, y) in x.iter() { |
136 | println!("x: {}, y: {}", x, y) | 155 | println!("x: {}, y: {}", x, y) |
137 | }; | 156 | }; |
138 | }"#, | 157 | } |
158 | "#, | ||
139 | ) | 159 | ) |
140 | } | 160 | } |
141 | 161 | ||
@@ -143,17 +163,30 @@ fn main() { | |||
143 | fn test_for_each_in_closure() { | 163 | fn test_for_each_in_closure() { |
144 | check_assist_with_fixtures( | 164 | check_assist_with_fixtures( |
145 | r#" | 165 | r#" |
166 | use empty_iter::*; | ||
146 | fn main() { | 167 | fn main() { |
147 | let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)]; | 168 | let x = Empty; |
148 | x.iter().for_each($0|(x, y)| println!("x: {}, y: {}", x, y)); | 169 | x.iter().for_each($0|(x, y)| println!("x: {}, y: {}", x, y)); |
149 | }"#, | 170 | }"#, |
150 | r#" | 171 | r#" |
172 | use empty_iter::*; | ||
151 | fn main() { | 173 | fn main() { |
152 | let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)]; | 174 | let x = Empty; |
153 | for (x, y) in x.iter() { | 175 | for (x, y) in x.iter() { |
154 | println!("x: {}, y: {}", x, y) | 176 | println!("x: {}, y: {}", x, y) |
155 | }; | 177 | }; |
156 | }"#, | 178 | } |
179 | "#, | ||
157 | ) | 180 | ) |
158 | } | 181 | } |
182 | |||
183 | #[test] | ||
184 | fn test_for_each_not_applicable() { | ||
185 | check_assist_not_applicable( | ||
186 | convert_iter_for_each_to_for, | ||
187 | r#" | ||
188 | fn main() { | ||
189 | value.$0for_each(|x| println!("{}", x)); | ||
190 | }"#) | ||
191 | } | ||
159 | } \ No newline at end of file | 192 | } \ No newline at end of file |