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