]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_map_option.rs
Fix `manual_map`: don't lint when partially moved values are used.
[rust.git] / tests / ui / manual_map_option.rs
1 // run-rustfix
2
3 #![warn(clippy::manual_map)]
4 #![allow(
5     clippy::no_effect,
6     clippy::map_identity,
7     clippy::unit_arg,
8     clippy::match_ref_pats,
9     dead_code
10 )]
11
12 fn main() {
13     match Some(0) {
14         Some(_) => Some(2),
15         None::<u32> => None,
16     };
17
18     match Some(0) {
19         Some(x) => Some(x + 1),
20         _ => None,
21     };
22
23     match Some("") {
24         Some(x) => Some(x.is_empty()),
25         None => None,
26     };
27
28     if let Some(x) = Some(0) {
29         Some(!x)
30     } else {
31         None
32     };
33
34     #[rustfmt::skip]
35     match Some(0) {
36         Some(x) => { Some(std::convert::identity(x)) }
37         None => { None }
38     };
39
40     match Some(&String::new()) {
41         Some(x) => Some(str::len(x)),
42         None => None,
43     };
44
45     match Some(0) {
46         Some(x) if false => Some(x + 1),
47         _ => None,
48     };
49
50     match &Some([0, 1]) {
51         Some(x) => Some(x[0]),
52         &None => None,
53     };
54
55     match &Some(0) {
56         &Some(x) => Some(x * 2),
57         None => None,
58     };
59
60     match Some(String::new()) {
61         Some(ref x) => Some(x.is_empty()),
62         _ => None,
63     };
64
65     match &&Some(String::new()) {
66         Some(x) => Some(x.len()),
67         _ => None,
68     };
69
70     match &&Some(0) {
71         &&Some(x) => Some(x + x),
72         &&_ => None,
73     };
74
75     #[warn(clippy::option_map_unit_fn)]
76     match &mut Some(String::new()) {
77         Some(x) => Some(x.push_str("")),
78         None => None,
79     };
80
81     #[allow(clippy::option_map_unit_fn)]
82     {
83         match &mut Some(String::new()) {
84             Some(x) => Some(x.push_str("")),
85             None => None,
86         };
87     }
88
89     match &mut Some(String::new()) {
90         Some(ref x) => Some(x.len()),
91         None => None,
92     };
93
94     match &mut &Some(String::new()) {
95         Some(x) => Some(x.is_empty()),
96         &mut _ => None,
97     };
98
99     match Some((0, 1, 2)) {
100         Some((x, y, z)) => Some(x + y + z),
101         None => None,
102     };
103
104     match Some([1, 2, 3]) {
105         Some([first, ..]) => Some(first),
106         None => None,
107     };
108
109     match &Some((String::new(), "test")) {
110         Some((x, y)) => Some((y, x)),
111         None => None,
112     };
113
114     match Some((String::new(), 0)) {
115         Some((ref x, y)) => Some((y, x)),
116         None => None,
117     };
118
119     match Some(Some(0)) {
120         Some(Some(_)) | Some(None) => Some(0),
121         None => None,
122     };
123
124     match Some(Some((0, 1))) {
125         Some(Some((x, 1))) => Some(x),
126         _ => None,
127     };
128
129     // #6795
130     fn f1() -> Result<(), ()> {
131         let _ = match Some(Ok(())) {
132             Some(x) => Some(x?),
133             None => None,
134         };
135         Ok(())
136     }
137
138     for &x in Some(Some(true)).iter() {
139         let _ = match x {
140             Some(x) => Some(if x { continue } else { x }),
141             None => None,
142         };
143     }
144
145     // #6797
146     let x1 = (Some(String::new()), 0);
147     let x2 = x1.0;
148     match x2 {
149         Some(x) => Some((x, x1.1)),
150         None => None,
151     };
152
153     struct S1 {
154         x: Option<String>,
155         y: u32,
156     }
157     impl S1 {
158         fn f(self) -> Option<(String, u32)> {
159             match self.x {
160                 Some(x) => Some((x, self.y)),
161                 None => None,
162             }
163         }
164     }
165 }