]> git.lizzy.rs Git - rust.git/commitdiff
out_of_bounds_indexing refactoring
authorJosh Mcguigan <joshmcg88@gmail.com>
Sun, 14 Oct 2018 14:49:28 +0000 (07:49 -0700)
committerJosh Mcguigan <joshmcg88@gmail.com>
Sun, 14 Oct 2018 14:49:28 +0000 (07:49 -0700)
clippy_lints/src/indexing_slicing.rs

index 8b7a1f7882b78e521d5ac6f6aa1c0c07f97cbf41..9f9c25f7728105fd4d7cb9fc627972604fb37677 100644 (file)
@@ -108,46 +108,41 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         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;
                     }
                 }