]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/or_fun_call.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / or_fun_call.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2
3#![warn(clippy::or_fun_call)]
4#![allow(dead_code)]
5#![allow(clippy::unnecessary_wraps)]
6
7use std::collections::BTreeMap;
8use std::collections::HashMap;
9use std::time::Duration;
10
11/// Checks implementation of the `OR_FUN_CALL` lint.
12fn 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 map_vec = HashMap::<u64, Vec<i32>>::new();
66 map_vec.entry(42).or_insert(vec![]);
67
68 let mut btree = BTreeMap::<u64, String>::new();
69 btree.entry(42).or_insert(String::new());
70
71 let mut btree_vec = BTreeMap::<u64, Vec<i32>>::new();
72 btree_vec.entry(42).or_insert(vec![]);
73
74 let stringy = Some(String::from(""));
75 let _ = stringy.unwrap_or("".to_owned());
76
77 let opt = Some(1);
78 let hello = "Hello";
79 let _ = opt.ok_or(format!("{} world.", hello));
80
81 // index
82 let map = HashMap::<u64, u64>::new();
83 let _ = Some(1).unwrap_or(map[&1]);
84 let map = BTreeMap::<u64, u64>::new();
85 let _ = Some(1).unwrap_or(map[&1]);
86 // don't lint index vec
87 let vec = vec![1];
88 let _ = Some(1).unwrap_or(vec[1]);
89}
90
91struct Foo(u8);
92struct Bar(String, Duration);
93#[rustfmt::skip]
94fn test_or_with_ctors() {
95 let opt = Some(1);
96 let opt_opt = Some(Some(1));
97 // we also test for const promotion, this makes sure we don't hit that
98 let two = 2;
99
100 let _ = opt_opt.unwrap_or(Some(2));
101 let _ = opt_opt.unwrap_or(Some(two));
102 let _ = opt.ok_or(Some(2));
103 let _ = opt.ok_or(Some(two));
104 let _ = opt.ok_or(Foo(2));
105 let _ = opt.ok_or(Foo(two));
106 let _ = opt.or(Some(2));
107 let _ = opt.or(Some(two));
108
109 let _ = Some("a".to_string()).or(Some("b".to_string()));
110
111 let b = "b".to_string();
112 let _ = Some(Bar("a".to_string(), Duration::from_secs(1)))
113 .or(Some(Bar(b, Duration::from_secs(2))));
114
115 let vec = vec!["foo"];
116 let _ = opt.ok_or(vec.len());
117
118 let array = ["foo"];
119 let _ = opt.ok_or(array.len());
120
121 let slice = &["foo"][..];
122 let _ = opt.ok_or(slice.len());
cdc7bbd5
XL
123
124 let string = "foo";
125 let _ = opt.ok_or(string.len());
f20569fa
XL
126}
127
128// Issue 4514 - early return
129fn f() -> Option<()> {
130 let a = Some(1);
131 let b = 1i32;
132
133 let _ = a.unwrap_or(b.checked_mul(3)?.min(240));
134
135 Some(())
136}
137
cdc7bbd5
XL
138mod issue6675 {
139 unsafe fn foo() {
140 let mut s = "test".to_owned();
141 None.unwrap_or(s.as_mut_vec());
142 }
143
144 fn bar() {
145 let mut s = "test".to_owned();
146 None.unwrap_or(unsafe { s.as_mut_vec() });
147 #[rustfmt::skip]
148 None.unwrap_or( unsafe { s.as_mut_vec() } );
149 }
150}
151
f20569fa 152fn main() {}