]> git.lizzy.rs Git - rust.git/blob - tests/ui/question_mark.fixed
new lint: match_like_matches_macro
[rust.git] / tests / ui / question_mark.fixed
1 // run-rustfix
2 #![allow(unreachable_code)]
3
4 fn some_func(a: Option<u32>) -> Option<u32> {
5     a?;
6
7     a
8 }
9
10 fn some_other_func(a: Option<u32>) -> Option<u32> {
11     if a.is_none() {
12         return None;
13     } else {
14         return Some(0);
15     }
16     unreachable!()
17 }
18
19 pub enum SeemsOption<T> {
20     Some(T),
21     None,
22 }
23
24 impl<T> SeemsOption<T> {
25     pub fn is_none(&self) -> bool {
26         matches!(*self, SeemsOption::None)
27     }
28 }
29
30 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
31     if a.is_none() {
32         return SeemsOption::None;
33     }
34
35     a
36 }
37
38 pub struct CopyStruct {
39     pub opt: Option<u32>,
40 }
41
42 impl CopyStruct {
43     #[rustfmt::skip]
44     pub fn func(&self) -> Option<u32> {
45         (self.opt)?;
46
47         self.opt?;
48
49         let _ = Some(self.opt?);
50
51         let _ = self.opt?;
52
53         self.opt
54     }
55 }
56
57 #[derive(Clone)]
58 pub struct MoveStruct {
59     pub opt: Option<Vec<u32>>,
60 }
61
62 impl MoveStruct {
63     pub fn ref_func(&self) -> Option<Vec<u32>> {
64         self.opt.as_ref()?;
65
66         self.opt.clone()
67     }
68
69     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
70         self.opt.as_ref()?;
71
72         self.opt
73     }
74
75     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
76         self.opt.as_ref()?;
77         Some(Vec::new())
78     }
79
80     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
81         let v: &Vec<_> = self.opt.as_ref()?;
82
83         Some(v.clone())
84     }
85
86     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
87         let v = self.opt?;
88
89         Some(v)
90     }
91 }
92
93 fn func() -> Option<i32> {
94     fn f() -> Option<String> {
95         Some(String::new())
96     }
97
98     f()?;
99
100     Some(0)
101 }
102
103 fn main() {
104     some_func(Some(42));
105     some_func(None);
106     some_other_func(Some(42));
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.mov_func_no_use();
117
118     let so = SeemsOption::Some(45);
119     returns_something_similar_to_option(so);
120
121     func();
122 }