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