Skip to content

Commit 90da9fc

Browse files
committed
minor: use minicore
1 parent a9623f3 commit 90da9fc

File tree

2 files changed

+43
-69
lines changed

2 files changed

+43
-69
lines changed

crates/ide_diagnostics/src/handlers/replace_filter_map_next_with_find_map.rs

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,16 @@ fn fixes(
5555

5656
#[cfg(test)]
5757
mod tests {
58-
use crate::tests::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
65-
use core::iter::Iterator;
66-
use core::option::Option::{self, Some, None};
67-
"#;
68-
let suffix = r#"
69-
//- /core/lib.rs crate:core
70-
pub mod option {
71-
pub enum Option<T> { Some(T), None }
72-
}
73-
pub 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-
}
58+
use crate::tests::{check_diagnostics, check_fix};
8859

8960
#[test]
9061
fn replace_filter_map_next_with_find_map2() {
9162
check_diagnostics(
9263
r#"
93-
fn foo() {
94-
let m = [1, 2, 3].iter().filter_map(|x| Some(92)).next();
95-
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..)
64+
//- minicore: iterators
65+
fn foo() {
66+
let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
67+
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..)
9668
"#,
9769
);
9870
}
@@ -101,11 +73,11 @@ pub mod iter {
10173
fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() {
10274
check_diagnostics(
10375
r#"
76+
//- minicore: iterators
10477
fn foo() {
105-
let m = [1, 2, 3]
106-
.iter()
107-
.filter_map(|x| Some(92))
108-
.len();
78+
let m = core::iter::repeat(())
79+
.filter_map(|()| Some(92))
80+
.count();
10981
}
11082
"#,
11183
);
@@ -115,12 +87,12 @@ fn foo() {
11587
fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
11688
check_diagnostics(
11789
r#"
90+
//- minicore: iterators
11891
fn foo() {
119-
let m = [1, 2, 3]
120-
.iter()
121-
.filter_map(|x| Some(92))
92+
let m = core::iter::repeat(())
93+
.filter_map(|()| Some(92))
12294
.map(|x| x + 2)
123-
.len();
95+
.next();
12496
}
12597
"#,
12698
);
@@ -130,10 +102,10 @@ fn foo() {
130102
fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() {
131103
check_diagnostics(
132104
r#"
105+
//- minicore: iterators
133106
fn foo() {
134-
let m = [1, 2, 3]
135-
.iter()
136-
.filter_map(|x| Some(92));
107+
let m = core::iter::repeat(())
108+
.filter_map(|()| Some(92));
137109
let n = m.next();
138110
}
139111
"#,
@@ -144,34 +116,14 @@ fn foo() {
144116
fn replace_with_wind_map() {
145117
check_fix(
146118
r#"
147-
//- /main.rs crate:main deps:core
148-
use core::iter::Iterator;
149-
use core::option::Option::{self, Some, None};
119+
//- minicore: iterators
150120
fn foo() {
151-
let m = [1, 2, 3].iter().$0filter_map(|x| Some(92)).next();
152-
}
153-
//- /core/lib.rs crate:core
154-
pub mod option {
155-
pub enum Option<T> { Some(T), None }
156-
}
157-
pub 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-
}
121+
let m = core::iter::repeat(()).$0filter_map(|()| Some(92)).next();
168122
}
169123
"#,
170124
r#"
171-
use core::iter::Iterator;
172-
use core::option::Option::{self, Some, None};
173125
fn foo() {
174-
let m = [1, 2, 3].iter().find_map(|x| Some(92));
126+
let m = core::iter::repeat(()).find_map(|()| Some(92));
175127
}
176128
"#,
177129
)

crates/test_utils/src/minicore.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//! option:
2323
//! result:
2424
//! iterator: option
25-
//! iterators: iterator
25+
//! iterators: iterator, fn
2626
//! default: sized
2727
//! clone: sized
2828
//! copy: clone
@@ -390,7 +390,6 @@ pub mod iter {
390390
iter: I,
391391
n: usize,
392392
}
393-
394393
impl<I> Iterator for Take<I>
395394
where
396395
I: Iterator,
@@ -401,6 +400,22 @@ pub mod iter {
401400
loop {}
402401
}
403402
}
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+
}
404419
}
405420
pub use self::adapters::Take;
406421

@@ -448,6 +463,13 @@ pub mod iter {
448463
fn take(self, n: usize) -> crate::iter::Take<Self> {
449464
loop {}
450465
}
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+
}
451473
// endregion:iterators
452474
}
453475
impl<I: Iterator + ?Sized> Iterator for &mut I {

0 commit comments

Comments
 (0)