minor: use minicore

This commit is contained in:
Aleksey Kladov 2021-06-18 23:48:18 +03:00
parent a9623f3165
commit 90da9fc9b3
2 changed files with 43 additions and 69 deletions
crates
ide_diagnostics/src/handlers
test_utils/src

@ -55,44 +55,16 @@ fn fixes(
#[cfg(test)]
mod tests {
use crate::tests::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))
}
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn replace_filter_map_next_with_find_map2() {
check_diagnostics(
r#"
fn foo() {
let m = [1, 2, 3].iter().filter_map(|x| Some(92)).next();
} //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 💡 weak: replace filter_map(..).next() with find_map(..)
//- minicore: iterators
fn foo() {
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() {
check_diagnostics(
r#"
//- minicore: iterators
fn foo() {
let m = [1, 2, 3]
.iter()
.filter_map(|x| Some(92))
.len();
let m = core::iter::repeat(())
.filter_map(|()| Some(92))
.count();
}
"#,
);
@ -115,12 +87,12 @@ fn foo() {
fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
check_diagnostics(
r#"
//- minicore: iterators
fn foo() {
let m = [1, 2, 3]
.iter()
.filter_map(|x| Some(92))
let m = core::iter::repeat(())
.filter_map(|()| Some(92))
.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() {
check_diagnostics(
r#"
//- minicore: iterators
fn foo() {
let m = [1, 2, 3]
.iter()
.filter_map(|x| Some(92));
let m = core::iter::repeat(())
.filter_map(|()| Some(92));
let n = m.next();
}
"#,
@ -144,34 +116,14 @@ fn foo() {
fn replace_with_wind_map() {
check_fix(
r#"
//- /main.rs crate:main deps:core
use core::iter::Iterator;
use core::option::Option::{self, Some, None};
//- minicore: iterators
fn foo() {
let m = [1, 2, 3].iter().$0filter_map(|x| 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 }
}
let m = core::iter::repeat(()).$0filter_map(|()| Some(92)).next();
}
"#,
r#"
use core::iter::Iterator;
use core::option::Option::{self, Some, None};
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:
//! result:
//! iterator: option
//! iterators: iterator
//! iterators: iterator, fn
//! default: sized
//! clone: sized
//! copy: clone
@ -390,7 +390,6 @@ pub mod iter {
iter: I,
n: usize,
}
impl<I> Iterator for Take<I>
where
I: Iterator,
@ -401,6 +400,22 @@ pub mod iter {
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;
@ -448,6 +463,13 @@ pub mod iter {
fn take(self, n: usize) -> crate::iter::Take<Self> {
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
}
impl<I: Iterator + ?Sized> Iterator for &mut I {