]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / question_mark.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 fn some_func(a: Option<u32>) -> Option<u32> {
11     if a.is_none() {
12         return None;
13     }
14
15     a
16 }
17
18 fn some_other_func(a: Option<u32>) -> Option<u32> {
19     if a.is_none() {
20         return None;
21     } else {
22         return Some(0);
23     }
24     unreachable!()
25 }
26
27 pub enum SeemsOption<T> {
28     Some(T),
29     None,
30 }
31
32 impl<T> SeemsOption<T> {
33     pub fn is_none(&self) -> bool {
34         match *self {
35             SeemsOption::None => true,
36             SeemsOption::Some(_) => false,
37         }
38     }
39 }
40
41 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
42     if a.is_none() {
43         return SeemsOption::None;
44     }
45
46     a
47 }
48
49 pub struct CopyStruct {
50     pub opt: Option<u32>,
51 }
52
53 impl CopyStruct {
54     #[rustfmt::skip]
55     pub fn func(&self) -> Option<u32> {
56         if (self.opt).is_none() {
57             return None;
58         }
59
60         if self.opt.is_none() {
61             return None
62         }
63
64         let _ = if self.opt.is_none() {
65             return None;
66         } else {
67             self.opt
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
104 fn main() {
105     some_func(Some(42));
106     some_func(None);
107
108     let copy_struct = CopyStruct { opt: Some(54) };
109     copy_struct.func();
110
111     let move_struct = MoveStruct {
112         opt: Some(vec![42, 1337]),
113     };
114     move_struct.ref_func();
115     move_struct.clone().mov_func_reuse();
116     move_struct.clone().mov_func_no_use();
117
118     let so = SeemsOption::Some(45);
119     returns_something_similar_to_option(so);
120 }