1 //! lint on indexing and slicing operations
3 use crate::consts::{constant, Constant};
5 use crate::utils::higher;
6 use crate::utils::higher::Range;
9 use rustc::{declare_lint, lint_array};
11 use syntax::ast::RangeLimits;
13 /// **What it does:** Checks for out of bounds array indexing with a constant
16 /// **Why is this bad?** This will always panic at runtime.
18 /// **Known problems:** Hopefully none.
22 /// let x = [1,2,3,4];
32 declare_clippy_lint! {
33 pub OUT_OF_BOUNDS_INDEXING,
35 "out of bounds constant indexing"
38 /// **What it does:** Checks for usage of indexing or slicing. Arrays are special cased, this lint
39 /// does report on arrays if we can tell that slicing operations are in bounds and does not
40 /// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
42 /// **Why is this bad?** Indexing and slicing can panic at runtime and there are
43 /// safe alternatives.
45 /// **Known problems:** Hopefully none.
50 /// let x = vec![0; 5];
65 /// let y = [0, 1, 2, 3];
81 declare_clippy_lint! {
84 "indexing/slicing usage"
87 #[derive(Copy, Clone)]
88 pub struct IndexingSlicing;
90 impl LintPass for IndexingSlicing {
91 fn get_lints(&self) -> LintArray {
92 lint_array!(INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING)
96 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
97 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
98 if let ExprKind::Index(ref array, ref index) = &expr.node {
99 let ty = cx.tables.expr_ty(array);
100 if let Some(range) = higher::range(cx, index) {
101 // Ranged indexes, i.e. &x[n..m], &x[n..], &x[..n] and &x[..]
102 if let ty::TyArray(_, s) = ty.sty {
103 let size: u128 = s.assert_usize(cx.tcx).unwrap().into();
104 // Index is a constant range.
105 if let Some((start, end)) = to_const_range(cx, range, size) {
106 if start > size || end > size {
109 OUT_OF_BOUNDS_INDEXING,
111 "range is out of bounds",
118 let help_msg = match (range.start, range.end) {
119 (None, Some(_)) => "Consider using `.get(..n)`or `.get_mut(..n)` instead",
120 (Some(_), None) => "Consider using `.get(n..)` or .get_mut(n..)` instead",
121 (Some(_), Some(_)) => "Consider using `.get(n..m)` or `.get_mut(n..m)` instead",
122 (None, None) => return, // [..] is ok.
125 utils::span_help_and_lint(
129 "slicing may panic.",
133 // Catchall non-range index, i.e. [n] or [n << m]
134 if let ty::TyArray(..) = ty.sty {
135 // Index is a constant uint.
136 if let Some(..) = constant(cx, cx.tables, index) {
137 // Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
142 utils::span_help_and_lint(
146 "indexing may panic.",
147 "Consider using `.get(n)` or `.get_mut(n)` instead",
154 /// Returns an option containing a tuple with the start and end (exclusive) of
156 fn to_const_range<'a, 'tcx>(
157 cx: &LateContext<'a, 'tcx>,
160 ) -> Option<(u128, u128)> {
163 .map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
164 let start = match s {
165 Some(Some(Constant::Int(x))) => x,
166 Some(_) => return None,
172 .map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
174 Some(Some(Constant::Int(x))) => if range.limits == RangeLimits::Closed {
179 Some(_) => return None,