]> git.lizzy.rs Git - rust.git/blob - tests/ui/map_unwrap_or.rs
Move fixable `map_unwrap_or` cases to rustfixed test
[rust.git] / tests / ui / map_unwrap_or.rs
1 // aux-build:option_helpers.rs
2
3 #![warn(clippy::map_unwrap_or)]
4
5 #[macro_use]
6 extern crate option_helpers;
7
8 use std::collections::HashMap;
9
10 #[rustfmt::skip]
11 fn option_methods() {
12     let opt = Some(1);
13
14     // Check for `option.map(_).unwrap_or(_)` use.
15     // Multi-line cases.
16     let _ = opt.map(|x| {
17         x + 1
18     }
19     ).unwrap_or(0);
20     let _ = opt.map(|x| x + 1)
21         .unwrap_or({
22             0
23         });
24     // Single line `map(f).unwrap_or(None)` case.
25     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
26     // Multi-line `map(f).unwrap_or(None)` cases.
27     let _ = opt.map(|x| {
28         Some(x + 1)
29     }
30     ).unwrap_or(None);
31     let _ = opt
32         .map(|x| Some(x + 1))
33         .unwrap_or(None);
34     // macro case
35     let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
36
37     // Should not lint if not copyable
38     let id: String = "identifier".to_string();
39     let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
40     // ...but DO lint if the `unwrap_or` argument is not used in the `map`
41     let id: String = "identifier".to_string();
42     let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
43
44     // Check for `option.map(_).unwrap_or_else(_)` use.
45     // Multi-line cases.
46     let _ = opt.map(|x| {
47         x + 1
48     }
49     ).unwrap_or_else(|| 0);
50     let _ = opt.map(|x| x + 1)
51         .unwrap_or_else(||
52             0
53         );
54 }
55
56 fn main() {
57     option_methods();
58 }