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