]> git.lizzy.rs Git - rust.git/blob - tests/ui/indexing_slicing.rs
Adapt the *.stderr files of the ui-tests to the tool_lints
[rust.git] / tests / ui / indexing_slicing.rs
1 #![feature(tool_lints)]
2
3 #![feature(plugin)]
4 #![warn(clippy::indexing_slicing)]
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[..=4];
21     &x[1..5];
22     &x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
23     &x[5..];
24     &x[..5];
25     &x[5..].iter().map(|x| 2 * x).collect::<Vec<i32>>();
26     &x[0..=4];
27     &x[0..][..3];
28     &x[1..][..5];
29
30     &x[4..]; // Ok, should not produce stderr.
31     &x[..4]; // Ok, should not produce stderr.
32     &x[..]; // Ok, should not produce stderr.
33     &x[1..]; // Ok, should not produce stderr.
34     &x[2..].iter().map(|x| 2 * x).collect::<Vec<i32>>(); // Ok, should not produce stderr.
35     &x[0..].get(..3); // Ok, should not produce stderr.
36     x[0]; // Ok, should not produce stderr.
37     x[3]; // Ok, should not produce stderr.
38     &x[0..3]; // Ok, should not produce stderr.
39
40     let y = &x;
41     y[0];
42     &y[1..2];
43     &y[0..=4];
44     &y[..=4];
45
46     &y[..]; // Ok, should not produce stderr.
47
48     let empty: [i8; 0] = [];
49     empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
50     &empty[1..5];
51     &empty[0..=4];
52     &empty[..=4];
53     &empty[1..];
54     &empty[..4];
55     &empty[0..=0];
56     &empty[..=0];
57
58     &empty[0..]; // Ok, should not produce stderr.
59     &empty[0..0]; // Ok, should not produce stderr.
60     &empty[..0]; // Ok, should not produce stderr.
61     &empty[..]; // Ok, should not produce stderr.
62
63     let v = vec![0; 5];
64     v[0];
65     v[10];
66     v[1 << 3];
67     &v[10..100];
68     &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
69     &v[10..];
70     &v[..100];
71
72     &v[..]; // Ok, should not produce stderr.
73
74     //
75     // Continue tests at end function to minimize the changes to this file's corresponding stderr.
76     //
77
78     const N: usize = 15; // Out of bounds
79     const M: usize = 3; // In bounds
80     x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
81     x[M]; // Ok, should not produce stderr.
82     v[N];
83     v[M];
84 }