]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.rs
new lint: match_like_matches_macro
[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         matches!(*self, SeemsOption::None)
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         let _ = if let Some(x) = self.opt {
62             x
63         } else {
64             return None;
65         };
66
67         self.opt
68     }
69 }
70
71 #[derive(Clone)]
72 pub struct MoveStruct {
73     pub opt: Option<Vec<u32>>,
74 }
75
76 impl MoveStruct {
77     pub fn ref_func(&self) -> Option<Vec<u32>> {
78         if self.opt.is_none() {
79             return None;
80         }
81
82         self.opt.clone()
83     }
84
85     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
86         if self.opt.is_none() {
87             return None;
88         }
89
90         self.opt
91     }
92
93     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
94         if self.opt.is_none() {
95             return None;
96         }
97         Some(Vec::new())
98     }
99
100     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
101         let v: &Vec<_> = if let Some(ref v) = self.opt {
102             v
103         } else {
104             return None;
105         };
106
107         Some(v.clone())
108     }
109
110     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
111         let v = if let Some(v) = self.opt {
112             v
113         } else {
114             return None;
115         };
116
117         Some(v)
118     }
119 }
120
121 fn func() -> Option<i32> {
122     fn f() -> Option<String> {
123         Some(String::new())
124     }
125
126     if f().is_none() {
127         return None;
128     }
129
130     Some(0)
131 }
132
133 fn main() {
134     some_func(Some(42));
135     some_func(None);
136     some_other_func(Some(42));
137
138     let copy_struct = CopyStruct { opt: Some(54) };
139     copy_struct.func();
140
141     let move_struct = MoveStruct {
142         opt: Some(vec![42, 1337]),
143     };
144     move_struct.ref_func();
145     move_struct.clone().mov_func_reuse();
146     move_struct.mov_func_no_use();
147
148     let so = SeemsOption::Some(45);
149     returns_something_similar_to_option(so);
150
151     func();
152 }