From 181184a350046a460441209fd34821c2c796b066 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 23:28:37 +0300 Subject: minor: use minicore --- .../src/handlers/replace_if_let_with_match.rs | 66 ++++++---------------- .../src/handlers/replace_unwrap_with_match.rs | 53 ++++++++--------- 2 files changed, 43 insertions(+), 76 deletions(-) diff --git a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs index 9404aa26d..f37aa0d53 100644 --- a/crates/ide_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ide_assists/src/handlers/replace_if_let_with_match.rs @@ -262,9 +262,7 @@ impl VariantData { check_assist( replace_if_let_with_match, r#" -enum Option { Some(T), None } -use Option::*; - +//- minicore: option fn foo(x: Option) { $0if let Some(x) = x { println!("{}", x) @@ -272,18 +270,15 @@ fn foo(x: Option) { println!("none") } } - "#, +"#, r#" -enum Option { Some(T), None } -use Option::*; - fn foo(x: Option) { match x { Some(x) => println!("{}", x), None => println!("none"), } } - "#, +"#, ); } @@ -292,9 +287,7 @@ fn foo(x: Option) { check_assist( replace_if_let_with_match, r#" -enum Option { Some(T), None } -use Option::*; - +//- minicore: option fn foo(x: Option) { $0if let None = x { println!("none") @@ -302,18 +295,15 @@ fn foo(x: Option) { println!("some") } } - "#, +"#, r#" -enum Option { Some(T), None } -use Option::*; - fn foo(x: Option) { match x { None => println!("none"), Some(_) => println!("some"), } } - "#, +"#, ); } @@ -322,9 +312,7 @@ fn foo(x: Option) { check_assist( replace_if_let_with_match, r#" -enum Result { Ok(T), Err(E) } -use Result::*; - +//- minicore: result fn foo(x: Result) { $0if let Ok(x) = x { println!("{}", x) @@ -332,18 +320,15 @@ fn foo(x: Result) { println!("none") } } - "#, +"#, r#" -enum Result { Ok(T), Err(E) } -use Result::*; - fn foo(x: Result) { match x { Ok(x) => println!("{}", x), Err(_) => println!("none"), } } - "#, +"#, ); } @@ -352,9 +337,7 @@ fn foo(x: Result) { check_assist( replace_if_let_with_match, r#" -enum Result { Ok(T), Err(E) } -use Result::*; - +//- minicore: result fn foo(x: Result) { $0if let Err(x) = x { println!("{}", x) @@ -362,18 +345,15 @@ fn foo(x: Result) { println!("ok") } } - "#, +"#, r#" -enum Result { Ok(T), Err(E) } -use Result::*; - fn foo(x: Result) { match x { Err(x) => println!("{}", x), Ok(_) => println!("ok"), } } - "#, +"#, ); } @@ -488,20 +468,15 @@ impl VariantData { check_assist( replace_match_with_if_let, r#" -enum Option { Some(T), None } -use Option::*; - +//- minicore: option fn foo(x: Option) { $0match x { Some(x) => println!("{}", x), None => println!("none"), } } - "#, +"#, r#" -enum Option { Some(T), None } -use Option::*; - fn foo(x: Option) { if let Some(x) = x { println!("{}", x) @@ -509,7 +484,7 @@ fn foo(x: Option) { println!("none") } } - "#, +"#, ); } @@ -518,20 +493,15 @@ fn foo(x: Option) { check_assist( replace_match_with_if_let, r#" -enum Result { Ok(T), Err(E) } -use Result::*; - +//- minicore: result fn foo(x: Result) { $0match x { Ok(x) => println!("{}", x), Err(_) => println!("none"), } } - "#, +"#, r#" -enum Result { Ok(T), Err(E) } -use Result::*; - fn foo(x: Result) { if let Ok(x) = x { println!("{}", x) @@ -539,7 +509,7 @@ fn foo(x: Result) { println!("none") } } - "#, +"#, ); } diff --git a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs index a3bfa221c..7e57353c6 100644 --- a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs @@ -97,25 +97,24 @@ mod tests { fn test_replace_result_unwrap_with_match() { check_assist( replace_unwrap_with_match, - r" -enum Result { Ok(T), Err(E) } + r#" +//- minicore: result fn i(a: T) -> T { a } fn main() { - let x: Result = Result::Ok(92); + let x: Result = Ok(92); let y = i(x).$0unwrap(); } - ", - r" -enum Result { Ok(T), Err(E) } +"#, + r#" fn i(a: T) -> T { a } fn main() { - let x: Result = Result::Ok(92); + let x: Result = Ok(92); let y = match i(x) { Ok(it) => it, $0_ => unreachable!(), }; } - ", +"#, ) } @@ -123,25 +122,24 @@ fn main() { fn test_replace_option_unwrap_with_match() { check_assist( replace_unwrap_with_match, - r" -enum Option { Some(T), None } + r#" +//- minicore: option fn i(a: T) -> T { a } fn main() { - let x = Option::Some(92); + let x = Some(92); let y = i(x).$0unwrap(); } - ", - r" -enum Option { Some(T), None } +"#, + r#" fn i(a: T) -> T { a } fn main() { - let x = Option::Some(92); + let x = Some(92); let y = match i(x) { Some(it) => it, $0_ => unreachable!(), }; } - ", +"#, ); } @@ -149,25 +147,24 @@ fn main() { fn test_replace_result_unwrap_with_match_chaining() { check_assist( replace_unwrap_with_match, - r" -enum Result { Ok(T), Err(E) } + r#" +//- minicore: result fn i(a: T) -> T { a } fn main() { - let x: Result = Result::Ok(92); + let x: Result = Ok(92); let y = i(x).$0unwrap().count_zeroes(); } - ", - r" -enum Result { Ok(T), Err(E) } +"#, + r#" fn i(a: T) -> T { a } fn main() { - let x: Result = Result::Ok(92); + let x: Result = Ok(92); let y = match i(x) { Ok(it) => it, $0_ => unreachable!(), }.count_zeroes(); } - ", +"#, ) } @@ -175,14 +172,14 @@ fn main() { fn replace_unwrap_with_match_target() { check_assist_target( replace_unwrap_with_match, - r" -enum Option { Some(T), None } + r#" +//- minicore: option fn i(a: T) -> T { a } fn main() { - let x = Option::Some(92); + let x = Some(92); let y = i(x).$0unwrap(); } - ", +"#, r"i(x).unwrap()", ); } -- cgit v1.2.3 From cc73abf72c46d9f13a176d93d1d38f3a72d638e3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 23:33:01 +0300 Subject: minor: use minicore --- crates/ide/src/inlay_hints.rs | 4 +- crates/ide_completion/src/completions/postfix.rs | 18 ++--- .../handlers/missing_ok_or_some_in_tail_expr.rs | 76 ++-------------------- 3 files changed, 13 insertions(+), 85 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 335d57a0d..48bab0b2e 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -908,9 +908,7 @@ fn main() { fn unit_structs_have_no_type_hints() { check_types( r#" -enum Result { Ok(T), Err(E) } -use Result::*; - +//- minicore: result struct SyntheticSyntax; fn main() { diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs index c3c7e4589..c11deebcb 100644 --- a/crates/ide_completion/src/completions/postfix.rs +++ b/crates/ide_completion/src/completions/postfix.rs @@ -436,18 +436,15 @@ fn main() { check_edit( "ifl", r#" -enum Option { Some(T), None } - +//- minicore: option fn main() { - let bar = Option::Some(true); + let bar = Some(true); bar.$0 } "#, r#" -enum Option { Some(T), None } - fn main() { - let bar = Option::Some(true); + let bar = Some(true); if let Some($1) = bar { $0 } @@ -461,18 +458,15 @@ fn main() { check_edit( "match", r#" -enum Result { Ok(T), Err(E) } - +//- minicore: result fn main() { - let bar = Result::Ok(true); + let bar = Ok(true); bar.$0 } "#, r#" -enum Result { Ok(T), Err(E) } - fn main() { - let bar = Result::Ok(true); + let bar = Ok(true); match bar { Ok(${1:_}) => {$2}, Err(${3:_}) => {$0}, diff --git a/crates/ide_diagnostics/src/handlers/missing_ok_or_some_in_tail_expr.rs b/crates/ide_diagnostics/src/handlers/missing_ok_or_some_in_tail_expr.rs index 63de54570..c0edcd7d3 100644 --- a/crates/ide_diagnostics/src/handlers/missing_ok_or_some_in_tail_expr.rs +++ b/crates/ide_diagnostics/src/handlers/missing_ok_or_some_in_tail_expr.rs @@ -49,26 +49,15 @@ mod tests { fn test_wrap_return_type_option() { check_fix( r#" -//- /main.rs crate:main deps:core -use core::option::Option::{self, Some, None}; - +//- minicore: option, result fn div(x: i32, y: i32) -> Option { if y == 0 { return None; } x / y$0 } -//- /core/lib.rs crate:core -pub mod result { - pub enum Result { Ok(T), Err(E) } -} -pub mod option { - pub enum Option { Some(T), None } -} "#, r#" -use core::option::Option::{self, Some, None}; - fn div(x: i32, y: i32) -> Option { if y == 0 { return None; @@ -83,26 +72,15 @@ fn div(x: i32, y: i32) -> Option { fn test_wrap_return_type() { check_fix( r#" -//- /main.rs crate:main deps:core -use core::result::Result::{self, Ok, Err}; - +//- minicore: option, result fn div(x: i32, y: i32) -> Result { if y == 0 { return Err(()); } x / y$0 } -//- /core/lib.rs crate:core -pub mod result { - pub enum Result { Ok(T), Err(E) } -} -pub mod option { - pub enum Option { Some(T), None } -} "#, r#" -use core::result::Result::{self, Ok, Err}; - fn div(x: i32, y: i32) -> Result { if y == 0 { return Err(()); @@ -117,26 +95,15 @@ fn div(x: i32, y: i32) -> Result { fn test_wrap_return_type_handles_generic_functions() { check_fix( r#" -//- /main.rs crate:main deps:core -use core::result::Result::{self, Ok, Err}; - +//- minicore: option, result fn div(x: T) -> Result { if x == 0 { return Err(7); } $0x } -//- /core/lib.rs crate:core -pub mod result { - pub enum Result { Ok(T), Err(E) } -} -pub mod option { - pub enum Option { Some(T), None } -} "#, r#" -use core::result::Result::{self, Ok, Err}; - fn div(x: T) -> Result { if x == 0 { return Err(7); @@ -151,9 +118,7 @@ fn div(x: T) -> Result { fn test_wrap_return_type_handles_type_aliases() { check_fix( r#" -//- /main.rs crate:main deps:core -use core::result::Result::{self, Ok, Err}; - +//- minicore: option, result type MyResult = Result; fn div(x: i32, y: i32) -> MyResult { @@ -162,17 +127,8 @@ fn div(x: i32, y: i32) -> MyResult { } x $0/ y } -//- /core/lib.rs crate:core -pub mod result { - pub enum Result { Ok(T), Err(E) } -} -pub mod option { - pub enum Option { Some(T), None } -} "#, r#" -use core::result::Result::{self, Ok, Err}; - type MyResult = Result; fn div(x: i32, y: i32) -> MyResult { @@ -189,18 +145,8 @@ fn div(x: i32, y: i32) -> MyResult { fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() { check_diagnostics( r#" -//- /main.rs crate:main deps:core -use core::result::Result::{self, Ok, Err}; - +//- minicore: option, result fn foo() -> Result<(), i32> { 0 } - -//- /core/lib.rs crate:core -pub mod result { - pub enum Result { Ok(T), Err(E) } -} -pub mod option { - pub enum Option { Some(T), None } -} "#, ); } @@ -209,20 +155,10 @@ pub mod option { fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() { check_diagnostics( r#" -//- /main.rs crate:main deps:core -use core::result::Result::{self, Ok, Err}; - +//- minicore: option, result enum SomeOtherEnum { Ok(i32), Err(String) } fn foo() -> SomeOtherEnum { 0 } - -//- /core/lib.rs crate:core -pub mod result { - pub enum Result { Ok(T), Err(E) } -} -pub mod option { - pub enum Option { Some(T), None } -} "#, ); } -- cgit v1.2.3 From a9623f3165f87124de836e81e238118e557bb9e9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 23:38:19 +0300 Subject: minor: use minicore --- crates/ide/src/inlay_hints.rs | 17 ++++------------- crates/ide_assists/src/handlers/fill_match_arms.rs | 11 +++-------- crates/ide_completion/src/completions/postfix.rs | 9 +++------ 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 48bab0b2e..95f9edce4 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -661,9 +661,7 @@ fn main() { fn function_call_parameter_hint() { check_params( r#" -enum Option { None, Some(T) } -use Option::*; - +//- minicore: option struct FileId {} struct SmolStr {} @@ -872,7 +870,6 @@ fn main() { check_types( r#" //- minicore: fn, sized - fn foo() -> impl Fn() { loop {} } fn foo1() -> impl Fn(f64) { loop {} } fn foo2() -> impl Fn(f64, f64) { loop {} } @@ -960,9 +957,7 @@ fn main() { fn if_expr() { check_types( r#" -enum Option { None, Some(T) } -use Option::*; - +//- minicore: option struct Test { a: Option, b: u8 } fn main() { @@ -992,9 +987,7 @@ fn main() { fn while_expr() { check_types( r#" -enum Option { None, Some(T) } -use Option::*; - +//- minicore: option struct Test { a: Option, b: u8 } fn main() { @@ -1010,9 +1003,7 @@ fn main() { fn match_arm_list() { check_types( r#" -enum Option { None, Some(T) } -use Option::*; - +//- minicore: option struct Test { a: Option, b: u8 } fn main() { diff --git a/crates/ide_assists/src/handlers/fill_match_arms.rs b/crates/ide_assists/src/handlers/fill_match_arms.rs index cd0f6dba9..318faa0fc 100644 --- a/crates/ide_assists/src/handlers/fill_match_arms.rs +++ b/crates/ide_assists/src/handlers/fill_match_arms.rs @@ -481,26 +481,21 @@ fn main() { check_assist( fill_match_arms, r#" -enum Option { Some(T), None } -use Option::*; - +//- minicore: option fn main() { match None$0 { None => {} } } - "#, +"#, r#" -enum Option { Some(T), None } -use Option::*; - fn main() { match None { None => {} Some(${0:_}) => todo!(), } } - "#, +"#, ); } diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs index c11deebcb..4e20ec003 100644 --- a/crates/ide_completion/src/completions/postfix.rs +++ b/crates/ide_completion/src/completions/postfix.rs @@ -509,18 +509,15 @@ fn main() { check_edit( "ifl", r#" -enum Option { Some(T), None } - +//- minicore: option fn main() { - let bar = &Option::Some(true); + let bar = &Some(true); bar.$0 } "#, r#" -enum Option { Some(T), None } - fn main() { - let bar = &Option::Some(true); + let bar = &Some(true); if let Some($1) = bar { $0 } -- cgit v1.2.3 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(-) 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