@@ -88,14 +88,28 @@ fn validate_method_call_expr(
88
88
89
89
#[ cfg( test) ]
90
90
mod tests {
91
- use crate :: tests:: check_assist;
91
+ use crate :: tests:: { check_assist, check_assist_not_applicable } ;
92
92
93
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
108
fn check_assist_with_fixtures ( before : & str , after : & str ) {
96
109
let before = & format ! (
97
- "//- /main.rs crate:main deps:core,empty_iter{}{}" ,
110
+ "//- /main.rs crate:main deps:core,empty_iter{}{}{} " ,
98
111
before,
112
+ EMPTY_ITER_FIXTURE ,
99
113
FamousDefs :: FIXTURE ,
100
114
) ;
101
115
check_assist ( convert_iter_for_each_to_for, before, after) ;
@@ -105,55 +119,74 @@ mod tests {
105
119
fn test_for_each_in_method ( ) {
106
120
check_assist_with_fixtures (
107
121
r#"
122
+ use empty_iter::*;
108
123
fn main() {
109
- let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)] ;
124
+ let x = Empty ;
110
125
x.iter().$0for_each(|(x, y)| {
111
126
println!("x: {}, y: {}", x, y);
112
127
});
113
128
}"# ,
114
129
r#"
130
+ use empty_iter::*;
115
131
fn main() {
116
- let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)] ;
132
+ let x = Empty ;
117
133
for (x, y) in x.iter() {
118
134
println!("x: {}, y: {}", x, y);
119
135
};
120
- }"# ,
136
+ }
137
+ "# ,
121
138
)
122
139
}
123
140
124
141
#[ test]
125
142
fn test_for_each_without_braces ( ) {
126
143
check_assist_with_fixtures (
127
144
r#"
145
+ use empty_iter::*;
128
146
fn main() {
129
- let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)] ;
147
+ let x = Empty ;
130
148
x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
131
149
}"# ,
132
150
r#"
151
+ use empty_iter::*;
133
152
fn main() {
134
- let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)] ;
153
+ let x = Empty ;
135
154
for (x, y) in x.iter() {
136
155
println!("x: {}, y: {}", x, y)
137
156
};
138
- }"# ,
157
+ }
158
+ "# ,
139
159
)
140
160
}
141
161
142
162
#[ test]
143
163
fn test_for_each_in_closure ( ) {
144
164
check_assist_with_fixtures (
145
165
r#"
166
+ use empty_iter::*;
146
167
fn main() {
147
- let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)] ;
168
+ let x = Empty ;
148
169
x.iter().for_each($0|(x, y)| println!("x: {}, y: {}", x, y));
149
170
}"# ,
150
171
r#"
172
+ use empty_iter::*;
151
173
fn main() {
152
- let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)] ;
174
+ let x = Empty ;
153
175
for (x, y) in x.iter() {
154
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
192
}
0 commit comments