|
| 1 | +//@run-rustfix |
| 2 | +#![deny(clippy::explicit_iter_loop)] |
| 3 | +#![allow( |
| 4 | + clippy::linkedlist, |
| 5 | + clippy::similar_names, |
| 6 | + clippy::needless_borrow, |
| 7 | + clippy::deref_addrof, |
| 8 | + dead_code |
| 9 | +)] |
| 10 | + |
| 11 | +use core::slice; |
| 12 | +use std::collections::*; |
| 13 | + |
| 14 | +fn main() { |
| 15 | + let mut vec = vec![1, 2, 3, 4]; |
| 16 | + |
| 17 | + for _ in &vec {} |
| 18 | + for _ in &mut vec {} |
| 19 | + |
| 20 | + for _ in &vec {} // these are fine |
| 21 | + for _ in &mut vec {} // these are fine |
| 22 | + |
| 23 | + for _ in &[1, 2, 3] {} |
| 24 | + |
| 25 | + for _ in &*(&mut [1, 2, 3]) {} |
| 26 | + |
| 27 | + for _ in &[0; 32] {} |
| 28 | + for _ in &[0; 33] {} |
| 29 | + |
| 30 | + let ll: LinkedList<()> = LinkedList::new(); |
| 31 | + for _ in &ll {} |
| 32 | + |
| 33 | + let vd: VecDeque<()> = VecDeque::new(); |
| 34 | + for _ in &vd {} |
| 35 | + |
| 36 | + let bh: BinaryHeap<()> = BinaryHeap::new(); |
| 37 | + for _ in &bh {} |
| 38 | + |
| 39 | + let hm: HashMap<(), ()> = HashMap::new(); |
| 40 | + for _ in &hm {} |
| 41 | + |
| 42 | + let bt: BTreeMap<(), ()> = BTreeMap::new(); |
| 43 | + for _ in &bt {} |
| 44 | + |
| 45 | + let hs: HashSet<()> = HashSet::new(); |
| 46 | + for _ in &hs {} |
| 47 | + |
| 48 | + let bs: BTreeSet<()> = BTreeSet::new(); |
| 49 | + for _ in &bs {} |
| 50 | + |
| 51 | + struct NoIntoIter(); |
| 52 | + impl NoIntoIter { |
| 53 | + fn iter(&self) -> slice::Iter<u8> { |
| 54 | + unimplemented!() |
| 55 | + } |
| 56 | + |
| 57 | + fn iter_mut(&mut self) -> slice::IterMut<u8> { |
| 58 | + unimplemented!() |
| 59 | + } |
| 60 | + } |
| 61 | + let mut x = NoIntoIter(); |
| 62 | + for _ in x.iter() {} // no error |
| 63 | + for _ in x.iter_mut() {} // no error |
| 64 | + |
| 65 | + struct IntoIterDiffTy; |
| 66 | + impl IntoIterator for &'_ IntoIterDiffTy { |
| 67 | + type Item = &'static (); |
| 68 | + type IntoIter = core::slice::Iter<'static, ()>; |
| 69 | + fn into_iter(self) -> Self::IntoIter { |
| 70 | + unimplemented!() |
| 71 | + } |
| 72 | + } |
| 73 | + impl IntoIterDiffTy { |
| 74 | + fn iter(&self) -> core::slice::Iter<'static, i32> { |
| 75 | + unimplemented!() |
| 76 | + } |
| 77 | + } |
| 78 | + let x = IntoIterDiffTy; |
| 79 | + for _ in x.iter() {} |
| 80 | + |
| 81 | + struct IntoIterDiffSig; |
| 82 | + impl IntoIterator for &'_ IntoIterDiffSig { |
| 83 | + type Item = &'static (); |
| 84 | + type IntoIter = core::slice::Iter<'static, ()>; |
| 85 | + fn into_iter(self) -> Self::IntoIter { |
| 86 | + unimplemented!() |
| 87 | + } |
| 88 | + } |
| 89 | + impl IntoIterDiffSig { |
| 90 | + fn iter(&self, _: u32) -> core::slice::Iter<'static, ()> { |
| 91 | + unimplemented!() |
| 92 | + } |
| 93 | + } |
| 94 | + let x = IntoIterDiffSig; |
| 95 | + for _ in x.iter(0) {} |
| 96 | + |
| 97 | + struct IntoIterDiffLt<'a>(&'a ()); |
| 98 | + impl<'a> IntoIterator for &'a IntoIterDiffLt<'_> { |
| 99 | + type Item = &'a (); |
| 100 | + type IntoIter = core::slice::Iter<'a, ()>; |
| 101 | + fn into_iter(self) -> Self::IntoIter { |
| 102 | + unimplemented!() |
| 103 | + } |
| 104 | + } |
| 105 | + impl<'a> IntoIterDiffLt<'a> { |
| 106 | + fn iter(&self) -> core::slice::Iter<'a, ()> { |
| 107 | + unimplemented!() |
| 108 | + } |
| 109 | + } |
| 110 | + let x = IntoIterDiffLt(&()); |
| 111 | + for _ in x.iter() {} |
| 112 | + |
| 113 | + struct CustomType; |
| 114 | + impl<'a> IntoIterator for &'a CustomType { |
| 115 | + type Item = &'a u32; |
| 116 | + type IntoIter = core::slice::Iter<'a, u32>; |
| 117 | + fn into_iter(self) -> Self::IntoIter { |
| 118 | + unimplemented!() |
| 119 | + } |
| 120 | + } |
| 121 | + impl<'a> IntoIterator for &'a mut CustomType { |
| 122 | + type Item = &'a mut u32; |
| 123 | + type IntoIter = core::slice::IterMut<'a, u32>; |
| 124 | + fn into_iter(self) -> Self::IntoIter { |
| 125 | + unimplemented!() |
| 126 | + } |
| 127 | + } |
| 128 | + impl CustomType { |
| 129 | + fn iter(&self) -> <&'_ Self as IntoIterator>::IntoIter { |
| 130 | + panic!() |
| 131 | + } |
| 132 | + |
| 133 | + fn iter_mut(&mut self) -> core::slice::IterMut<'_, u32> { |
| 134 | + panic!() |
| 135 | + } |
| 136 | + } |
| 137 | + let mut x = CustomType; |
| 138 | + for _ in &x {} |
| 139 | + for _ in &mut x {} |
| 140 | +} |
0 commit comments