]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/map_unwrap_or_fixable.rs
Rollup merge of #80981 - bjorn3:bjorn3-patch-1, r=jonas-schievink
[rust.git] / src / tools / clippy / tests / ui / map_unwrap_or_fixable.rs
1 // run-rustfix
2 // aux-build:option_helpers.rs
3
4 #![warn(clippy::map_unwrap_or)]
5
6 #[macro_use]
7 extern crate option_helpers;
8
9 use std::collections::HashMap;
10
11 #[rustfmt::skip]
12 fn option_methods() {
13     let opt = Some(1);
14
15     // Check for `option.map(_).unwrap_or_else(_)` use.
16     // single line case
17     let _ = opt.map(|x| x + 1)
18         // Should lint even though this call is on a separate line.
19         .unwrap_or_else(|| 0);
20
21     // Macro case.
22     // Should not lint.
23     let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
24
25     // Issue #4144
26     {
27         let mut frequencies = HashMap::new();
28         let word = "foo";
29
30         frequencies
31             .get_mut(word)
32             .map(|count| {
33                 *count += 1;
34             })
35             .unwrap_or_else(|| {
36                 frequencies.insert(word.to_owned(), 1);
37             });
38     }
39 }
40
41 #[rustfmt::skip]
42 fn result_methods() {
43     let res: Result<i32, ()> = Ok(1);
44
45     // Check for `result.map(_).unwrap_or_else(_)` use.
46     // single line case
47     let _ = res.map(|x| x + 1)
48         // should lint even though this call is on a separate line
49         .unwrap_or_else(|_e| 0);
50
51     // macro case
52     let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
53 }
54
55 fn main() {
56     option_methods();
57     result_methods();
58 }