]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/map_unwrap_or_fixable.fixed
Rollup merge of #78728 - a1phyr:const_cell_into_inner, r=dtolnay
[rust.git] / src / tools / clippy / tests / ui / map_unwrap_or_fixable.fixed
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_or_else(|| 0, |x| x + 1);
18
19     // Macro case.
20     // Should not lint.
21     let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
22
23     // Issue #4144
24     {
25         let mut frequencies = HashMap::new();
26         let word = "foo";
27
28         frequencies
29             .get_mut(word)
30             .map(|count| {
31                 *count += 1;
32             })
33             .unwrap_or_else(|| {
34                 frequencies.insert(word.to_owned(), 1);
35             });
36     }
37 }
38
39 #[rustfmt::skip]
40 fn result_methods() {
41     let res: Result<i32, ()> = Ok(1);
42
43     // Check for `result.map(_).unwrap_or_else(_)` use.
44     // single line case
45     let _ = res.map_or_else(|_e| 0, |x| x + 1);
46
47     // macro case
48     let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
49 }
50
51 fn main() {
52     option_methods();
53     result_methods();
54 }