1 // aux-build:option_helpers.rs
3 #![warn(clippy::map_unwrap_or)]
6 extern crate option_helpers;
8 use std::collections::HashMap;
14 // Check for `option.map(_).unwrap_or(_)` use.
16 let _ = opt.map(|x| x + 1)
17 // Should lint even though this call is on a separate line.
24 let _ = opt.map(|x| x + 1)
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.
39 let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
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);
48 // Check for `option.map(_).unwrap_or_else(_)` use.
53 ).unwrap_or_else(|| 0);
54 let _ = opt.map(|x| x + 1)
62 let res: Result<i32, ()> = Ok(1);
64 // Check for `result.map(_).unwrap_or_else(_)` use.
69 ).unwrap_or_else(|_e| 0);
70 let _ = res.map(|x| x + 1)
71 .unwrap_or_else(|_e| {
75 let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint