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