aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs86
-rw-r--r--crates/test_utils/src/minicore.rs26
2 files changed, 43 insertions, 69 deletions
diff --git a/crates/ide_diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs b/crates/ide_diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs
index cd87a10bb..839ceac03 100644
--- a/crates/ide_diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs
+++ b/crates/ide_diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs
@@ -55,44 +55,16 @@ fn fixes(
55 55
56#[cfg(test)] 56#[cfg(test)]
57mod tests { 57mod tests {
58 use crate::tests::check_fix; 58 use crate::tests::{check_diagnostics, check_fix};
59
60 // Register the required standard library types to make the tests work
61 #[track_caller]
62 fn check_diagnostics(ra_fixture: &str) {
63 let prefix = r#"
64//- /main.rs crate:main deps:core
65use core::iter::Iterator;
66use core::option::Option::{self, Some, None};
67"#;
68 let suffix = r#"
69//- /core/lib.rs crate:core
70pub mod option {
71 pub enum Option<T> { Some(T), None }
72}
73pub mod iter {
74 pub trait Iterator {
75 type Item;
76 fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
77 fn next(&mut self) -> Option<Self::Item>;
78 }
79 pub struct FilterMap {}
80 impl Iterator for FilterMap {
81 type Item = i32;
82 fn next(&mut self) -> i32 { 7 }
83 }
84}
85"#;
86 crate::tests::check_diagnostics(&format!("{}{}{}", prefix, ra_fixture, suffix))
87 }
88 59
89 #[test] 60 #[test]
90 fn replace_filter_map_next_with_find_map2() { 61 fn replace_filter_map_next_with_find_map2() {
91 check_diagnostics( 62 check_diagnostics(
92 r#" 63 r#"
93 fn foo() { 64//- minicore: iterators
94 let m = [1, 2, 3].iter().filter_map(|x| Some(92)).next(); 65fn foo() {
95 } //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..) 66 let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
67} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..)
96"#, 68"#,
97 ); 69 );
98 } 70 }
@@ -101,11 +73,11 @@ pub mod iter {
101 fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() { 73 fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() {
102 check_diagnostics( 74 check_diagnostics(
103 r#" 75 r#"
76//- minicore: iterators
104fn foo() { 77fn foo() {
105 let m = [1, 2, 3] 78 let m = core::iter::repeat(())
106 .iter() 79 .filter_map(|()| Some(92))
107 .filter_map(|x| Some(92)) 80 .count();
108 .len();
109} 81}
110"#, 82"#,
111 ); 83 );
@@ -115,12 +87,12 @@ fn foo() {
115 fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() { 87 fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
116 check_diagnostics( 88 check_diagnostics(
117 r#" 89 r#"
90//- minicore: iterators
118fn foo() { 91fn foo() {
119 let m = [1, 2, 3] 92 let m = core::iter::repeat(())
120 .iter() 93 .filter_map(|()| Some(92))
121 .filter_map(|x| Some(92))
122 .map(|x| x + 2) 94 .map(|x| x + 2)
123 .len(); 95 .next();
124} 96}
125"#, 97"#,
126 ); 98 );
@@ -130,10 +102,10 @@ fn foo() {
130 fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() { 102 fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() {
131 check_diagnostics( 103 check_diagnostics(
132 r#" 104 r#"
105//- minicore: iterators
133fn foo() { 106fn foo() {
134 let m = [1, 2, 3] 107 let m = core::iter::repeat(())
135 .iter() 108 .filter_map(|()| Some(92));
136 .filter_map(|x| Some(92));
137 let n = m.next(); 109 let n = m.next();
138} 110}
139"#, 111"#,
@@ -144,34 +116,14 @@ fn foo() {
144 fn replace_with_wind_map() { 116 fn replace_with_wind_map() {
145 check_fix( 117 check_fix(
146 r#" 118 r#"
147//- /main.rs crate:main deps:core 119//- minicore: iterators
148use core::iter::Iterator;
149use core::option::Option::{self, Some, None};
150fn foo() { 120fn foo() {
151 let m = [1, 2, 3].iter().$0filter_map(|x| Some(92)).next(); 121 let m = core::iter::repeat(()).$0filter_map(|()| Some(92)).next();
152}
153//- /core/lib.rs crate:core
154pub mod option {
155 pub enum Option<T> { Some(T), None }
156}
157pub mod iter {
158 pub trait Iterator {
159 type Item;
160 fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
161 fn next(&mut self) -> Option<Self::Item>;
162 }
163 pub struct FilterMap {}
164 impl Iterator for FilterMap {
165 type Item = i32;
166 fn next(&mut self) -> i32 { 7 }
167 }
168} 122}
169"#, 123"#,
170 r#" 124 r#"
171use core::iter::Iterator;
172use core::option::Option::{self, Some, None};
173fn foo() { 125fn foo() {
174 let m = [1, 2, 3].iter().find_map(|x| Some(92)); 126 let m = core::iter::repeat(()).find_map(|()| Some(92));
175} 127}
176"#, 128"#,
177 ) 129 )
diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs
index 71f07d38a..ce6ad8541 100644
--- a/crates/test_utils/src/minicore.rs
+++ b/crates/test_utils/src/minicore.rs
@@ -22,7 +22,7 @@
22//! option: 22//! option:
23//! result: 23//! result:
24//! iterator: option 24//! iterator: option
25//! iterators: iterator 25//! iterators: iterator, fn
26//! default: sized 26//! default: sized
27//! clone: sized 27//! clone: sized
28//! copy: clone 28//! copy: clone
@@ -390,7 +390,6 @@ pub mod iter {
390 iter: I, 390 iter: I,
391 n: usize, 391 n: usize,
392 } 392 }
393
394 impl<I> Iterator for Take<I> 393 impl<I> Iterator for Take<I>
395 where 394 where
396 I: Iterator, 395 I: Iterator,
@@ -401,6 +400,22 @@ pub mod iter {
401 loop {} 400 loop {}
402 } 401 }
403 } 402 }
403
404 pub struct FilterMap<I, F> {
405 iter: I,
406 f: F,
407 }
408 impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
409 where
410 F: FnMut(I::Item) -> Option<B>,
411 {
412 type Item = B;
413
414 #[inline]
415 fn next(&mut self) -> Option<B> {
416 loop {}
417 }
418 }
404 } 419 }
405 pub use self::adapters::Take; 420 pub use self::adapters::Take;
406 421
@@ -448,6 +463,13 @@ pub mod iter {
448 fn take(self, n: usize) -> crate::iter::Take<Self> { 463 fn take(self, n: usize) -> crate::iter::Take<Self> {
449 loop {} 464 loop {}
450 } 465 }
466 fn filter_map<B, F>(self, f: F) -> crate::iter::FilterMap<Self, F>
467 where
468 Self: Sized,
469 F: FnMut(Self::Item) -> Option<B>,
470 {
471 loop {}
472 }
451 // endregion:iterators 473 // endregion:iterators
452 } 474 }
453 impl<I: Iterator + ?Sized> Iterator for &mut I { 475 impl<I: Iterator + ?Sized> Iterator for &mut I {