mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
minor: use minicore
This commit is contained in:
parent
a9623f3165
commit
90da9fc9b3
@ -55,44 +55,16 @@ fn fixes(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests::check_fix;
|
use crate::tests::{check_diagnostics, check_fix};
|
||||||
|
|
||||||
// Register the required standard library types to make the tests work
|
|
||||||
#[track_caller]
|
|
||||||
fn check_diagnostics(ra_fixture: &str) {
|
|
||||||
let prefix = r#"
|
|
||||||
//- /main.rs crate:main deps:core
|
|
||||||
use core::iter::Iterator;
|
|
||||||
use core::option::Option::{self, Some, None};
|
|
||||||
"#;
|
|
||||||
let suffix = r#"
|
|
||||||
//- /core/lib.rs crate:core
|
|
||||||
pub mod option {
|
|
||||||
pub enum Option<T> { Some(T), None }
|
|
||||||
}
|
|
||||||
pub mod iter {
|
|
||||||
pub trait Iterator {
|
|
||||||
type Item;
|
|
||||||
fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
|
|
||||||
fn next(&mut self) -> Option<Self::Item>;
|
|
||||||
}
|
|
||||||
pub struct FilterMap {}
|
|
||||||
impl Iterator for FilterMap {
|
|
||||||
type Item = i32;
|
|
||||||
fn next(&mut self) -> i32 { 7 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"#;
|
|
||||||
crate::tests::check_diagnostics(&format!("{}{}{}", prefix, ra_fixture, suffix))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn replace_filter_map_next_with_find_map2() {
|
fn replace_filter_map_next_with_find_map2() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
//- minicore: iterators
|
||||||
let m = [1, 2, 3].iter().filter_map(|x| Some(92)).next();
|
fn foo() {
|
||||||
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..)
|
let m = core::iter::repeat(()).filter_map(|()| Some(92)).next();
|
||||||
|
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..)
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -101,11 +73,11 @@ pub mod iter {
|
|||||||
fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() {
|
fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: iterators
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let m = [1, 2, 3]
|
let m = core::iter::repeat(())
|
||||||
.iter()
|
.filter_map(|()| Some(92))
|
||||||
.filter_map(|x| Some(92))
|
.count();
|
||||||
.len();
|
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
@ -115,12 +87,12 @@ fn foo() {
|
|||||||
fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
|
fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: iterators
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let m = [1, 2, 3]
|
let m = core::iter::repeat(())
|
||||||
.iter()
|
.filter_map(|()| Some(92))
|
||||||
.filter_map(|x| Some(92))
|
|
||||||
.map(|x| x + 2)
|
.map(|x| x + 2)
|
||||||
.len();
|
.next();
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
@ -130,10 +102,10 @@ fn foo() {
|
|||||||
fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() {
|
fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() {
|
||||||
check_diagnostics(
|
check_diagnostics(
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: iterators
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let m = [1, 2, 3]
|
let m = core::iter::repeat(())
|
||||||
.iter()
|
.filter_map(|()| Some(92));
|
||||||
.filter_map(|x| Some(92));
|
|
||||||
let n = m.next();
|
let n = m.next();
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
@ -144,34 +116,14 @@ fn foo() {
|
|||||||
fn replace_with_wind_map() {
|
fn replace_with_wind_map() {
|
||||||
check_fix(
|
check_fix(
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs crate:main deps:core
|
//- minicore: iterators
|
||||||
use core::iter::Iterator;
|
|
||||||
use core::option::Option::{self, Some, None};
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let m = [1, 2, 3].iter().$0filter_map(|x| Some(92)).next();
|
let m = core::iter::repeat(()).$0filter_map(|()| Some(92)).next();
|
||||||
}
|
|
||||||
//- /core/lib.rs crate:core
|
|
||||||
pub mod option {
|
|
||||||
pub enum Option<T> { Some(T), None }
|
|
||||||
}
|
|
||||||
pub mod iter {
|
|
||||||
pub trait Iterator {
|
|
||||||
type Item;
|
|
||||||
fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
|
|
||||||
fn next(&mut self) -> Option<Self::Item>;
|
|
||||||
}
|
|
||||||
pub struct FilterMap {}
|
|
||||||
impl Iterator for FilterMap {
|
|
||||||
type Item = i32;
|
|
||||||
fn next(&mut self) -> i32 { 7 }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
use core::iter::Iterator;
|
|
||||||
use core::option::Option::{self, Some, None};
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let m = [1, 2, 3].iter().find_map(|x| Some(92));
|
let m = core::iter::repeat(()).find_map(|()| Some(92));
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
//! option:
|
//! option:
|
||||||
//! result:
|
//! result:
|
||||||
//! iterator: option
|
//! iterator: option
|
||||||
//! iterators: iterator
|
//! iterators: iterator, fn
|
||||||
//! default: sized
|
//! default: sized
|
||||||
//! clone: sized
|
//! clone: sized
|
||||||
//! copy: clone
|
//! copy: clone
|
||||||
@ -390,7 +390,6 @@ pub mod iter {
|
|||||||
iter: I,
|
iter: I,
|
||||||
n: usize,
|
n: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I> Iterator for Take<I>
|
impl<I> Iterator for Take<I>
|
||||||
where
|
where
|
||||||
I: Iterator,
|
I: Iterator,
|
||||||
@ -401,6 +400,22 @@ pub mod iter {
|
|||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct FilterMap<I, F> {
|
||||||
|
iter: I,
|
||||||
|
f: F,
|
||||||
|
}
|
||||||
|
impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
|
||||||
|
where
|
||||||
|
F: FnMut(I::Item) -> Option<B>,
|
||||||
|
{
|
||||||
|
type Item = B;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn next(&mut self) -> Option<B> {
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub use self::adapters::Take;
|
pub use self::adapters::Take;
|
||||||
|
|
||||||
@ -448,6 +463,13 @@ pub mod iter {
|
|||||||
fn take(self, n: usize) -> crate::iter::Take<Self> {
|
fn take(self, n: usize) -> crate::iter::Take<Self> {
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
fn filter_map<B, F>(self, f: F) -> crate::iter::FilterMap<Self, F>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(Self::Item) -> Option<B>,
|
||||||
|
{
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
// endregion:iterators
|
// endregion:iterators
|
||||||
}
|
}
|
||||||
impl<I: Iterator + ?Sized> Iterator for &mut I {
|
impl<I: Iterator + ?Sized> Iterator for &mut I {
|
||||||
|
Loading…
Reference in New Issue
Block a user