1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint on indexing and slicing operations
12 use crate::consts::{constant, Constant};
13 use crate::rustc::hir::*;
14 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use crate::rustc::{declare_tool_lint, lint_array};
17 use crate::syntax::ast::RangeLimits;
19 use crate::utils::higher;
20 use crate::utils::higher::Range;
22 /// **What it does:** Checks for out of bounds array indexing with a constant
25 /// **Why is this bad?** This will always panic at runtime.
27 /// **Known problems:** Hopefully none.
31 /// let x = [1, 2, 3, 4];
41 declare_clippy_lint! {
42 pub OUT_OF_BOUNDS_INDEXING,
44 "out of bounds constant indexing"
47 /// **What it does:** Checks for usage of indexing or slicing. Arrays are special cased, this lint
48 /// does report on arrays if we can tell that slicing operations are in bounds and does not
49 /// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
51 /// **Why is this bad?** Indexing and slicing can panic at runtime and there are
52 /// safe alternatives.
54 /// **Known problems:** Hopefully none.
59 /// let x = vec![0; 5];
74 /// let y = [0, 1, 2, 3];
90 declare_clippy_lint! {
93 "indexing/slicing usage"
96 #[derive(Copy, Clone)]
97 pub struct IndexingSlicing;
99 impl LintPass for IndexingSlicing {
100 fn get_lints(&self) -> LintArray {
101 lint_array!(INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING)
105 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
106 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
107 if let ExprKind::Index(ref array, ref index) = &expr.node {
108 let ty = cx.tables.expr_ty(array);
109 if let Some(range) = higher::range(cx, index) {
110 // Ranged indexes, i.e. &x[n..m], &x[n..], &x[..n] and &x[..]
111 if let ty::Array(_, s) = ty.sty {
112 let size: u128 = s.assert_usize(cx.tcx).unwrap().into();
114 let const_range = to_const_range(cx, range, size);
116 if let (Some(start), _) = const_range {
120 OUT_OF_BOUNDS_INDEXING,
121 range.start.map_or(expr.span, |start| start.span),
122 "range is out of bounds",
128 if let (_, Some(end)) = const_range {
132 OUT_OF_BOUNDS_INDEXING,
133 range.end.map_or(expr.span, |end| end.span),
134 "range is out of bounds",
140 if let (Some(_), Some(_)) = const_range {
141 // early return because both start and end are constants
142 // and we have proven above that they are in bounds
147 let help_msg = match (range.start, range.end) {
148 (None, Some(_)) => "Consider using `.get(..n)`or `.get_mut(..n)` instead",
149 (Some(_), None) => "Consider using `.get(n..)` or .get_mut(n..)` instead",
150 (Some(_), Some(_)) => "Consider using `.get(n..m)` or `.get_mut(n..m)` instead",
151 (None, None) => return, // [..] is ok.
154 utils::span_help_and_lint(cx, INDEXING_SLICING, expr.span, "slicing may panic.", help_msg);
156 // Catchall non-range index, i.e. [n] or [n << m]
157 if let ty::Array(..) = ty.sty {
158 // Index is a constant uint.
159 if let Some(..) = constant(cx, cx.tables, index) {
160 // Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
165 utils::span_help_and_lint(
169 "indexing may panic.",
170 "Consider using `.get(n)` or `.get_mut(n)` instead",
177 /// Returns a tuple of options with the start and end (exclusive) values of
178 /// the range. If the start or end is not constant, None is returned.
179 fn to_const_range<'a, 'tcx>(
180 cx: &LateContext<'a, 'tcx>,
183 ) -> (Option<u128>, Option<u128>) {
184 let s = range.start.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
185 let start = match s {
186 Some(Some(Constant::Int(x))) => Some(x),
191 let e = range.end.map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
193 Some(Some(Constant::Int(x))) => {
194 if range.limits == RangeLimits::Closed {
201 None => Some(array_size),