]> git.lizzy.rs Git - rust.git/blob - tests/ui/indexing_slicing.rs
Merge remote-tracking branch 'upstream/master'
[rust.git] / tests / ui / indexing_slicing.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10
11
12
13 #![feature(plugin)]
14 #![warn(clippy::indexing_slicing)]
15 #![warn(clippy::out_of_bounds_indexing)]
16 #![allow(clippy::no_effect, clippy::unnecessary_operation)]
17
18 fn main() {
19     let x = [1, 2, 3, 4];
20     let index: usize = 1;
21     let index_from: usize = 2;
22     let index_to: usize = 3;
23     x[index];
24     &x[index..];
25     &x[..index];
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.
30     &x[..=4];
31     &x[1..5];
32     &x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
33     &x[5..];
34     &x[..5];
35     &x[5..].iter().map(|x| 2 * x).collect::<Vec<i32>>();
36     &x[0..=4];
37     &x[0..][..3];
38     &x[1..][..5];
39
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.
49
50     let y = &x;
51     y[0];
52     &y[1..2];
53     &y[0..=4];
54     &y[..=4];
55
56     &y[..]; // Ok, should not produce stderr.
57
58     let empty: [i8; 0] = [];
59     empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
60     &empty[1..5];
61     &empty[0..=4];
62     &empty[..=4];
63     &empty[1..];
64     &empty[..4];
65     &empty[0..=0];
66     &empty[..=0];
67
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.
72
73     let v = vec![0; 5];
74     v[0];
75     v[10];
76     v[1 << 3];
77     &v[10..100];
78     &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
79     &v[10..];
80     &v[..100];
81
82     &v[..]; // Ok, should not produce stderr.
83
84     //
85     // Continue tests at end function to minimize the changes to this file's corresponding stderr.
86     //
87
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.
92     v[N];
93     v[M];
94 }