summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/iter_on_single_items.fixed
blob: 1fa4b03641bc73334438a452644037a410b6cbcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// run-rustfix
#![warn(clippy::iter_on_single_items)]
#![allow(clippy::iter_next_slice, clippy::redundant_clone)]

fn array() {
    assert_eq!(std::iter::once(123).next(), Some(123));
    assert_eq!(std::iter::once(&mut 123).next(), Some(&mut 123));
    assert_eq!(std::iter::once(&123).next(), Some(&123));
    assert_eq!(std::iter::once(123).next(), Some(123));
    assert_eq!(std::iter::once(&mut 123).next(), Some(&mut 123));
    assert_eq!(std::iter::once(&123).next(), Some(&123));

    // Don't trigger on non-iter methods
    let _: Option<String> = Some("test".to_string()).clone();
    let _: [String; 1] = ["test".to_string()].clone();

    // Don't trigger on match or if branches
    let _ = match 123 {
        123 => [].iter(),
        _ => ["test"].iter(),
    };

    let _ = if false { ["test"].iter() } else { [].iter() };
}

macro_rules! in_macros {
    () => {
        assert_eq!([123].into_iter().next(), Some(123));
        assert_eq!([123].iter_mut().next(), Some(&mut 123));
        assert_eq!([123].iter().next(), Some(&123));
        assert_eq!(Some(123).into_iter().next(), Some(123));
        assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
        assert_eq!(Some(123).iter().next(), Some(&123));
    };
}

// Don't trigger on a `Some` that isn't std's option
mod custom_option {
    #[allow(unused)]
    enum CustomOption {
        Some(i32),
        None,
    }

    impl CustomOption {
        fn iter(&self) {}
        fn iter_mut(&mut self) {}
        fn into_iter(self) {}
    }
    use CustomOption::*;

    pub fn custom_option() {
        Some(3).iter();
        Some(3).iter_mut();
        Some(3).into_iter();
    }
}

fn main() {
    array();
    custom_option::custom_option();
    in_macros!();
}