]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
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 | // Single line case. | |
16 | let _ = opt.map(|x| x + 1) | |
17 | // Should lint even though this call is on a separate line. | |
18 | .unwrap_or(0); | |
19 | // Multi-line cases. | |
20 | let _ = opt.map(|x| { | |
21 | x + 1 | |
22 | } | |
23 | ).unwrap_or(0); | |
24 | let _ = opt.map(|x| x + 1) | |
25 | .unwrap_or({ | |
26 | 0 | |
27 | }); | |
28 | // Single line `map(f).unwrap_or(None)` case. | |
29 | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None); | |
30 | // Multi-line `map(f).unwrap_or(None)` cases. | |
31 | let _ = opt.map(|x| { | |
32 | Some(x + 1) | |
33 | } | |
34 | ).unwrap_or(None); | |
35 | let _ = opt | |
36 | .map(|x| Some(x + 1)) | |
37 | .unwrap_or(None); | |
38 | // macro case | |
39 | let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint | |
40 | ||
41 | // Should not lint if not copyable | |
42 | let id: String = "identifier".to_string(); | |
43 | let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id); | |
44 | // ...but DO lint if the `unwrap_or` argument is not used in the `map` | |
45 | let id: String = "identifier".to_string(); | |
46 | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id); | |
47 | ||
48 | // Check for `option.map(_).unwrap_or_else(_)` use. | |
49 | // Multi-line cases. | |
50 | let _ = opt.map(|x| { | |
51 | x + 1 | |
52 | } | |
53 | ).unwrap_or_else(|| 0); | |
54 | let _ = opt.map(|x| x + 1) | |
55 | .unwrap_or_else(|| | |
56 | 0 | |
57 | ); | |
58 | } | |
59 | ||
60 | #[rustfmt::skip] | |
61 | fn result_methods() { | |
62 | let res: Result<i32, ()> = Ok(1); | |
63 | ||
64 | // Check for `result.map(_).unwrap_or_else(_)` use. | |
65 | // multi line cases | |
66 | let _ = res.map(|x| { | |
67 | x + 1 | |
68 | } | |
69 | ).unwrap_or_else(|_e| 0); | |
70 | let _ = res.map(|x| x + 1) | |
71 | .unwrap_or_else(|_e| { | |
72 | 0 | |
73 | }); | |
74 | // macro case | |
75 | let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint | |
76 | } | |
77 | ||
78 | fn main() { | |
79 | option_methods(); | |
80 | result_methods(); | |
81 | } |