]> git.lizzy.rs Git - rust.git/blob - tests/ui/indexing_slicing.rs
Auto merge of #4314 - chansuke:add-negation-to-is_empty, r=flip1995
[rust.git] / tests / ui / indexing_slicing.rs
1 #![feature(plugin)]
2 #![warn(clippy::indexing_slicing)]
3 // We also check the out_of_bounds_indexing lint here, because it lints similar things and
4 // we want to avoid false positives.
5 #![warn(clippy::out_of_bounds_indexing)]
6 #![allow(clippy::no_effect, clippy::unnecessary_operation)]
7
8 fn main() {
9     let x = [1, 2, 3, 4];
10     let index: usize = 1;
11     let index_from: usize = 2;
12     let index_to: usize = 3;
13     x[index];
14     &x[index..];
15     &x[..index];
16     &x[index_from..index_to];
17     &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
18     x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
19     x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
20     &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
21     &x[0..][..3];
22     &x[1..][..5];
23
24     &x[0..].get(..3); // Ok, should not produce stderr.
25     x[0]; // Ok, should not produce stderr.
26     x[3]; // Ok, should not produce stderr.
27     &x[0..3]; // Ok, should not produce stderr.
28
29     let y = &x;
30     y[0];
31     &y[1..2];
32     &y[0..=4];
33     &y[..=4];
34
35     &y[..]; // Ok, should not produce stderr.
36
37     let v = vec![0; 5];
38     v[0];
39     v[10];
40     v[1 << 3];
41     &v[10..100];
42     &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
43     &v[10..];
44     &v[..100];
45
46     &v[..]; // Ok, should not produce stderr.
47
48     //
49     // Continue tests at end function to minimize the changes to this file's corresponding stderr.
50     //
51
52     const N: usize = 15; // Out of bounds
53     const M: usize = 3; // In bounds
54     x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
55     x[M]; // Ok, should not produce stderr.
56     v[N];
57     v[M];
58 }