rust/tests/ui/iter_out_of_bounds.rs

71 lines
2.3 KiB
Rust
Raw Normal View History

2023-08-24 18:13:35 -05:00
//@no-rustfix
2023-08-24 13:12:03 -05:00
#![deny(clippy::iter_out_of_bounds)]
2023-08-24 18:13:35 -05:00
#![allow(clippy::useless_vec)]
2023-08-24 13:12:03 -05:00
fn opaque_empty_iter() -> impl Iterator<Item = ()> {
std::iter::empty()
}
fn main() {
for _ in [1, 2, 3].iter().skip(4) {
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
unreachable!();
}
for (i, _) in [1, 2, 3].iter().take(4).enumerate() {
//~^ ERROR: this `.take()` call takes more items than the iterator will produce
assert!(i <= 2);
}
#[allow(clippy::needless_borrow)]
for _ in (&&&&&&[1, 2, 3]).iter().take(4) {}
//~^ ERROR: this `.take()` call takes more items than the iterator will produce
for _ in [1, 2, 3].iter().skip(4) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
2023-08-24 18:13:35 -05:00
for _ in [1; 3].iter().skip(4) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
2023-08-24 13:12:03 -05:00
// Should not lint
for _ in opaque_empty_iter().skip(1) {}
2023-08-24 18:13:35 -05:00
for _ in vec![1, 2, 3].iter().skip(4) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
for _ in vec![1; 3].iter().skip(4) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
let x = [1, 2, 3];
for _ in x.iter().skip(4) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
let n = 4;
for _ in x.iter().skip(n) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
2023-08-24 13:12:03 -05:00
let empty = std::iter::empty::<i8>;
for _ in empty().skip(1) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
for _ in empty().take(1) {}
//~^ ERROR: this `.take()` call takes more items than the iterator will produce
for _ in std::iter::once(1).skip(2) {}
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
for _ in std::iter::once(1).take(2) {}
//~^ ERROR: this `.take()` call takes more items than the iterator will produce
2023-08-27 16:38:43 -05:00
for x in [].iter().take(1) {
//~^ ERROR: this `.take()` call takes more items than the iterator will produce
let _: &i32 = x;
}
// ok, not out of bounds
for _ in [1].iter().take(1) {}
for _ in [1, 2, 3].iter().take(2) {}
for _ in [1, 2, 3].iter().skip(2) {}
2023-08-24 13:12:03 -05:00
}