2 use rustc::middle::const_eval::EvalHint::ExprTypeChecked;
3 use rustc::middle::const_eval::{eval_const_expr_partial, ConstVal};
4 use rustc::middle::ty::TyArray;
5 use rustc_front::hir::*;
8 /// **What it does:** Check for out of bounds array indexing with a constant index.
10 /// **Why is this bad?** This will always panic at runtime.
12 /// **Known problems:** Hopefully none.
17 /// let x = [1,2,3,4];
22 pub OUT_OF_BOUNDS_INDEXING,
24 "out of bound constant indexing"
28 pub struct ArrayIndexing;
30 impl LintPass for ArrayIndexing {
31 fn get_lints(&self) -> LintArray {
32 lint_array!(OUT_OF_BOUNDS_INDEXING)
36 impl LateLintPass for ArrayIndexing {
37 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
38 if let ExprIndex(ref array, ref index) = e.node {
39 let ty = cx.tcx.expr_ty(array);
41 if let TyArray(_, size) = ty.sty {
42 let index = eval_const_expr_partial(cx.tcx, &index, ExprTypeChecked, None);
43 if let Ok(ConstVal::Uint(index)) = index {
44 if size as u64 <= index {
45 span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span,
46 "const index-expr is out of bounds");