]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/question_mark.fixed
Rollup merge of #78502 - matthewjasper:chalkup, r=nikomatsakis
[rust.git] / src / tools / clippy / 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         match *self {
27             SeemsOption::None => true,
28             SeemsOption::Some(_) => false,
29         }
30     }
31 }
32
33 fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
34     if a.is_none() {
35         return SeemsOption::None;
36     }
37
38     a
39 }
40
41 pub struct CopyStruct {
42     pub opt: Option<u32>,
43 }
44
45 impl CopyStruct {
46     #[rustfmt::skip]
47     pub fn func(&self) -> Option<u32> {
48         (self.opt)?;
49
50         self.opt?;
51
52         let _ = Some(self.opt?);
53
54         let _ = self.opt?;
55
56         self.opt
57     }
58 }
59
60 #[derive(Clone)]
61 pub struct MoveStruct {
62     pub opt: Option<Vec<u32>>,
63 }
64
65 impl MoveStruct {
66     pub fn ref_func(&self) -> Option<Vec<u32>> {
67         self.opt.as_ref()?;
68
69         self.opt.clone()
70     }
71
72     pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
73         self.opt.as_ref()?;
74
75         self.opt
76     }
77
78     pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
79         self.opt.as_ref()?;
80         Some(Vec::new())
81     }
82
83     pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
84         let v: &Vec<_> = self.opt.as_ref()?;
85
86         Some(v.clone())
87     }
88
89     pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
90         let v = self.opt?;
91
92         Some(v)
93     }
94 }
95
96 fn func() -> Option<i32> {
97     fn f() -> Option<String> {
98         Some(String::new())
99     }
100
101     f()?;
102
103     Some(0)
104 }
105
106 fn main() {
107     some_func(Some(42));
108     some_func(None);
109     some_other_func(Some(42));
110
111     let copy_struct = CopyStruct { opt: Some(54) };
112     copy_struct.func();
113
114     let move_struct = MoveStruct {
115         opt: Some(vec![42, 1337]),
116     };
117     move_struct.ref_func();
118     move_struct.clone().mov_func_reuse();
119     move_struct.mov_func_no_use();
120
121     let so = SeemsOption::Some(45);
122     returns_something_similar_to_option(so);
123
124     func();
125 }