1
+ #![ feature( inline_const) ]
1
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, since `suppress-restriction-lint-in-const` is set true.
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, since `suppress-restriction-lint-in-const` is set true.
31
+ const { & ARR [ idx4 ( ) ] } ; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
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
+ }
2
49
3
50
/// An opaque integer representation
4
51
pub struct Integer < ' a > {
@@ -11,5 +58,3 @@ impl<'a> Integer<'a> {
11
58
self . value [ 0 ] & 0b1000_0000 != 0
12
59
}
13
60
}
14
-
15
- fn main ( ) { }
0 commit comments