rust/tests/ui/range.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2018-10-06 11:18:06 -05:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
struct NotARange;
impl NotARange {
fn step_by(&self, _: u32) {}
}
2018-07-28 10:34:52 -05:00
#[warn(clippy::iterator_step_by_zero, clippy::range_zip_with_len)]
fn main() {
2017-07-05 01:51:33 -05:00
let _ = (0..1).step_by(0);
// No warning for non-zero step
let _ = (0..1).step_by(1);
2017-07-05 01:51:33 -05:00
let _ = (1..).step_by(0);
2017-09-28 12:40:19 -05:00
let _ = (1..=2).step_by(0);
2017-07-05 01:51:33 -05:00
let x = 0..1;
let _ = x.step_by(0);
// No error, not a range.
let y = NotARange;
y.step_by(0);
2018-12-09 16:26:16 -06:00
let v1 = vec![1, 2, 3];
let v2 = vec![4, 5];
2017-02-08 07:58:07 -06:00
let _x = v1.iter().zip(0..v1.len());
let _y = v1.iter().zip(0..v2.len()); // No error
2017-07-05 01:51:33 -05:00
// check const eval
2018-12-09 16:26:16 -06:00
let _ = v1.iter().step_by(2 / 3);
}
2017-10-12 09:24:21 -05:00
#[allow(unused)]
fn no_panic_with_fake_range_types() {
struct Range {
foo: i32,
}
let _ = Range { foo: 0 };
}