]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/map_unwrap_or_fixable.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / src / tools / clippy / tests / ui / map_unwrap_or_fixable.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2// aux-build:option_helpers.rs
3
4#![warn(clippy::map_unwrap_or)]
5
6#[macro_use]
7extern crate option_helpers;
8
9use std::collections::HashMap;
10
11#[rustfmt::skip]
12fn 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]
42fn 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
55fn main() {
56 option_methods();
57 result_methods();
58}