about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/explicit_into_iter_loop.fixed
blob: c1b3c478eeb91713b35738b2a920a2220f21a2b5 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#![allow(non_local_definitions)]
#![warn(clippy::explicit_into_iter_loop)]

fn main() {
    // Issue #4958
    fn _takes_iterator<T>(iterator: &T)
    where
        for<'a> &'a T: IntoIterator<Item = &'a String>,
    {
        for _ in iterator {}
        //~^ explicit_into_iter_loop
    }

    struct T;
    impl IntoIterator for &T {
        type Item = ();
        type IntoIter = std::vec::IntoIter<Self::Item>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }

    let mut t = T;
    for _ in &t {}
    //~^ explicit_into_iter_loop

    let r = &t;
    for _ in r {}
    //~^ explicit_into_iter_loop

    // No suggestion for this.
    // We'd have to suggest `for _ in *rr {}` which is less clear.
    let rr = &&t;
    for _ in rr.into_iter() {}

    let mr = &mut t;
    for _ in &*mr {}
    //~^ explicit_into_iter_loop

    struct U;
    impl IntoIterator for &mut U {
        type Item = ();
        type IntoIter = std::vec::IntoIter<Self::Item>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }

    let mut u = U;
    for _ in &mut u {}
    //~^ explicit_into_iter_loop

    let mr = &mut u;
    for _ in &mut *mr {}
    //~^ explicit_into_iter_loop

    // Issue #6900
    struct S;
    impl S {
        #[allow(clippy::should_implement_trait)]
        pub fn into_iter<T>(self) -> I<T> {
            unimplemented!()
        }
    }

    struct I<T>(T);
    impl<T> Iterator for I<T> {
        type Item = T;
        fn next(&mut self) -> Option<Self::Item> {
            unimplemented!()
        }
    }

    for _ in S.into_iter::<u32>() {}
}

fn issue14630() {
    macro_rules! mac {
        (into_iter $e:expr) => {
            $e.into_iter()
        };
    }

    for _ in dbg!([1, 2]) {}
    //~^ explicit_into_iter_loop

    for _ in mac!(into_iter [1, 2]) {}
}