@@ -8,7 +8,8 @@ use rustc::hir;
8
8
use syntax:: ast:: RangeLimits ;
9
9
use utils:: { self , higher} ;
10
10
11
- /// **What it does:** Checks for out of bounds array indexing with a constant index.
11
+ /// **What it does:** Checks for out of bounds array indexing with a constant
12
+ /// index.
12
13
///
13
14
/// **Why is this bad?** This will always panic at runtime.
14
15
///
@@ -46,7 +47,7 @@ declare_restriction_lint! {
46
47
"indexing/slicing usage"
47
48
}
48
49
49
- #[ derive( Copy , Clone ) ]
50
+ #[ derive( Copy , Clone ) ]
50
51
pub struct ArrayIndexing ;
51
52
52
53
impl LintPass for ArrayIndexing {
@@ -61,8 +62,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
61
62
// Array with known size can be checked statically
62
63
let ty = cx. tables . expr_ty ( array) ;
63
64
if let ty:: TyArray ( _, size) = ty. sty {
64
- let size = ConstInt :: Usize ( ConstUsize :: new ( size as u64 , cx. sess ( ) . target . uint_type )
65
- . expect ( "array size is invalid" ) ) ;
65
+ let size = ConstInt :: Usize (
66
+ ConstUsize :: new ( size as u64 , cx. sess ( ) . target . uint_type ) . expect ( "array size is invalid" ) ,
67
+ ) ;
66
68
let parent_item = cx. tcx . hir . get_parent ( e. id ) ;
67
69
let parent_def_id = cx. tcx . hir . local_def_id ( parent_item) ;
68
70
let substs = Substs :: identity_for_item ( cx. tcx , parent_def_id) ;
@@ -80,12 +82,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
80
82
81
83
// Index is a constant range
82
84
if let Some ( range) = higher:: range ( index) {
83
- let start = range. start
84
- . map ( |start| constcx. eval ( start) )
85
- . map ( |v| v. ok ( ) ) ;
86
- let end = range. end
87
- . map ( |end| constcx. eval ( end) )
88
- . map ( |v| v. ok ( ) ) ;
85
+ let start = range. start . map ( |start| constcx. eval ( start) ) . map ( |v| v. ok ( ) ) ;
86
+ let end = range. end . map ( |end| constcx. eval ( end) ) . map ( |v| v. ok ( ) ) ;
89
87
90
88
if let Some ( ( start, end) ) = to_const_range ( & start, & end, range. limits , size) {
91
89
if start > size || end > size {
@@ -111,12 +109,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
111
109
}
112
110
}
113
111
114
- /// Returns an option containing a tuple with the start and end (exclusive) of the range.
112
+ /// Returns an option containing a tuple with the start and end (exclusive) of
113
+ /// the range.
115
114
fn to_const_range (
116
115
start : & Option < Option < ConstVal > > ,
117
116
end : & Option < Option < ConstVal > > ,
118
117
limits : RangeLimits ,
119
- array_size : ConstInt
118
+ array_size : ConstInt ,
120
119
) -> Option < ( ConstInt , ConstInt ) > {
121
120
let start = match * start {
122
121
Some ( Some ( ConstVal :: Integral ( x) ) ) => x,
@@ -128,24 +127,23 @@ fn to_const_range(
128
127
Some ( Some ( ConstVal :: Integral ( x) ) ) => {
129
128
if limits == RangeLimits :: Closed {
130
129
match x {
131
- ConstInt :: U8 ( _) => ( x + ConstInt :: U8 ( 1 ) ) ,
132
- ConstInt :: U16 ( _) => ( x + ConstInt :: U16 ( 1 ) ) ,
133
- ConstInt :: U32 ( _) => ( x + ConstInt :: U32 ( 1 ) ) ,
134
- ConstInt :: U64 ( _) => ( x + ConstInt :: U64 ( 1 ) ) ,
135
- ConstInt :: U128 ( _) => ( x + ConstInt :: U128 ( 1 ) ) ,
136
- ConstInt :: Usize ( ConstUsize :: Us16 ( _) ) => ( x + ConstInt :: Usize ( ConstUsize :: Us16 ( 1 ) ) ) ,
137
- ConstInt :: Usize ( ConstUsize :: Us32 ( _) ) => ( x + ConstInt :: Usize ( ConstUsize :: Us32 ( 1 ) ) ) ,
138
- ConstInt :: Usize ( ConstUsize :: Us64 ( _) ) => ( x + ConstInt :: Usize ( ConstUsize :: Us64 ( 1 ) ) ) ,
139
- ConstInt :: I8 ( _) => ( x + ConstInt :: I8 ( 1 ) ) ,
140
- ConstInt :: I16 ( _) => ( x + ConstInt :: I16 ( 1 ) ) ,
141
- ConstInt :: I32 ( _) => ( x + ConstInt :: I32 ( 1 ) ) ,
142
- ConstInt :: I64 ( _) => ( x + ConstInt :: I64 ( 1 ) ) ,
143
- ConstInt :: I128 ( _) => ( x + ConstInt :: I128 ( 1 ) ) ,
144
- ConstInt :: Isize ( ConstIsize :: Is16 ( _) ) => ( x + ConstInt :: Isize ( ConstIsize :: Is16 ( 1 ) ) ) ,
145
- ConstInt :: Isize ( ConstIsize :: Is32 ( _) ) => ( x + ConstInt :: Isize ( ConstIsize :: Is32 ( 1 ) ) ) ,
146
- ConstInt :: Isize ( ConstIsize :: Is64 ( _) ) => ( x + ConstInt :: Isize ( ConstIsize :: Is64 ( 1 ) ) ) ,
147
- }
148
- . expect ( "such a big array is not realistic" )
130
+ ConstInt :: U8 ( _) => ( x + ConstInt :: U8 ( 1 ) ) ,
131
+ ConstInt :: U16 ( _) => ( x + ConstInt :: U16 ( 1 ) ) ,
132
+ ConstInt :: U32 ( _) => ( x + ConstInt :: U32 ( 1 ) ) ,
133
+ ConstInt :: U64 ( _) => ( x + ConstInt :: U64 ( 1 ) ) ,
134
+ ConstInt :: U128 ( _) => ( x + ConstInt :: U128 ( 1 ) ) ,
135
+ ConstInt :: Usize ( ConstUsize :: Us16 ( _) ) => ( x + ConstInt :: Usize ( ConstUsize :: Us16 ( 1 ) ) ) ,
136
+ ConstInt :: Usize ( ConstUsize :: Us32 ( _) ) => ( x + ConstInt :: Usize ( ConstUsize :: Us32 ( 1 ) ) ) ,
137
+ ConstInt :: Usize ( ConstUsize :: Us64 ( _) ) => ( x + ConstInt :: Usize ( ConstUsize :: Us64 ( 1 ) ) ) ,
138
+ ConstInt :: I8 ( _) => ( x + ConstInt :: I8 ( 1 ) ) ,
139
+ ConstInt :: I16 ( _) => ( x + ConstInt :: I16 ( 1 ) ) ,
140
+ ConstInt :: I32 ( _) => ( x + ConstInt :: I32 ( 1 ) ) ,
141
+ ConstInt :: I64 ( _) => ( x + ConstInt :: I64 ( 1 ) ) ,
142
+ ConstInt :: I128 ( _) => ( x + ConstInt :: I128 ( 1 ) ) ,
143
+ ConstInt :: Isize ( ConstIsize :: Is16 ( _) ) => ( x + ConstInt :: Isize ( ConstIsize :: Is16 ( 1 ) ) ) ,
144
+ ConstInt :: Isize ( ConstIsize :: Is32 ( _) ) => ( x + ConstInt :: Isize ( ConstIsize :: Is32 ( 1 ) ) ) ,
145
+ ConstInt :: Isize ( ConstIsize :: Is64 ( _) ) => ( x + ConstInt :: Isize ( ConstIsize :: Is64 ( 1 ) ) ) ,
146
+ } . expect ( "such a big array is not realistic" )
149
147
} else {
150
148
x
151
149
}
0 commit comments