]> git.lizzy.rs Git - rust.git/blob - tests/ui/option_map_unit_fn.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / option_map_unit_fn.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 #![warn(clippy::option_map_unit_fn)]
11 #![allow(unused)]
12
13 fn do_nothing<T>(_: T) {}
14
15 fn diverge<T>(_: T) -> ! {
16     panic!()
17 }
18
19 fn plus_one(value: usize) -> usize {
20     value + 1
21 }
22
23 struct HasOption {
24     field: Option<usize>,
25 }
26
27 impl HasOption {
28     fn do_option_nothing(self: &Self, value: usize) {}
29
30     fn do_option_plus_one(self: &Self, value: usize) -> usize {
31         value + 1
32     }
33 }
34 #[rustfmt::skip]
35 fn option_map_unit_fn() {
36     let x = HasOption { field: Some(10) };
37
38     x.field.map(plus_one);
39     let _ : Option<()> = x.field.map(do_nothing);
40
41     x.field.map(do_nothing);
42
43     x.field.map(do_nothing);
44
45     x.field.map(diverge);
46
47     let captured = 10;
48     if let Some(value) = x.field { do_nothing(value + captured) };
49     let _ : Option<()> = x.field.map(|value| do_nothing(value + captured));
50
51     x.field.map(|value| x.do_option_nothing(value + captured));
52
53     x.field.map(|value| { x.do_option_plus_one(value + captured); });
54
55
56     x.field.map(|value| do_nothing(value + captured));
57
58     x.field.map(|value| { do_nothing(value + captured) });
59
60     x.field.map(|value| { do_nothing(value + captured); });
61
62     x.field.map(|value| { { do_nothing(value + captured); } });
63
64
65     x.field.map(|value| diverge(value + captured));
66
67     x.field.map(|value| { diverge(value + captured) });
68
69     x.field.map(|value| { diverge(value + captured); });
70
71     x.field.map(|value| { { diverge(value + captured); } });
72
73
74     x.field.map(|value| plus_one(value + captured));
75     x.field.map(|value| { plus_one(value + captured) });
76     x.field.map(|value| { let y = plus_one(value + captured); });
77
78     x.field.map(|value| { plus_one(value + captured); });
79
80     x.field.map(|value| { { plus_one(value + captured); } });
81
82
83     x.field.map(|ref value| { do_nothing(value + captured) });
84
85
86     x.field.map(|value| { do_nothing(value); do_nothing(value) });
87
88     x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
89
90     // Suggestion for the let block should be `{ ... }` as it's too difficult to build a
91     // proper suggestion for these cases
92     x.field.map(|value| {
93         do_nothing(value);
94         do_nothing(value)
95     });
96     x.field.map(|value| { do_nothing(value); do_nothing(value); });
97
98     // The following should suggest `if let Some(_X) ...` as it's difficult to generate a proper let variable name for them
99     Some(42).map(diverge);
100     "12".parse::<i32>().ok().map(diverge);
101     Some(plus_one(1)).map(do_nothing);
102
103     // Should suggest `if let Some(_y) ...` to not override the existing foo variable
104     let y = Some(42);
105     y.map(do_nothing);
106 }
107
108 fn main() {}