]> git.lizzy.rs Git - rust.git/blob - tests/ui/map_unwrap_or.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / tests / ui / map_unwrap_or.rs
1 // FIXME: Add "run-rustfix" once it's supported for multipart suggestions
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(_)` 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(0);
20     // Multi-line cases.
21     let _ = opt.map(|x| {
22         x + 1
23     }
24     ).unwrap_or(0);
25     let _ = opt.map(|x| x + 1)
26         .unwrap_or({
27             0
28         });
29     // Single line `map(f).unwrap_or(None)` case.
30     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
31     // Multi-line `map(f).unwrap_or(None)` cases.
32     let _ = opt.map(|x| {
33         Some(x + 1)
34     }
35     ).unwrap_or(None);
36     let _ = opt
37         .map(|x| Some(x + 1))
38         .unwrap_or(None);
39     // macro case
40     let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
41
42     // Should not lint if not copyable
43     let id: String = "identifier".to_string();
44     let _ = Some("prefix").map(|p| format!("{}.{}", p, id)).unwrap_or(id);
45     // ...but DO lint if the `unwrap_or` argument is not used in the `map`
46     let id: String = "identifier".to_string();
47     let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
48
49     // Check for `option.map(_).unwrap_or_else(_)` use.
50     // single line case
51     let _ = opt.map(|x| x + 1)
52         // Should lint even though this call is on a separate line.
53         .unwrap_or_else(|| 0);
54     // Multi-line cases.
55     let _ = opt.map(|x| {
56         x + 1
57     }
58     ).unwrap_or_else(|| 0);
59     let _ = opt.map(|x| x + 1)
60         .unwrap_or_else(||
61             0
62         );
63     // Macro case.
64     // Should not lint.
65     let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
66
67     // Issue #4144
68     {
69         let mut frequencies = HashMap::new();
70         let word = "foo";
71
72         frequencies
73             .get_mut(word)
74             .map(|count| {
75                 *count += 1;
76             })
77             .unwrap_or_else(|| {
78                 frequencies.insert(word.to_owned(), 1);
79             });
80     }
81 }
82
83 fn result_methods() {
84     let res: Result<i32, ()> = Ok(1);
85
86     // Check for `result.map(_).unwrap_or_else(_)` use.
87     // single line case
88     let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
89                                                       // multi line cases
90     let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
91     let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
92     // macro case
93     let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
94 }
95
96 fn main() {
97     option_methods();
98     result_methods();
99 }