about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/explicit_iter_loop.fixed
blob: bffa1c4cf4085b96f188098a15ad20a5915b2ef1 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![deny(clippy::explicit_iter_loop)]
#![allow(
    clippy::linkedlist,
    clippy::similar_names,
    clippy::needless_borrow,
    clippy::deref_addrof,
    clippy::unnecessary_mut_passed,
    dead_code,
    non_local_definitions
)]

use core::slice;
use std::collections::*;

fn main() {
    let mut vec = vec![1, 2, 3, 4];

    for _ in &vec {}
    //~^ explicit_iter_loop
    for _ in &mut vec {}
    //~^ explicit_iter_loop

    let rvec = &vec;
    for _ in rvec {}
    //~^ explicit_iter_loop

    let rmvec = &mut vec;
    for _ in rmvec.iter() {}
    for _ in rmvec.iter_mut() {}

    for _ in &vec {} // these are fine
    for _ in &mut vec {} // these are fine

    for _ in &[1, 2, 3] {}
    //~^ explicit_iter_loop

    for _ in (&mut [1, 2, 3]).iter() {}

    for _ in &[0; 32] {}
    //~^ explicit_iter_loop
    for _ in &[0; 33] {}
    //~^ explicit_iter_loop

    let ll: LinkedList<()> = LinkedList::new();
    for _ in &ll {}
    //~^ explicit_iter_loop
    let rll = &ll;
    for _ in rll {}
    //~^ explicit_iter_loop

    let vd: VecDeque<()> = VecDeque::new();
    for _ in &vd {}
    //~^ explicit_iter_loop
    let rvd = &vd;
    for _ in rvd {}
    //~^ explicit_iter_loop

    let bh: BinaryHeap<()> = BinaryHeap::new();
    for _ in &bh {}
    //~^ explicit_iter_loop

    let hm: HashMap<(), ()> = HashMap::new();
    for _ in &hm {}
    //~^ explicit_iter_loop

    let bt: BTreeMap<(), ()> = BTreeMap::new();
    for _ in &bt {}
    //~^ explicit_iter_loop

    let hs: HashSet<()> = HashSet::new();
    for _ in &hs {}
    //~^ explicit_iter_loop

    let bs: BTreeSet<()> = BTreeSet::new();
    for _ in &bs {}
    //~^ explicit_iter_loop

    struct NoIntoIter();
    impl NoIntoIter {
        fn iter(&self) -> slice::Iter<'_, u8> {
            unimplemented!()
        }

        fn iter_mut(&mut self) -> slice::IterMut<'_, u8> {
            unimplemented!()
        }
    }
    let mut x = NoIntoIter();
    for _ in x.iter() {} // no error
    for _ in x.iter_mut() {} // no error

    struct IntoIterDiffTy;
    impl IntoIterator for &'_ IntoIterDiffTy {
        type Item = &'static ();
        type IntoIter = core::slice::Iter<'static, ()>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }
    impl IntoIterDiffTy {
        fn iter(&self) -> core::slice::Iter<'static, i32> {
            unimplemented!()
        }
    }
    let x = IntoIterDiffTy;
    for _ in x.iter() {}

    struct IntoIterDiffSig;
    impl IntoIterator for &'_ IntoIterDiffSig {
        type Item = &'static ();
        type IntoIter = core::slice::Iter<'static, ()>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }
    impl IntoIterDiffSig {
        fn iter(&self, _: u32) -> core::slice::Iter<'static, ()> {
            unimplemented!()
        }
    }
    let x = IntoIterDiffSig;
    for _ in x.iter(0) {}

    struct IntoIterDiffLt<'a>(&'a ());
    impl<'a> IntoIterator for &'a IntoIterDiffLt<'_> {
        type Item = &'a ();
        type IntoIter = core::slice::Iter<'a, ()>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }
    impl<'a> IntoIterDiffLt<'a> {
        fn iter(&self) -> core::slice::Iter<'a, ()> {
            unimplemented!()
        }
    }
    let x = IntoIterDiffLt(&());
    for _ in x.iter() {}

    struct CustomType;
    impl<'a> IntoIterator for &'a CustomType {
        type Item = &'a u32;
        type IntoIter = core::slice::Iter<'a, u32>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }
    impl<'a> IntoIterator for &'a mut CustomType {
        type Item = &'a mut u32;
        type IntoIter = core::slice::IterMut<'a, u32>;
        fn into_iter(self) -> Self::IntoIter {
            unimplemented!()
        }
    }
    impl CustomType {
        fn iter(&self) -> <&'_ Self as IntoIterator>::IntoIter {
            panic!()
        }

        fn iter_mut(&mut self) -> core::slice::IterMut<'_, u32> {
            panic!()
        }
    }
    let mut x = CustomType;
    for _ in &x {}
    //~^ explicit_iter_loop
    for _ in &mut x {}
    //~^ explicit_iter_loop

    let r = &x;
    for _ in r {}
    //~^ explicit_iter_loop
}

#[clippy::msrv = "1.79"]
pub fn issue_13184() {
    // https://github.com/rust-lang/rust-clippy/issues/13184
    // No need to fix, as IntoIterator for Box is valid starting from 1.80
    let mut values: Box<[u32]> = Box::new([1, 2]);
    for _ in values.iter() {}
    for _ in values.iter_mut() {}

    let rvalues = &values;
    for _ in rvalues.iter() {}
}

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

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

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