|
| 1 | +//! lint on indexing and slicing operations |
| 2 | +
|
| 3 | +use crate::consts::{constant, Constant}; |
| 4 | +use crate::utils; |
| 5 | +use crate::utils::higher; |
| 6 | +use crate::utils::higher::Range; |
| 7 | +use rustc::hir::*; |
| 8 | +use rustc::lint::*; |
| 9 | +use rustc::ty; |
| 10 | +use syntax::ast::RangeLimits; |
| 11 | + |
| 12 | +/// **What it does:** Checks for out of bounds array indexing with a constant |
| 13 | +/// index. |
| 14 | +/// |
| 15 | +/// **Why is this bad?** This will always panic at runtime. |
| 16 | +/// |
| 17 | +/// **Known problems:** Hopefully none. |
| 18 | +/// |
| 19 | +/// **Example:** |
| 20 | +/// ```rust |
| 21 | +/// let x = [1,2,3,4]; |
| 22 | +/// |
| 23 | +/// // Bad |
| 24 | +/// x[9]; |
| 25 | +/// &x[2..9]; |
| 26 | +/// |
| 27 | +/// // Good |
| 28 | +/// x[0]; |
| 29 | +/// x[3]; |
| 30 | +/// ``` |
| 31 | +declare_clippy_lint! { |
| 32 | + pub OUT_OF_BOUNDS_INDEXING, |
| 33 | + correctness, |
| 34 | + "out of bounds constant indexing" |
| 35 | +} |
| 36 | + |
| 37 | +/// **What it does:** Checks for usage of indexing or slicing. Arrays are special cased, this lint |
| 38 | +/// does report on arrays if we can tell that slicing operations are in bounds and does not |
| 39 | +/// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint. |
| 40 | +/// |
| 41 | +/// **Why is this bad?** Indexing and slicing can panic at runtime and there are |
| 42 | +/// safe alternatives. |
| 43 | +/// |
| 44 | +/// **Known problems:** Hopefully none. |
| 45 | +/// |
| 46 | +/// **Example:** |
| 47 | +/// ```rust |
| 48 | +/// // Vector |
| 49 | +/// let x = vec![0; 5]; |
| 50 | +/// |
| 51 | +/// // Bad |
| 52 | +/// x[2]; |
| 53 | +/// &x[2..100]; |
| 54 | +/// &x[2..]; |
| 55 | +/// &x[..100]; |
| 56 | +/// |
| 57 | +/// // Good |
| 58 | +/// x.get(2); |
| 59 | +/// x.get(2..100); |
| 60 | +/// x.get(2..); |
| 61 | +/// x.get(..100); |
| 62 | +/// |
| 63 | +/// // Array |
| 64 | +/// let y = [0, 1, 2, 3]; |
| 65 | +/// |
| 66 | +/// // Bad |
| 67 | +/// &y[10..100]; |
| 68 | +/// &y[10..]; |
| 69 | +/// &y[..100]; |
| 70 | +/// |
| 71 | +/// // Good |
| 72 | +/// &y[2..]; |
| 73 | +/// &y[..2]; |
| 74 | +/// &y[0..3]; |
| 75 | +/// y.get(10); |
| 76 | +/// y.get(10..100); |
| 77 | +/// y.get(10..); |
| 78 | +/// y.get(..100); |
| 79 | +/// ``` |
| 80 | +declare_clippy_lint! { |
| 81 | + pub INDEXING_SLICING, |
| 82 | + pedantic, |
| 83 | + "indexing/slicing usage" |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Copy, Clone)] |
| 87 | +pub struct IndexingSlicing; |
| 88 | + |
| 89 | +impl LintPass for IndexingSlicing { |
| 90 | + fn get_lints(&self) -> LintArray { |
| 91 | + lint_array!(INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing { |
| 96 | + fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { |
| 97 | + if let ExprIndex(ref array, ref index) = &expr.node { |
| 98 | + let ty = cx.tables.expr_ty(array); |
| 99 | + if let Some(range) = higher::range(cx, index) { |
| 100 | + // Ranged indexes, i.e. &x[n..m], &x[n..], &x[..n] and &x[..] |
| 101 | + if let ty::TyArray(_, s) = ty.sty { |
| 102 | + let size: u128 = s.assert_usize(cx.tcx).unwrap().into(); |
| 103 | + // Index is a constant range. |
| 104 | + if let Some((start, end)) = to_const_range(cx, range, size) { |
| 105 | + if start > size || end > size { |
| 106 | + utils::span_lint( |
| 107 | + cx, |
| 108 | + OUT_OF_BOUNDS_INDEXING, |
| 109 | + expr.span, |
| 110 | + "range is out of bounds", |
| 111 | + ); |
| 112 | + } |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + let help_msg = match (range.start, range.end) { |
| 118 | + (None, Some(_)) => "Consider using `.get(..n)`or `.get_mut(..n)` instead", |
| 119 | + (Some(_), None) => "Consider using `.get(n..)` or .get_mut(n..)` instead", |
| 120 | + (Some(_), Some(_)) => "Consider using `.get(n..m)` or `.get_mut(n..m)` instead", |
| 121 | + (None, None) => return, // [..] is ok. |
| 122 | + }; |
| 123 | + |
| 124 | + utils::span_help_and_lint( |
| 125 | + cx, |
| 126 | + INDEXING_SLICING, |
| 127 | + expr.span, |
| 128 | + "slicing may panic.", |
| 129 | + help_msg, |
| 130 | + ); |
| 131 | + } else { |
| 132 | + // Catchall non-range index, i.e. [n] or [n << m] |
| 133 | + if let ty::TyArray(..) = ty.sty { |
| 134 | + // Index is a constant uint. |
| 135 | + if let Some(..) = constant(cx, cx.tables, index) { |
| 136 | + // Let rustc's `const_err` lint handle constant `usize` indexing on arrays. |
| 137 | + return; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + utils::span_help_and_lint( |
| 142 | + cx, |
| 143 | + INDEXING_SLICING, |
| 144 | + expr.span, |
| 145 | + "indexing may panic.", |
| 146 | + "Consider using `.get(n)` or `.get_mut(n)` instead", |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// Returns an option containing a tuple with the start and end (exclusive) of |
| 154 | +/// the range. |
| 155 | +fn to_const_range<'a, 'tcx>( |
| 156 | + cx: &LateContext<'a, 'tcx>, |
| 157 | + range: Range, |
| 158 | + array_size: u128, |
| 159 | +) -> Option<(u128, u128)> { |
| 160 | + let s = range |
| 161 | + .start |
| 162 | + .map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c)); |
| 163 | + let start = match s { |
| 164 | + Some(Some(Constant::Int(x))) => x, |
| 165 | + Some(_) => return None, |
| 166 | + None => 0, |
| 167 | + }; |
| 168 | + |
| 169 | + let e = range |
| 170 | + .end |
| 171 | + .map(|expr| constant(cx, cx.tables, expr).map(|(c, _)| c)); |
| 172 | + let end = match e { |
| 173 | + Some(Some(Constant::Int(x))) => if range.limits == RangeLimits::Closed { |
| 174 | + x + 1 |
| 175 | + } else { |
| 176 | + x |
| 177 | + }, |
| 178 | + Some(_) => return None, |
| 179 | + None => array_size, |
| 180 | + }; |
| 181 | + |
| 182 | + Some((start, end)) |
| 183 | +} |
0 commit comments