aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r--crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs55
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)]
90mod tests { 90mod 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
97pub struct EmptyIter;
98impl Iterator for EmptyIter {
99 type Item = usize;
100 fn next(&mut self) -> Option<Self::Item> { None }
101}
102pub struct Empty;
103impl 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#"
122use empty_iter::*;
108fn main() { 123fn 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#"
130use empty_iter::*;
115fn main() { 131fn 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#"
145use empty_iter::*;
128fn main() { 146fn 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#"
151use empty_iter::*;
133fn main() { 152fn 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#"
166use empty_iter::*;
146fn main() { 167fn 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#"
172use empty_iter::*;
151fn main() { 173fn 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#"
188fn main() {
189 value.$0for_each(|x| println!("{}", x));
190}"#)
191 }
159} \ No newline at end of file 192} \ No newline at end of file