]> git.lizzy.rs Git - rust.git/blob - tests/ui/or_fun_call.rs
Auto merge of #6476 - 1c3t3a:1c3t3a-from-over-into, r=llogiq
[rust.git] / tests / ui / or_fun_call.rs
1 // run-rustfix
2
3 #![warn(clippy::or_fun_call)]
4 #![allow(dead_code)]
5 #![allow(clippy::unnecessary_wraps)]
6
7 use std::collections::BTreeMap;
8 use std::collections::HashMap;
9 use std::time::Duration;
10
11 /// Checks implementation of the `OR_FUN_CALL` lint.
12 fn or_fun_call() {
13     struct Foo;
14
15     impl Foo {
16         fn new() -> Foo {
17             Foo
18         }
19     }
20
21     enum Enum {
22         A(i32),
23     }
24
25     fn make<T>() -> T {
26         unimplemented!();
27     }
28
29     let with_enum = Some(Enum::A(1));
30     with_enum.unwrap_or(Enum::A(5));
31
32     let with_const_fn = Some(Duration::from_secs(1));
33     with_const_fn.unwrap_or(Duration::from_secs(5));
34
35     let with_constructor = Some(vec![1]);
36     with_constructor.unwrap_or(make());
37
38     let with_new = Some(vec![1]);
39     with_new.unwrap_or(Vec::new());
40
41     let with_const_args = Some(vec![1]);
42     with_const_args.unwrap_or(Vec::with_capacity(12));
43
44     let with_err: Result<_, ()> = Ok(vec![1]);
45     with_err.unwrap_or(make());
46
47     let with_err_args: Result<_, ()> = Ok(vec![1]);
48     with_err_args.unwrap_or(Vec::with_capacity(12));
49
50     let with_default_trait = Some(1);
51     with_default_trait.unwrap_or(Default::default());
52
53     let with_default_type = Some(1);
54     with_default_type.unwrap_or(u64::default());
55
56     let with_vec = Some(vec![1]);
57     with_vec.unwrap_or(vec![]);
58
59     let without_default = Some(Foo);
60     without_default.unwrap_or(Foo::new());
61
62     let mut map = HashMap::<u64, String>::new();
63     map.entry(42).or_insert(String::new());
64
65     let mut btree = BTreeMap::<u64, String>::new();
66     btree.entry(42).or_insert(String::new());
67
68     let stringy = Some(String::from(""));
69     let _ = stringy.unwrap_or("".to_owned());
70
71     let opt = Some(1);
72     let hello = "Hello";
73     let _ = opt.ok_or(format!("{} world.", hello));
74
75     // index
76     let map = HashMap::<u64, u64>::new();
77     let _ = Some(1).unwrap_or(map[&1]);
78     let map = BTreeMap::<u64, u64>::new();
79     let _ = Some(1).unwrap_or(map[&1]);
80     // don't lint index vec
81     let vec = vec![1];
82     let _ = Some(1).unwrap_or(vec[1]);
83 }
84
85 struct Foo(u8);
86 struct Bar(String, Duration);
87 #[rustfmt::skip]
88 fn test_or_with_ctors() {
89     let opt = Some(1);
90     let opt_opt = Some(Some(1));
91     // we also test for const promotion, this makes sure we don't hit that
92     let two = 2;
93
94     let _ = opt_opt.unwrap_or(Some(2));
95     let _ = opt_opt.unwrap_or(Some(two));
96     let _ = opt.ok_or(Some(2));
97     let _ = opt.ok_or(Some(two));
98     let _ = opt.ok_or(Foo(2));
99     let _ = opt.ok_or(Foo(two));
100     let _ = opt.or(Some(2));
101     let _ = opt.or(Some(two));
102
103     let _ = Some("a".to_string()).or(Some("b".to_string()));
104
105     let b = "b".to_string();
106     let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
107         .or(Some(Bar(b, Duration::from_secs(2))));
108
109     let vec = vec!["foo"];
110     let _ = opt.ok_or(vec.len());
111
112     let array = ["foo"];
113     let _ = opt.ok_or(array.len());
114
115     let slice = &["foo"][..];
116     let _ = opt.ok_or(slice.len());
117 }
118
119 // Issue 4514 - early return
120 fn f() -> Option<()> {
121     let a = Some(1);
122     let b = 1i32;
123
124     let _ = a.unwrap_or(b.checked_mul(3)?.min(240));
125
126     Some(())
127 }
128
129 fn main() {}