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.
11 #![warn(clippy::indexing_slicing)]
12 #![warn(clippy::out_of_bounds_indexing)]
13 #![allow(clippy::no_effect, clippy::unnecessary_operation)]
18 let index_from: usize = 2;
19 let index_to: usize = 3;
23 &x[index_from..index_to];
24 &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
25 x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
26 x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
29 &x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
32 &x[5..].iter().map(|x| 2 * x).collect::<Vec<i32>>();
37 &x[4..]; // Ok, should not produce stderr.
38 &x[..4]; // Ok, should not produce stderr.
39 &x[..]; // Ok, should not produce stderr.
40 &x[1..]; // Ok, should not produce stderr.
41 &x[2..].iter().map(|x| 2 * x).collect::<Vec<i32>>(); // Ok, should not produce stderr.
42 &x[0..].get(..3); // Ok, should not produce stderr.
43 x[0]; // Ok, should not produce stderr.
44 x[3]; // Ok, should not produce stderr.
45 &x[0..3]; // Ok, should not produce stderr.
53 &y[..]; // Ok, should not produce stderr.
55 let empty: [i8; 0] = [];
56 empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
65 &empty[0..]; // Ok, should not produce stderr.
66 &empty[0..0]; // Ok, should not produce stderr.
67 &empty[..0]; // Ok, should not produce stderr.
68 &empty[..]; // Ok, should not produce stderr.
75 &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
79 &v[..]; // Ok, should not produce stderr.
82 // Continue tests at end function to minimize the changes to this file's corresponding stderr.
85 const N: usize = 15; // Out of bounds
86 const M: usize = 3; // In bounds
87 x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
88 x[M]; // Ok, should not produce stderr.
94 &x[num..10]; // should trigger out of bounds error
95 &x[10..num]; // should trigger out of bounds error