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