]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / question_mark.rs
1 fn some_func(a: Option<u32>) -> Option<u32> {
2     if a.is_none() {
3         return None;
4     }
5
6     a
7 }
8
9 fn some_other_func(a: Option<u32>) -> Option<u32> {
10     if a.is_none() {
11         return None;
12     } else {
13         return Some(0);
14     }
15     unreachable!()
16 }
17
18 pub enum SeemsOption<T> {
19     Some(T),
20     None,
21 }
22
23 impl<T> SeemsOption<T> {
24     pub fn is_none(&self) -> bool {
25         match *self {
26             SeemsOption::None => true,
27             SeemsOption::Some(_) => false,
28         }
29     }
30 }
31
32 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
33     if a.is_none() {
34         return SeemsOption::None;
35     }
36
37     a
38 }
39
40 pub struct CopyStruct {
41     pub opt: Option<u32>,
42 }
43
44 impl CopyStruct {
45     #[rustfmt::skip]
46     pub fn func(&self) -> Option<u32> {
47         if (self.opt).is_none() {
48             return None;
49         }
50
51         if self.opt.is_none() {
52             return None
53         }
54
55         let _ = if self.opt.is_none() {
56             return None;
57         } else {
58             self.opt
59         };
60
61         self.opt
62     }
63 }
64
65 #[derive(Clone)]
66 pub struct MoveStruct {
67     pub opt: Option<Vec<u32>>,
68 }
69
70 impl MoveStruct {
71     pub fn ref_func(&self) -> Option<Vec<u32>> {
72         if self.opt.is_none() {
73             return None;
74         }
75
76         self.opt.clone()
77     }
78
79     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
80         if self.opt.is_none() {
81             return None;
82         }
83
84         self.opt
85     }
86
87     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
88         if self.opt.is_none() {
89             return None;
90         }
91         Some(Vec::new())
92     }
93 }
94
95 fn main() {
96     some_func(Some(42));
97     some_func(None);
98
99     let copy_struct = CopyStruct { opt: Some(54) };
100     copy_struct.func();
101
102     let move_struct = MoveStruct {
103         opt: Some(vec![42, 1337]),
104     };
105     move_struct.ref_func();
106     move_struct.clone().mov_func_reuse();
107     move_struct.clone().mov_func_no_use();
108
109     let so = SeemsOption::Some(45);
110     returns_something_similar_to_option(so);
111 }