@@ -3,6 +3,7 @@ use rustc::ty;
3
3
use rustc:: hir;
4
4
use syntax:: ast:: RangeLimits ;
5
5
use utils:: { self , higher} ;
6
+ use utils:: higher:: Range ;
6
7
use consts:: { constant, Constant } ;
7
8
8
9
/// **What it does:** Checks for out of bounds array indexing with a constant
@@ -73,10 +74,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
73
74
74
75
// Index is a constant range
75
76
if let Some ( range) = higher:: range ( index) {
76
- let start = range. start . map ( |start| constant ( cx, start) . map ( |( c, _) | c) ) ;
77
- let end = range. end . map ( |end| constant ( cx, end) . map ( |( c, _) | c) ) ;
78
-
79
- if let Some ( ( start, end) ) = to_const_range ( & start, & end, range. limits , size) {
77
+ if let Some ( ( start, end) ) = to_const_range ( cx, range, size) {
80
78
if start > size || end > size {
81
79
utils:: span_lint ( cx, OUT_OF_BOUNDS_INDEXING , e. span , "range is out of bounds" ) ;
82
80
}
@@ -102,20 +100,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIndexing {
102
100
103
101
/// Returns an option containing a tuple with the start and end (exclusive) of
104
102
/// the range.
105
- fn to_const_range (
106
- start : & Option < Option < Constant > > ,
107
- end : & Option < Option < Constant > > ,
108
- limits : RangeLimits ,
109
- array_size : u128 ,
110
- ) -> Option < ( u128 , u128 ) > {
111
- let start = match * start {
103
+ fn to_const_range < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , range : Range , array_size : u128 ) -> Option < ( u128 , u128 ) > {
104
+ let s = range. start . map ( |expr| constant ( cx, expr) . map ( |( c, _) | c) ) ;
105
+ let start = match s {
112
106
Some ( Some ( Constant :: Int ( x) ) ) => x,
113
107
Some ( _) => return None ,
114
108
None => 0 ,
115
109
} ;
116
110
117
- let end = match * end {
118
- Some ( Some ( Constant :: Int ( x) ) ) => if limits == RangeLimits :: Closed {
111
+ let e = range. end . map ( |expr| constant ( cx, expr) . map ( |( c, _) | c) ) ;
112
+ let end = match e {
113
+ Some ( Some ( Constant :: Int ( x) ) ) => if range. limits == RangeLimits :: Closed {
119
114
x + 1
120
115
} else {
121
116
x
0 commit comments