]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/question_mark.rs
Auto merge of #84620 - Dylan-DPC:rollup-wkv97im, r=Dylan-DPC
[rust.git] / src / tools / clippy / tests / ui / question_mark.rs
1 // run-rustfix
2 #![allow(unreachable_code)]
3 #![allow(clippy::unnecessary_wraps)]
4
5 fn some_func(a: Option<u32>) -> Option<u32> {
6     if a.is_none() {
7         return None;
8     }
9
10     a
11 }
12
13 fn some_other_func(a: Option<u32>) -> Option<u32> {
14     if a.is_none() {
15         return None;
16     } else {
17         return Some(0);
18     }
19     unreachable!()
20 }
21
22 pub enum SeemsOption<T> {
23     Some(T),
24     None,
25 }
26
27 impl<T> SeemsOption<T> {
28     pub fn is_none(&self) -> bool {
29         match *self {
30             SeemsOption::None => true,
31             SeemsOption::Some(_) => false,
32         }
33     }
34 }
35
36 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
37     if a.is_none() {
38         return SeemsOption::None;
39     }
40
41     a
42 }
43
44 pub struct CopyStruct {
45     pub opt: Option<u32>,
46 }
47
48 impl CopyStruct {
49     #[rustfmt::skip]
50     pub fn func(&self) -> Option<u32> {
51         if (self.opt).is_none() {
52             return None;
53         }
54
55         if self.opt.is_none() {
56             return None
57         }
58
59         let _ = if self.opt.is_none() {
60             return None;
61         } else {
62             self.opt
63         };
64
65         let _ = if let Some(x) = self.opt {
66             x
67         } else {
68             return None;
69         };
70
71         self.opt
72     }
73 }
74
75 #[derive(Clone)]
76 pub struct MoveStruct {
77     pub opt: Option<Vec<u32>>,
78 }
79
80 impl MoveStruct {
81     pub fn ref_func(&self) -> Option<Vec<u32>> {
82         if self.opt.is_none() {
83             return None;
84         }
85
86         self.opt.clone()
87     }
88
89     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
90         if self.opt.is_none() {
91             return None;
92         }
93
94         self.opt
95     }
96
97     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
98         if self.opt.is_none() {
99             return None;
100         }
101         Some(Vec::new())
102     }
103
104     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
105         let v: &Vec<_> = if let Some(ref v) = self.opt {
106             v
107         } else {
108             return None;
109         };
110
111         Some(v.clone())
112     }
113
114     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
115         let v = if let Some(v) = self.opt {
116             v
117         } else {
118             return None;
119         };
120
121         Some(v)
122     }
123 }
124
125 fn func() -> Option<i32> {
126     fn f() -> Option<String> {
127         Some(String::new())
128     }
129
130     if f().is_none() {
131         return None;
132     }
133
134     Some(0)
135 }
136
137 fn main() {
138     some_func(Some(42));
139     some_func(None);
140     some_other_func(Some(42));
141
142     let copy_struct = CopyStruct { opt: Some(54) };
143     copy_struct.func();
144
145     let move_struct = MoveStruct {
146         opt: Some(vec![42, 1337]),
147     };
148     move_struct.ref_func();
149     move_struct.clone().mov_func_reuse();
150     move_struct.mov_func_no_use();
151
152     let so = SeemsOption::Some(45);
153     returns_something_similar_to_option(so);
154
155     func();
156 }