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.
11 //! lint on indexing and slicing operations
13 use crate::consts::{constant, Constant};
15 use crate::utils::higher;
16 use crate::utils::higher::Range;
17 use crate::rustc::hir::*;
18 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
19 use crate::rustc::{declare_tool_lint, lint_array};
21 use crate::syntax::ast::RangeLimits;
23 /// **What it does:** Checks for out of bounds array indexing with a constant
26 /// **Why is this bad?** This will always panic at runtime.
28 /// **Known problems:** Hopefully none.
32 /// let x = [1,2,3,4];
42 declare_clippy_lint! {
43 pub OUT_OF_BOUNDS_INDEXING,
45 "out of bounds constant indexing"
48 /// **What it does:** Checks for usage of indexing or slicing. Arrays are special cased, this lint
49 /// does report on arrays if we can tell that slicing operations are in bounds and does not
50 /// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
52 /// **Why is this bad?** Indexing and slicing can panic at runtime and there are
53 /// safe alternatives.
55 /// **Known problems:** Hopefully none.
60 /// let x = vec![0; 5];
75 /// let y = [0, 1, 2, 3];
91 declare_clippy_lint! {
94 "indexing/slicing usage"
97 #[derive(Copy, Clone)]
98 pub struct IndexingSlicing;
100 impl LintPass for IndexingSlicing {
101 fn get_lints(&self) -> LintArray {
102 lint_array!(INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING)
106 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
107 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
108 if let ExprKind::Index(ref array, ref index) = &expr.node {
109 let ty = cx.tables.expr_ty(array);
110 if let Some(range) = higher::range(cx, index) {
111 // Ranged indexes, i.e. &x[n..m], &x[n..], &x[..n] and &x[..]
112 if let ty::Array(_, s) = ty.sty {
113 let size: u128 = s.assert_usize(cx.tcx).unwrap().into();
114 // Index is a constant range.
115 if let Some((start, end)) = to_const_range(cx, range, size) {
116 if start > size || end > size {
119 OUT_OF_BOUNDS_INDEXING,
121 "range is out of bounds",
128 let help_msg = match (range.start, range.end) {
129 (None, Some(_)) => "Consider using `.get(..n)`or `.get_mut(..n)` instead",
130 (Some(_), None) => "Consider using `.get(n..)` or .get_mut(n..)` instead",
131 (Some(_), Some(_)) => "Consider using `.get(n..m)` or `.get_mut(n..m)` instead",
132 (None, None) => return, // [..] is ok.
135 utils::span_help_and_lint(
139 "slicing may panic.",
143 // Catchall non-range index, i.e. [n] or [n << m]
144 if let ty::Array(..) = ty.sty {
145 // Index is a constant uint.
146 if let Some(..) = constant(cx, cx.tables, index) {
147 // Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
152 utils::span_help_and_lint(
156 "indexing may panic.",
157 "Consider using `.get(n)` or `.get_mut(n)` instead",
164 /// Returns an option containing a tuple with the start and end (exclusive) of
166 fn to_const_range<'a, 'tcx>(
167 cx: &LateContext<'a, 'tcx>,
170 ) -> Option<(u128, u128)> {
173 .map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
174 let start = match s {
175 Some(Some(Constant::Int(x))) => x,
176 Some(_) => return None,
182 .map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c));
184 Some(Some(Constant::Int(x))) => if range.limits == RangeLimits::Closed {
189 Some(_) => return None,