1 //! lint on indexing and slicing operations
3 use crate::consts::{constant, Constant};
5 use crate::utils::higher;
6 use crate::utils::higher::Range;
8 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
10 use rustc::{declare_lint_pass, declare_tool_lint};
11 use syntax::ast::RangeLimits;
13 declare_clippy_lint! {
14 /// **What it does:** Checks for out of bounds array indexing with a constant
17 /// **Why is this bad?** This will always panic at runtime.
19 /// **Known problems:** Hopefully none.
23 /// # #![allow(const_err)]
24 /// let x = [1, 2, 3, 4];
34 pub OUT_OF_BOUNDS_INDEXING,
36 "out of bounds constant indexing"
39 declare_clippy_lint! {
40 /// **What it does:** Checks for usage of indexing or slicing. Arrays are special cases, this lint
41 /// does report on arrays if we can tell that slicing operations are in bounds and does not
42 /// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
44 /// **Why is this bad?** Indexing and slicing can panic at runtime and there are
45 /// safe alternatives.
47 /// **Known problems:** Hopefully none.
52 /// let x = vec![0; 5];
67 /// let y = [0, 1, 2, 3];
85 "indexing/slicing usage"
88 declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
90 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
91 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
92 if let ExprKind::Index(ref array, ref index) = &expr.kind {
93 let ty = cx.tables.expr_ty(array);
94 if let Some(range) = higher::range(cx, index) {
95 // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
96 if let ty::Array(_, s) = ty.kind {
97 let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
99 let const_range = to_const_range(cx, range, size);
101 if let (Some(start), _) = const_range {
105 OUT_OF_BOUNDS_INDEXING,
106 range.start.map_or(expr.span, |start| start.span),
107 "range is out of bounds",
113 if let (_, Some(end)) = const_range {
117 OUT_OF_BOUNDS_INDEXING,
118 range.end.map_or(expr.span, |end| end.span),
119 "range is out of bounds",
125 if let (Some(_), Some(_)) = const_range {
126 // early return because both start and end are constants
127 // and we have proven above that they are in bounds
132 let help_msg = match (range.start, range.end) {
133 (None, Some(_)) => "Consider using `.get(..n)`or `.get_mut(..n)` instead",
134 (Some(_), None) => "Consider using `.get(n..)` or .get_mut(n..)` instead",
135 (Some(_), Some(_)) => "Consider using `.get(n..m)` or `.get_mut(n..m)` instead",
136 (None, None) => return, // [..] is ok.
139 utils::span_help_and_lint(cx, INDEXING_SLICING, expr.span, "slicing may panic.", help_msg);
141 // Catchall non-range index, i.e., [n] or [n << m]
142 if let ty::Array(..) = ty.kind {
143 // Index is a constant uint.
144 if let Some(..) = constant(cx, cx.tables, index) {
145 // Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
150 utils::span_help_and_lint(
154 "indexing may panic.",
155 "Consider using `.get(n)` or `.get_mut(n)` instead",
162 /// Returns a tuple of options with the start and end (exclusive) values of
163 /// the range. If the start or end is not constant, None is returned.
164 fn to_const_range<'a, 'tcx>(
165 cx: &LateContext<'a, 'tcx>,
168 ) -> (Option<u128>, Option<u128>) {
169 let s = range.start.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
170 let start = match s {
171 Some(Some(Constant::Int(x))) => Some(x),
176 let e = range.end.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
178 Some(Some(Constant::Int(x))) => {
179 if range.limits == RangeLimits::Closed {
186 None => Some(array_size),