From 90da9fc9b302de46097065f0d6428ad33c292217 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 23:48:18 +0300 Subject: minor: use minicore --- .../replace_filter_map_next_with_find_map.rs | 86 +++++----------------- crates/test_utils/src/minicore.rs | 26 ++++++- 2 files changed, 43 insertions(+), 69 deletions(-) (limited to 'crates') 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( #[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 { Some(T), None } -} -pub mod iter { - pub trait Iterator { - type Item; - fn filter_map(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option { FilterMap } - fn next(&mut self) -> Option; - } - 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 { Some(T), None } -} -pub mod iter { - pub trait Iterator { - type Item; - fn filter_map(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option { FilterMap } - fn next(&mut self) -> Option; - } - 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)); } "#, ) 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 @@ //! 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 Iterator for Take where I: Iterator, @@ -401,6 +400,22 @@ pub mod iter { loop {} } } + + pub struct FilterMap { + iter: I, + f: F, + } + impl Iterator for FilterMap + where + F: FnMut(I::Item) -> Option, + { + type Item = B; + + #[inline] + fn next(&mut self) -> Option { + loop {} + } + } } pub use self::adapters::Take; @@ -448,6 +463,13 @@ pub mod iter { fn take(self, n: usize) -> crate::iter::Take { loop {} } + fn filter_map(self, f: F) -> crate::iter::FilterMap + where + Self: Sized, + F: FnMut(Self::Item) -> Option, + { + loop {} + } // endregion:iterators } impl Iterator for &mut I { -- cgit v1.2.3