]> git.lizzy.rs Git - rust.git/blob - tests/ui/result_map_unit_fn.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / result_map_unit_fn.rs
1 #![feature(never_type)]
2 #![warn(clippy::result_map_unit_fn)]
3 #![allow(unused)]
4
5 fn do_nothing<T>(_: T) {}
6
7 fn diverge<T>(_: T) -> ! {
8     panic!()
9 }
10
11 fn plus_one(value: usize) -> usize {
12     value + 1
13 }
14
15 struct HasResult {
16     field: Result<usize, usize>,
17 }
18
19 impl HasResult {
20     fn do_result_nothing(self: &Self, value: usize) {}
21
22     fn do_result_plus_one(self: &Self, value: usize) -> usize {
23         value + 1
24     }
25 }
26
27 #[rustfmt::skip]
28 fn result_map_unit_fn() {
29     let x = HasResult { field: Ok(10) };
30
31     x.field.map(plus_one);
32     let _: Result<(), usize> = 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 Ok(value) = x.field { do_nothing(value + captured) };
42     let _: Result<(), usize> = x.field.map(|value| do_nothing(value + captured));
43
44     x.field.map(|value| x.do_result_nothing(value + captured));
45
46     x.field.map(|value| { x.do_result_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 Ok(_X) ...` as it's difficult to generate a proper let variable name for them
92     let res: Result<!, usize> = Ok(42).map(diverge);
93     "12".parse::<i32>().map(diverge);
94
95     let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
96
97     // Should suggest `if let Ok(_y) ...` to not override the existing foo variable
98     let y: Result<usize, usize> = Ok(42);
99     y.map(do_nothing);
100 }
101
102 fn main() {}