]>
git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/or_fun_call.rs
3 #![warn(clippy::or_fun_call)]
5 #![allow(clippy::unnecessary_wraps)]
7 use std
::collections
::BTreeMap
;
8 use std
::collections
::HashMap
;
9 use std
::time
::Duration
;
11 /// Checks implementation of the `OR_FUN_CALL` lint.
29 let with_enum
= Some(Enum
::A(1));
30 with_enum
.unwrap_or(Enum
::A(5));
32 let with_const_fn
= Some(Duration
::from_secs(1));
33 with_const_fn
.unwrap_or(Duration
::from_secs(5));
35 let with_constructor
= Some(vec
![1]);
36 with_constructor
.unwrap_or(make());
38 let with_new
= Some(vec
![1]);
39 with_new
.unwrap_or(Vec
::new());
41 let with_const_args
= Some(vec
![1]);
42 with_const_args
.unwrap_or(Vec
::with_capacity(12));
44 let with_err
: Result
<_
, ()> = Ok(vec
![1]);
45 with_err
.unwrap_or(make());
47 let with_err_args
: Result
<_
, ()> = Ok(vec
![1]);
48 with_err_args
.unwrap_or(Vec
::with_capacity(12));
50 let with_default_trait
= Some(1);
51 with_default_trait
.unwrap_or(Default
::default());
53 let with_default_type
= Some(1);
54 with_default_type
.unwrap_or(u64::default());
56 let with_vec
= Some(vec
![1]);
57 with_vec
.unwrap_or(vec
![]);
59 let without_default
= Some(Foo
);
60 without_default
.unwrap_or(Foo
::new());
62 let mut map
= HashMap
::<u64, String
>::new();
63 map
.entry(42).or_insert(String
::new());
65 let mut map_vec
= HashMap
::<u64, Vec
<i32>>::new();
66 map_vec
.entry(42).or_insert(vec
![]);
68 let mut btree
= BTreeMap
::<u64, String
>::new();
69 btree
.entry(42).or_insert(String
::new());
71 let mut btree_vec
= BTreeMap
::<u64, Vec
<i32>>::new();
72 btree_vec
.entry(42).or_insert(vec
![]);
74 let stringy
= Some(String
::from(""));
75 let _
= stringy
.unwrap_or("".to_owned());
79 let _
= opt
.ok_or(format
!("{} world.", hello
));
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
88 let _
= Some(1).unwrap_or(vec
[1]);
92 struct Bar(String
, Duration
);
94 fn test_or_with_ctors() {
96 let opt_opt
= Some(Some(1));
97 // we also test for const promotion, this makes sure we don't hit that
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
));
109 let _
= Some("a".to_string()).or(Some("b".to_string()));
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))));
115 let vec
= vec
!["foo"];
116 let _
= opt
.ok_or(vec
.len());
119 let _
= opt
.ok_or(array
.len());
121 let slice
= &["foo"][..];
122 let _
= opt
.ok_or(slice
.len());
125 let _
= opt
.ok_or(string
.len());
128 // Issue 4514 - early return
129 fn f() -> Option
<()> {
133 let _
= a
.unwrap_or(b
.checked_mul(3)?
.min(240));
140 let mut s
= "test".to_owned();
141 None
.unwrap_or(s
.as_mut_vec());
145 let mut s
= "test".to_owned();
146 None
.unwrap_or(unsafe { s.as_mut_vec() }
);
148 None
.unwrap_or( unsafe { s.as_mut_vec() }
);