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