]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/map_flatten_fixable.fixed
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / map_flatten_fixable.fixed
1 // run-rustfix
2
3 #![warn(clippy::all, clippy::pedantic)]
4 #![allow(clippy::let_underscore_drop)]
5 #![allow(clippy::missing_docs_in_private_items)]
6 #![allow(clippy::map_identity)]
7 #![allow(clippy::redundant_closure)]
8 #![allow(clippy::unnecessary_wraps)]
9 #![feature(result_flattening)]
10
11 fn main() {
12 // mapping to Option on Iterator
13 fn option_id(x: i8) -> Option<i8> {
14 Some(x)
15 }
16 let option_id_ref: fn(i8) -> Option<i8> = option_id;
17 let option_id_closure = |x| Some(x);
18 let _: Vec<_> = vec![5_i8; 6].into_iter().filter_map(option_id).collect();
19 let _: Vec<_> = vec![5_i8; 6].into_iter().filter_map(option_id_ref).collect();
20 let _: Vec<_> = vec![5_i8; 6].into_iter().filter_map(option_id_closure).collect();
21 let _: Vec<_> = vec![5_i8; 6].into_iter().filter_map(|x| x.checked_add(1)).collect();
22
23 // mapping to Iterator on Iterator
24 let _: Vec<_> = vec![5_i8; 6].into_iter().flat_map(|x| 0..x).collect();
25
26 // mapping to Option on Option
27 let _: Option<_> = (Some(Some(1))).and_then(|x| x);
28
29 // mapping to Result on Result
30 let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x);
31
32 issue8734();
33 issue8878();
34 }
35
36 fn issue8734() {
37 // let _ = [0u8, 1, 2, 3]
38 // .into_iter()
39 // .map(|n| match n {
40 // 1 => [n
41 // .saturating_add(1)
42 // .saturating_add(1)
43 // .saturating_add(1)
44 // .saturating_add(1)
45 // .saturating_add(1)
46 // .saturating_add(1)
47 // .saturating_add(1)
48 // .saturating_add(1)],
49 // n => [n],
50 // })
51 // .flatten();
52 }
53
54 #[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again
55 #[rustfmt::skip] // whitespace is important for this one
56 fn issue8878() {
57 std::collections::HashMap::<u32, u32>::new()
58 .get(&0)
59 .and_then(|_| {
60 // we need some newlines
61 // so that the span is big enough
62 // we need some newlines
63 // so that the span is big enough
64 // for a splitted output of the diagnostic
65 Some("")
66 // whitespace beforehand is important as well
67 });
68 }