X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=tests%2Fui%2For_fun_call.rs;h=7599b945a9137ed63a21cc06ad70aeed0f5f6532;hb=79d152190cb8ddce0da420929c0a9dcc47727ba9;hp=1b4732b5b5646dcb4440c617172729d1a36314f3;hpb=910d538ef1f643334974aa0c2f45ff44a74d92b1;p=rust.git diff --git a/tests/ui/or_fun_call.rs b/tests/ui/or_fun_call.rs index 1b4732b5b56..7599b945a91 100644 --- a/tests/ui/or_fun_call.rs +++ b/tests/ui/or_fun_call.rs @@ -1,7 +1,11 @@ +// run-rustfix + #![warn(clippy::or_fun_call)] +#![allow(dead_code)] use std::collections::BTreeMap; use std::collections::HashMap; +use std::time::Duration; /// Checks implementation of the `OR_FUN_CALL` lint. fn or_fun_call() { @@ -24,8 +28,8 @@ fn make() -> T { let with_enum = Some(Enum::A(1)); with_enum.unwrap_or(Enum::A(5)); - let with_const_fn = Some(::std::time::Duration::from_secs(1)); - with_const_fn.unwrap_or(::std::time::Duration::from_secs(5)); + let with_const_fn = Some(Duration::from_secs(1)); + with_const_fn.unwrap_or(Duration::from_secs(5)); let with_constructor = Some(vec![1]); with_constructor.unwrap_or(make()); @@ -51,8 +55,6 @@ fn make() -> T { let with_vec = Some(vec![1]); with_vec.unwrap_or(vec![]); - // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]); - let without_default = Some(Foo); without_default.unwrap_or(Foo::new()); @@ -70,4 +72,39 @@ fn make() -> T { let _ = opt.ok_or(format!("{} world.", hello)); } +struct Foo(u8); +struct Bar(String, Duration); +#[rustfmt::skip] +fn test_or_with_ctors() { + let opt = Some(1); + let opt_opt = Some(Some(1)); + // we also test for const promotion, this makes sure we don't hit that + let two = 2; + + let _ = opt_opt.unwrap_or(Some(2)); + let _ = opt_opt.unwrap_or(Some(two)); + let _ = opt.ok_or(Some(2)); + let _ = opt.ok_or(Some(two)); + let _ = opt.ok_or(Foo(2)); + let _ = opt.ok_or(Foo(two)); + let _ = opt.or(Some(2)); + let _ = opt.or(Some(two)); + + let _ = Some("a".to_string()).or(Some("b".to_string())); + + let b = "b".to_string(); + let _ = Some(Bar("a".to_string(), Duration::from_secs(1))) + .or(Some(Bar(b, Duration::from_secs(2)))); +} + +// Issue 4514 - early return +fn f() -> Option<()> { + let a = Some(1); + let b = 1i32; + + let _ = a.unwrap_or(b.checked_mul(3)?.min(240)); + + Some(()) +} + fn main() {}