1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
14 #![warn(clippy::indexing_slicing)]
15 #![warn(clippy::out_of_bounds_indexing)]
16 #![allow(clippy::no_effect, clippy::unnecessary_operation)]
21 let index_from: usize = 2;
22 let index_to: usize = 3;
26 &x[index_from..index_to];
27 &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
28 x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
29 x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
32 &x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
35 &x[5..].iter().map(|x| 2 * x).collect::<Vec<i32>>();
40 &x[4..]; // Ok, should not produce stderr.
41 &x[..4]; // Ok, should not produce stderr.
42 &x[..]; // Ok, should not produce stderr.
43 &x[1..]; // Ok, should not produce stderr.
44 &x[2..].iter().map(|x| 2 * x).collect::<Vec<i32>>(); // Ok, should not produce stderr.
45 &x[0..].get(..3); // Ok, should not produce stderr.
46 x[0]; // Ok, should not produce stderr.
47 x[3]; // Ok, should not produce stderr.
48 &x[0..3]; // Ok, should not produce stderr.
56 &y[..]; // Ok, should not produce stderr.
58 let empty: [i8; 0] = [];
59 empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
68 &empty[0..]; // Ok, should not produce stderr.
69 &empty[0..0]; // Ok, should not produce stderr.
70 &empty[..0]; // Ok, should not produce stderr.
71 &empty[..]; // Ok, should not produce stderr.
78 &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
82 &v[..]; // Ok, should not produce stderr.
85 // Continue tests at end function to minimize the changes to this file's corresponding stderr.
88 const N: usize = 15; // Out of bounds
89 const M: usize = 3; // In bounds
90 x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
91 x[M]; // Ok, should not produce stderr.
97 &x[num..10]; // should trigger out of bounds error
98 &x[10..num]; // should trigger out of bounds error