]>
Commit | Line | Data |
---|---|---|
04454e1e | 1 | #![feature(inline_const)] |
f20569fa XL |
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)] | |
04454e1e FG |
6 | #![allow(const_err, clippy::no_effect, clippy::unnecessary_operation)] |
7 | ||
8 | const ARR: [i32; 2] = [1, 2]; | |
9 | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr. | |
10 | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts. | |
11 | ||
12 | const fn idx() -> usize { | |
13 | 1 | |
14 | } | |
15 | const fn idx4() -> usize { | |
16 | 4 | |
17 | } | |
f20569fa XL |
18 | |
19 | fn main() { | |
20 | let x = [1, 2, 3, 4]; | |
21 | let index: usize = 1; | |
22 | x[index]; | |
04454e1e FG |
23 | x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. |
24 | x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. | |
f20569fa XL |
25 | |
26 | x[0]; // Ok, should not produce stderr. | |
27 | x[3]; // Ok, should not produce stderr. | |
04454e1e FG |
28 | x[const { idx() }]; // Ok, should not produce stderr. |
29 | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. | |
30 | const { &ARR[idx()] }; // Ok, should not produce stderr. | |
31 | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts. | |
f20569fa XL |
32 | |
33 | let y = &x; | |
34 | y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021 | |
35 | y[4]; // Ok, rustc will handle references too. | |
36 | ||
37 | let v = vec![0; 5]; | |
38 | v[0]; | |
39 | v[10]; | |
40 | v[1 << 3]; | |
41 | ||
42 | const N: usize = 15; // Out of bounds | |
43 | const M: usize = 3; // In bounds | |
04454e1e | 44 | x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays. |
f20569fa XL |
45 | x[M]; // Ok, should not produce stderr. |
46 | v[N]; | |
47 | v[M]; | |
48 | } |