]> git.lizzy.rs Git - rust.git/blob - tests/ui/indexing_slicing.rs
Merge branch 'master' into rustfmt_tests
[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 #![feature(plugin)]
11 #![warn(clippy::indexing_slicing)]
12 #![warn(clippy::out_of_bounds_indexing)]
13 #![allow(clippy::no_effect, clippy::unnecessary_operation)]
14
15 fn main() {
16     let x = [1, 2, 3, 4];
17     let index: usize = 1;
18     let index_from: usize = 2;
19     let index_to: usize = 3;
20     x[index];
21     &x[index..];
22     &x[..index];
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.
27     &x[..=4];
28     &x[1..5];
29     &x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
30     &x[5..];
31     &x[..5];
32     &x[5..].iter().map(|x| 2 * x).collect::<Vec<i32>>();
33     &x[0..=4];
34     &x[0..][..3];
35     &x[1..][..5];
36
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.
46
47     let y = &x;
48     y[0];
49     &y[1..2];
50     &y[0..=4];
51     &y[..=4];
52
53     &y[..]; // Ok, should not produce stderr.
54
55     let empty: [i8; 0] = [];
56     empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
57     &empty[1..5];
58     &empty[0..=4];
59     &empty[..=4];
60     &empty[1..];
61     &empty[..4];
62     &empty[0..=0];
63     &empty[..=0];
64
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.
69
70     let v = vec![0; 5];
71     v[0];
72     v[10];
73     v[1 << 3];
74     &v[10..100];
75     &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
76     &v[10..];
77     &v[..100];
78
79     &v[..]; // Ok, should not produce stderr.
80
81     //
82     // Continue tests at end function to minimize the changes to this file's corresponding stderr.
83     //
84
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.
89     v[N];
90     v[M];
91
92     // issue 3102
93     let num = 1;
94     &x[num..10]; // should trigger out of bounds error
95     &x[10..num]; // should trigger out of bounds error
96 }