3 #![warn(clippy::or_fun_call)]
6 use std::collections::BTreeMap;
7 use std::collections::HashMap;
8 use std::time::Duration;
10 /// Checks implementation of the `OR_FUN_CALL` lint.
28 let with_enum = Some(Enum::A(1));
29 with_enum.unwrap_or(Enum::A(5));
31 let with_const_fn = Some(Duration::from_secs(1));
32 with_const_fn.unwrap_or_else(|| Duration::from_secs(5));
34 let with_constructor = Some(vec![1]);
35 with_constructor.unwrap_or_else(make);
37 let with_new = Some(vec![1]);
38 with_new.unwrap_or_default();
40 let with_const_args = Some(vec![1]);
41 with_const_args.unwrap_or_else(|| Vec::with_capacity(12));
43 let with_err: Result<_, ()> = Ok(vec![1]);
44 with_err.unwrap_or_else(|_| make());
46 let with_err_args: Result<_, ()> = Ok(vec![1]);
47 with_err_args.unwrap_or_else(|_| Vec::with_capacity(12));
49 let with_default_trait = Some(1);
50 with_default_trait.unwrap_or_default();
52 let with_default_type = Some(1);
53 with_default_type.unwrap_or_default();
55 let with_vec = Some(vec![1]);
56 with_vec.unwrap_or_default();
58 let without_default = Some(Foo);
59 without_default.unwrap_or_else(Foo::new);
61 let stringy = Some(String::from(""));
62 let _ = stringy.unwrap_or_else(|| "".to_owned());
66 let _ = opt.ok_or(format!("{} world.", hello));
70 struct Bar(String, Duration);
72 fn test_or_with_ctors() {
74 let opt_opt = Some(Some(1));
75 // we also test for const promotion, this makes sure we don't hit that
78 let _ = opt_opt.unwrap_or(Some(2));
79 let _ = opt_opt.unwrap_or(Some(two));
80 let _ = opt.ok_or(Some(2));
81 let _ = opt.ok_or(Some(two));
82 let _ = opt.ok_or(Foo(2));
83 let _ = opt.ok_or(Foo(two));
84 let _ = opt.or(Some(2));
85 let _ = opt.or(Some(two));
87 let _ = Some("a".to_string()).or_else(|| Some("b".to_string()));
89 let b = "b".to_string();
90 let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
91 .or_else(|| Some(Bar(b, Duration::from_secs(2))));
93 let vec = vec!["foo"];
94 let _ = opt.ok_or(vec.len());
97 let _ = opt.ok_or(array.len());
99 let slice = &["foo"][..];
100 let _ = opt.ok_or(slice.len());
103 // Issue 4514 - early return
104 fn f() -> Option<()> {
108 let _ = a.unwrap_or(b.checked_mul(3)?.min(240));
113 // Issue 5886 - const fn (with no arguments)
114 pub fn skip_const_fn_with_no_args() {
115 const fn foo() -> Option<i32> {
118 let _ = None.or(foo());
121 let mut map = std::collections::HashMap::new();
122 map.insert(1, vec![1]);
123 map.entry(1).or_insert(vec![]);
125 let mut map = HashMap::<u64, String>::new();
126 map.entry(42).or_insert(String::new());
128 let mut btree = BTreeMap::<u64, String>::new();
129 btree.entry(42).or_insert(String::new());