]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/map_flatten_fixable.rs
Merge commit 'fdb84cbfd25908df5683f8f62388f663d9260e39' into clippyup
[rust.git] / src / tools / clippy / tests / ui / map_flatten_fixable.rs
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().map(option_id).flatten().collect();
19     let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_ref).flatten().collect();
20     let _: Vec<_> = vec![5_i8; 6].into_iter().map(option_id_closure).flatten().collect();
21     let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| x.checked_add(1)).flatten().collect();
22
23     // mapping to Iterator on Iterator
24     let _: Vec<_> = vec![5_i8; 6].into_iter().map(|x| 0..x).flatten().collect();
25
26     // mapping to Option on Option
27     let _: Option<_> = (Some(Some(1))).map(|x| x).flatten();
28
29     // mapping to Result on Result
30     let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten();
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         .map(|_| {
60 // we need some newlines
61 // so that the span is big enough
62 // for a split output of the diagnostic
63             Some("")
64  // whitespace beforehand is important as well
65         })
66         .flatten();
67 }