]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/indexing_slicing_index.rs
Rollup merge of #102072 - scottmcm:ptr-alignment-type, r=thomcc
[rust.git] / src / tools / clippy / tests / ui / indexing_slicing_index.rs
1 #![feature(inline_const)]
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)]
6 #![allow(unconditional_panic, 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 }
18
19 fn main() {
20     let x = [1, 2, 3, 4];
21     let index: usize = 1;
22     x[index];
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.
25
26     x[0]; // Ok, should not produce stderr.
27     x[3]; // Ok, should not produce stderr.
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.
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
44     x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
45     x[M]; // Ok, should not produce stderr.
46     v[N];
47     v[M];
48 }