]> git.lizzy.rs Git - rust.git/blob - tests/ui/result_map_unit_fn.rs
rustfmt tests
[rust.git] / tests / ui / result_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 #![feature(never_type)]
11 #![warn(clippy::result_map_unit_fn)]
12 #![allow(unused)]
13
14 fn do_nothing<T>(_: T) {}
15
16 fn diverge<T>(_: T) -> ! {
17     panic!()
18 }
19
20 fn plus_one(value: usize) -> usize {
21     value + 1
22 }
23
24 struct HasResult {
25     field: Result<usize, usize>,
26 }
27
28 impl HasResult {
29     fn do_result_nothing(self: &Self, value: usize) {}
30
31     fn do_result_plus_one(self: &Self, value: usize) -> usize {
32         value + 1
33     }
34 }
35
36 fn result_map_unit_fn() {
37     let x = HasResult { field: Ok(10) };
38
39     x.field.map(plus_one);
40     let _: Result<(), usize> = x.field.map(do_nothing);
41
42     x.field.map(do_nothing);
43
44     x.field.map(do_nothing);
45
46     x.field.map(diverge);
47
48     let captured = 10;
49     if let Ok(value) = x.field {
50         do_nothing(value + captured)
51     };
52     let _: Result<(), usize> = x.field.map(|value| do_nothing(value + captured));
53
54     x.field.map(|value| x.do_result_nothing(value + captured));
55
56     x.field.map(|value| {
57         x.do_result_plus_one(value + captured);
58     });
59
60     x.field.map(|value| do_nothing(value + captured));
61
62     x.field.map(|value| do_nothing(value + captured));
63
64     x.field.map(|value| {
65         do_nothing(value + captured);
66     });
67
68     x.field.map(|value| {
69         do_nothing(value + captured);
70     });
71
72     x.field.map(|value| diverge(value + captured));
73
74     x.field.map(|value| diverge(value + captured));
75
76     x.field.map(|value| {
77         diverge(value + captured);
78     });
79
80     x.field.map(|value| {
81         diverge(value + captured);
82     });
83
84     x.field.map(|value| plus_one(value + captured));
85     x.field.map(|value| plus_one(value + captured));
86     x.field.map(|value| {
87         let y = plus_one(value + captured);
88     });
89
90     x.field.map(|value| {
91         plus_one(value + captured);
92     });
93
94     x.field.map(|value| {
95         plus_one(value + captured);
96     });
97
98     x.field.map(|ref value| do_nothing(value + captured));
99
100     x.field.map(|value| {
101         do_nothing(value);
102         do_nothing(value)
103     });
104
105     x.field.map(|value| {
106         if value > 0 {
107             do_nothing(value);
108             do_nothing(value)
109         }
110     });
111
112     // Suggestion for the let block should be `{ ... }` as it's too difficult to build a
113     // proper suggestion for these cases
114     x.field.map(|value| {
115         do_nothing(value);
116         do_nothing(value)
117     });
118     x.field.map(|value| {
119         do_nothing(value);
120         do_nothing(value);
121     });
122
123     // The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let
124     // variable name for them
125     let res: Result<!, usize> = Ok(42).map(diverge);
126     "12".parse::<i32>().map(diverge);
127
128     let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
129
130     // Should suggest `if let Ok(_y) ...` to not override the existing foo variable
131     let y: Result<usize, usize> = Ok(42);
132     y.map(do_nothing);
133 }
134
135 fn main() {}