1 //! lint on indexing and slicing operations
3 use crate::consts::{constant, Constant};
5 use crate::utils::higher;
6 use crate::utils::higher::Range;
7 use rustc::declare_lint_pass;
8 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
11 use rustc_session::declare_tool_lint;
12 use syntax::ast::RangeLimits;
14 declare_clippy_lint! {
15 /// **What it does:** Checks for out of bounds array indexing with a constant
18 /// **Why is this bad?** This will always panic at runtime.
20 /// **Known problems:** Hopefully none.
24 /// # #![allow(const_err)]
25 /// let x = [1, 2, 3, 4];
35 pub OUT_OF_BOUNDS_INDEXING,
37 "out of bounds constant indexing"
40 declare_clippy_lint! {
41 /// **What it does:** Checks for usage of indexing or slicing. Arrays are special cases, this lint
42 /// does report on arrays if we can tell that slicing operations are in bounds and does not
43 /// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
45 /// **Why is this bad?** Indexing and slicing can panic at runtime and there are
46 /// safe alternatives.
48 /// **Known problems:** Hopefully none.
53 /// let x = vec![0; 5];
68 /// let y = [0, 1, 2, 3];
86 "indexing/slicing usage"
89 declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
91 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
92 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
93 if let ExprKind::Index(ref array, ref index) = &expr.kind {
94 let ty = cx.tables.expr_ty(array);
95 if let Some(range) = higher::range(cx, index) {
96 // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
97 if let ty::Array(_, s) = ty.kind {
98 let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
100 let const_range = to_const_range(cx, range, size);
102 if let (Some(start), _) = const_range {
106 OUT_OF_BOUNDS_INDEXING,
107 range.start.map_or(expr.span, |start| start.span),
108 "range is out of bounds",
114 if let (_, Some(end)) = const_range {
118 OUT_OF_BOUNDS_INDEXING,
119 range.end.map_or(expr.span, |end| end.span),
120 "range is out of bounds",
126 if let (Some(_), Some(_)) = const_range {
127 // early return because both start and end are constants
128 // and we have proven above that they are in bounds
133 let help_msg = match (range.start, range.end) {
134 (None, Some(_)) => "Consider using `.get(..n)`or `.get_mut(..n)` instead",
135 (Some(_), None) => "Consider using `.get(n..)` or .get_mut(n..)` instead",
136 (Some(_), Some(_)) => "Consider using `.get(n..m)` or `.get_mut(n..m)` instead",
137 (None, None) => return, // [..] is ok.
140 utils::span_help_and_lint(cx, INDEXING_SLICING, expr.span, "slicing may panic.", help_msg);
142 // Catchall non-range index, i.e., [n] or [n << m]
143 if let ty::Array(..) = ty.kind {
144 // Index is a constant uint.
145 if let Some(..) = constant(cx, cx.tables, index) {
146 // Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
151 utils::span_help_and_lint(
155 "indexing may panic.",
156 "Consider using `.get(n)` or `.get_mut(n)` instead",
163 /// Returns a tuple of options with the start and end (exclusive) values of
164 /// the range. If the start or end is not constant, None is returned.
165 fn to_const_range<'a, 'tcx>(
166 cx: &LateContext<'a, 'tcx>,
169 ) -> (Option<u128>, Option<u128>) {
170 let s = range.start.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
171 let start = match s {
172 Some(Some(Constant::Int(x))) => Some(x),
177 let e = range.end.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
179 Some(Some(Constant::Int(x))) => {
180 if range.limits == RangeLimits::Closed {
187 None => Some(array_size),