1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
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.
10 #![warn(clippy::option_map_unit_fn)]
13 fn do_nothing<T>(_: T) {}
15 fn diverge<T>(_: T) -> ! {
19 fn plus_one(value: usize) -> usize {
28 fn do_option_nothing(self: &Self, value: usize) {}
30 fn do_option_plus_one(self: &Self, value: usize) -> usize {
35 fn option_map_unit_fn() {
36 let x = HasOption { field: Some(10) };
38 x.field.map(plus_one);
39 let _ : Option<()> = x.field.map(do_nothing);
41 x.field.map(do_nothing);
43 x.field.map(do_nothing);
48 if let Some(value) = x.field { do_nothing(value + captured) };
49 let _ : Option<()> = x.field.map(|value| do_nothing(value + captured));
51 x.field.map(|value| x.do_option_nothing(value + captured));
53 x.field.map(|value| { x.do_option_plus_one(value + captured); });
56 x.field.map(|value| do_nothing(value + captured));
58 x.field.map(|value| { do_nothing(value + captured) });
60 x.field.map(|value| { do_nothing(value + captured); });
62 x.field.map(|value| { { do_nothing(value + captured); } });
65 x.field.map(|value| diverge(value + captured));
67 x.field.map(|value| { diverge(value + captured) });
69 x.field.map(|value| { diverge(value + captured); });
71 x.field.map(|value| { { diverge(value + captured); } });
74 x.field.map(|value| plus_one(value + captured));
75 x.field.map(|value| { plus_one(value + captured) });
76 x.field.map(|value| { let y = plus_one(value + captured); });
78 x.field.map(|value| { plus_one(value + captured); });
80 x.field.map(|value| { { plus_one(value + captured); } });
83 x.field.map(|ref value| { do_nothing(value + captured) });
86 x.field.map(|value| { do_nothing(value); do_nothing(value) });
88 x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
90 // Suggestion for the let block should be `{ ... }` as it's too difficult to build a
91 // proper suggestion for these cases
96 x.field.map(|value| { do_nothing(value); do_nothing(value); });
98 // The following should suggest `if let Some(_X) ...` as it's difficult to generate a proper let variable name for them
99 Some(42).map(diverge);
100 "12".parse::<i32>().ok().map(diverge);
101 Some(plus_one(1)).map(do_nothing);
103 // Should suggest `if let Some(_y) ...` to not override the existing foo variable