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