From 5c3928282699244f5e85a66f0cded09ea3dfbeda Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sun, 14 Oct 2018 07:49:28 -0700 Subject: [PATCH] out_of_bounds_indexing refactoring --- clippy_lints/src/indexing_slicing.rs | 65 +++++++++++++--------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 8b7a1f7882b7..9f9c25f77281 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -108,46 +108,41 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { if let ExprKind::Index(ref array, ref index) = &expr.node { let ty = cx.tables.expr_ty(array); if let Some(range) = higher::range(cx, index) { + // Ranged indexes, i.e. &x[n..m], &x[n..], &x[..n] and &x[..] if let ty::Array(_, s) = ty.sty { let size: u128 = s.assert_usize(cx.tcx).unwrap().into(); - match to_const_range(cx, range, size) { - (None, None) => {}, - (Some(start), None) => { - if start > size { - utils::span_lint( - cx, - OUT_OF_BOUNDS_INDEXING, - expr.span, - "range is out of bounds", - ); - return; - } - }, - (None, Some(end)) => { - if end > size { - utils::span_lint( - cx, - OUT_OF_BOUNDS_INDEXING, - expr.span, - "range is out of bounds", - ); - return; - } - }, - (Some(start), Some(end)) => { - if start > size || end > size { - utils::span_lint( - cx, - OUT_OF_BOUNDS_INDEXING, - expr.span, - "range is out of bounds", - ); - } - // early return because both start and end are constant + let const_range = to_const_range(cx, range, size); + + if let (Some(start), _) = const_range { + if start > size { + utils::span_lint( + cx, + OUT_OF_BOUNDS_INDEXING, + expr.span, + "range is out of bounds", + ); return; - }, + } + } + + if let (_, Some(end)) = const_range { + if end > size { + utils::span_lint( + cx, + OUT_OF_BOUNDS_INDEXING, + expr.span, + "range is out of bounds", + ); + return; + } + } + + if let (Some(_), Some(_)) = const_range { + // early return because both start and end are constants + // and we have proven above that they are in bounds + return; } }