2018-05-22 23:56:02 -05:00
|
|
|
//! lint on indexing and slicing operations
|
|
|
|
|
2021-06-03 01:41:37 -05:00
|
|
|
use clippy_utils::consts::{constant, Constant};
|
2022-12-17 07:12:54 -06:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::higher;
|
2020-02-29 21:23:33 -06:00
|
|
|
use rustc_ast::ast::RangeLimits;
|
2020-02-21 02:39:38 -06:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::ty;
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::impl_lint_pass;
|
2015-12-21 12:22:29 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for out of bounds array indexing with a constant
|
2019-03-05 10:50:33 -06:00
|
|
|
/// index.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// This will always panic at runtime.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```rust,no_run
|
2019-03-05 10:50:33 -06:00
|
|
|
/// let x = [1, 2, 3, 4];
|
|
|
|
///
|
|
|
|
/// x[9];
|
|
|
|
/// &x[2..9];
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-06-16 10:39:06 -05:00
|
|
|
/// # let x = [1, 2, 3, 4];
|
|
|
|
/// // Index within bounds
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
|
|
|
/// x[0];
|
|
|
|
/// x[3];
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2015-12-21 12:22:29 -06:00
|
|
|
pub OUT_OF_BOUNDS_INDEXING,
|
2018-03-28 08:24:26 -05:00
|
|
|
correctness,
|
2016-08-06 03:18:36 -05:00
|
|
|
"out of bounds constant indexing"
|
2015-12-21 12:22:29 -06:00
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usage of indexing or slicing. Arrays are special cases, this lint
|
2019-03-05 10:50:33 -06:00
|
|
|
/// does report on arrays if we can tell that slicing operations are in bounds and does not
|
|
|
|
/// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Indexing and slicing can panic at runtime and there are
|
2019-03-05 10:50:33 -06:00
|
|
|
/// safe alternatives.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-08-03 14:24:50 -05:00
|
|
|
/// ```rust,no_run
|
2019-03-05 10:50:33 -06:00
|
|
|
/// // Vector
|
|
|
|
/// let x = vec![0; 5];
|
|
|
|
///
|
|
|
|
/// x[2];
|
|
|
|
/// &x[2..100];
|
|
|
|
///
|
|
|
|
/// // Array
|
|
|
|
/// let y = [0, 1, 2, 3];
|
|
|
|
///
|
|
|
|
/// &y[10..100];
|
|
|
|
/// &y[10..];
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-06-16 10:39:06 -05:00
|
|
|
/// # #![allow(unused)]
|
|
|
|
///
|
|
|
|
/// # let x = vec![0; 5];
|
|
|
|
/// # let y = [0, 1, 2, 3];
|
|
|
|
/// x.get(2);
|
|
|
|
/// x.get(2..100);
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
|
|
|
/// y.get(10);
|
|
|
|
/// y.get(10..100);
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-03-11 03:51:16 -06:00
|
|
|
pub INDEXING_SLICING,
|
2018-08-02 12:06:03 -05:00
|
|
|
restriction,
|
2016-03-11 03:51:16 -06:00
|
|
|
"indexing/slicing usage"
|
|
|
|
}
|
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
impl_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]);
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct IndexingSlicing {
|
|
|
|
suppress_restriction_lint_in_const: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexingSlicing {
|
|
|
|
pub fn new(suppress_restriction_lint_in_const: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
suppress_restriction_lint_in_const,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 12:22:29 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-12-17 07:12:54 -06:00
|
|
|
if self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
|
2022-04-07 12:39:59 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:43:17 -05:00
|
|
|
if let ExprKind::Index(array, index, _) = &expr.kind {
|
2022-12-17 07:12:54 -06:00
|
|
|
let note = "the suggestion might not be applicable in constant blocks";
|
2020-09-24 07:49:22 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(array).peel_refs();
|
2021-08-08 09:49:13 -05:00
|
|
|
if let Some(range) = higher::Range::hir(index) {
|
2019-01-30 19:15:29 -06:00
|
|
|
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Array(_, s) = ty.kind() {
|
2023-02-14 02:51:19 -06:00
|
|
|
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) {
|
2020-03-02 14:53:41 -06:00
|
|
|
size.into()
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2018-10-13 15:51:53 -05:00
|
|
|
|
2018-10-14 09:49:28 -05:00
|
|
|
let const_range = to_const_range(cx, range, size);
|
|
|
|
|
|
|
|
if let (Some(start), _) = const_range {
|
|
|
|
if start > size {
|
2020-02-18 07:28:18 -06:00
|
|
|
span_lint(
|
2018-10-14 09:49:28 -05:00
|
|
|
cx,
|
|
|
|
OUT_OF_BOUNDS_INDEXING,
|
2018-10-15 06:44:39 -05:00
|
|
|
range.start.map_or(expr.span, |start| start.span),
|
2018-10-14 09:49:28 -05:00
|
|
|
"range is out of bounds",
|
|
|
|
);
|
2018-10-13 15:51:53 -05:00
|
|
|
return;
|
2018-10-14 09:49:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let (_, Some(end)) = const_range {
|
|
|
|
if end > size {
|
2020-02-18 07:28:18 -06:00
|
|
|
span_lint(
|
2018-10-14 09:49:28 -05:00
|
|
|
cx,
|
|
|
|
OUT_OF_BOUNDS_INDEXING,
|
2018-10-15 06:44:39 -05:00
|
|
|
range.end.map_or(expr.span, |end| end.span),
|
2018-10-14 09:49:28 -05:00
|
|
|
"range is out of bounds",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let (Some(_), Some(_)) = const_range {
|
|
|
|
// early return because both start and end are constants
|
|
|
|
// and we have proven above that they are in bounds
|
|
|
|
return;
|
2017-09-13 08:34:04 -05:00
|
|
|
}
|
2015-12-21 12:22:29 -06:00
|
|
|
}
|
2018-06-14 15:04:37 -05:00
|
|
|
|
|
|
|
let help_msg = match (range.start, range.end) {
|
2021-03-12 08:30:50 -06:00
|
|
|
(None, Some(_)) => "consider using `.get(..n)`or `.get_mut(..n)` instead",
|
|
|
|
(Some(_), None) => "consider using `.get(n..)` or .get_mut(n..)` instead",
|
|
|
|
(Some(_), Some(_)) => "consider using `.get(n..m)` or `.get_mut(n..m)` instead",
|
2018-06-14 15:04:37 -05:00
|
|
|
(None, None) => return, // [..] is ok.
|
|
|
|
};
|
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
|
|
|
|
diag.help(help_msg);
|
|
|
|
|
|
|
|
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
|
|
|
|
diag.note(note);
|
|
|
|
}
|
|
|
|
});
|
2018-06-14 15:04:37 -05:00
|
|
|
} else {
|
2019-01-30 19:15:29 -06:00
|
|
|
// Catchall non-range index, i.e., [n] or [n << m]
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Array(..) = ty.kind() {
|
2022-04-07 12:39:59 -05:00
|
|
|
// Index is a const block.
|
|
|
|
if let ExprKind::ConstBlock(..) = index.kind {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-14 15:04:37 -05:00
|
|
|
// Index is a constant uint.
|
2023-12-28 12:33:07 -06:00
|
|
|
if let Some(constant) = constant(cx, cx.typeck_results(), index) {
|
|
|
|
// only `usize` index is legal in rust array index
|
|
|
|
// leave other type to rustc
|
|
|
|
if let Constant::Int(off) = constant
|
2024-02-27 08:25:18 -06:00
|
|
|
&& off <= usize::MAX as u128
|
2023-12-28 12:33:07 -06:00
|
|
|
&& let ty::Uint(utype) = cx.typeck_results().expr_ty(index).kind()
|
|
|
|
&& *utype == ty::UintTy::Usize
|
|
|
|
&& let ty::Array(_, s) = ty.kind()
|
|
|
|
&& let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env)
|
|
|
|
{
|
|
|
|
// get constant offset and check whether it is in bounds
|
|
|
|
let off = usize::try_from(off).unwrap();
|
|
|
|
let size = usize::try_from(size).unwrap();
|
|
|
|
|
|
|
|
if off >= size {
|
|
|
|
span_lint(cx, OUT_OF_BOUNDS_INDEXING, expr.span, "index is out of bounds");
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 16:30:43 -05:00
|
|
|
// Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
|
2018-06-14 15:04:37 -05:00
|
|
|
return;
|
2016-03-11 03:51:16 -06:00
|
|
|
}
|
|
|
|
}
|
2018-06-14 15:04:37 -05:00
|
|
|
|
2022-12-17 07:12:54 -06:00
|
|
|
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
|
|
|
|
diag.help("consider using `.get(n)` or `.get_mut(n)` instead");
|
|
|
|
|
|
|
|
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
|
|
|
|
diag.note(note);
|
|
|
|
}
|
|
|
|
});
|
2015-12-21 12:22:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 03:51:16 -06:00
|
|
|
|
2018-10-13 15:51:53 -05:00
|
|
|
/// Returns a tuple of options with the start and end (exclusive) values of
|
|
|
|
/// the range. If the start or end is not constant, None is returned.
|
2022-11-21 13:34:47 -06:00
|
|
|
fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u128) -> (Option<u128>, Option<u128>) {
|
2023-05-20 08:39:26 -05:00
|
|
|
let s = range.start.map(|expr| constant(cx, cx.typeck_results(), expr));
|
2018-04-01 23:34:11 -05:00
|
|
|
let start = match s {
|
2018-10-13 15:51:53 -05:00
|
|
|
Some(Some(Constant::Int(x))) => Some(x),
|
|
|
|
Some(_) => None,
|
|
|
|
None => Some(0),
|
2016-03-11 03:51:16 -06:00
|
|
|
};
|
|
|
|
|
2023-05-20 08:39:26 -05:00
|
|
|
let e = range.end.map(|expr| constant(cx, cx.typeck_results(), expr));
|
2018-04-01 23:34:11 -05:00
|
|
|
let end = match e {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some(Some(Constant::Int(x))) => {
|
|
|
|
if range.limits == RangeLimits::Closed {
|
|
|
|
Some(x + 1)
|
|
|
|
} else {
|
|
|
|
Some(x)
|
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2018-10-13 15:51:53 -05:00
|
|
|
Some(_) => None,
|
|
|
|
None => Some(array_size),
|
2016-03-11 03:51:16 -06:00
|
|
|
};
|
|
|
|
|
2018-10-13 15:51:53 -05:00
|
|
|
(start, end)
|
2016-03-11 03:51:16 -06:00
|
|
|
}
|