Removing lint for constant usize
array indexing
This commit removes the logic in this PR that linted out-of-bounds constant `usize` indexing on arrays. That case is already handled by rustc's `const_err` lint. Beyond removing the linting logic, the test file and its associated stderr were updated to verify that const `usize` indexing operations on arrays are no longer handled by this `indexing_slicing` lint.
This commit is contained in:
parent
e63f5dfedb
commit
c479b3bc28
@ -34,9 +34,9 @@ declare_clippy_lint! {
|
|||||||
"out of bounds constant indexing"
|
"out of bounds constant indexing"
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **What it does:** Checks for usage of indexing or slicing. Does not report
|
/// **What it does:** Checks for usage of indexing or slicing. Arrays are special cased, this lint
|
||||||
/// on arrays if we can tell that the indexing or slicing operations are in
|
/// does report on arrays if we can tell that slicing operations are in bounds and does not
|
||||||
/// bounds.
|
/// lint on constant `usize` indexing on arrays because that is handled by rustc's `const_err` lint.
|
||||||
///
|
///
|
||||||
/// **Why is this bad?** Indexing and slicing can panic at runtime and there are
|
/// **Why is this bad?** Indexing and slicing can panic at runtime and there are
|
||||||
/// safe alternatives.
|
/// safe alternatives.
|
||||||
@ -64,13 +64,11 @@ declare_clippy_lint! {
|
|||||||
/// let y = [0, 1, 2, 3];
|
/// let y = [0, 1, 2, 3];
|
||||||
///
|
///
|
||||||
/// // Bad
|
/// // Bad
|
||||||
/// y[10];
|
|
||||||
/// &y[10..100];
|
/// &y[10..100];
|
||||||
/// &y[10..];
|
/// &y[10..];
|
||||||
/// &y[..100];
|
/// &y[..100];
|
||||||
///
|
///
|
||||||
/// // Good
|
/// // Good
|
||||||
/// y[2];
|
|
||||||
/// &y[2..];
|
/// &y[2..];
|
||||||
/// &y[..2];
|
/// &y[..2];
|
||||||
/// &y[0..3];
|
/// &y[0..3];
|
||||||
@ -132,20 +130,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Catchall non-range index, i.e. [n] or [n << m]
|
// Catchall non-range index, i.e. [n] or [n << m]
|
||||||
if let ty::TyArray(_, s) = ty.sty {
|
if let ty::TyArray(..) = ty.sty {
|
||||||
let size: u128 = s.assert_usize(cx.tcx).unwrap().into();
|
|
||||||
// Index is a constant uint.
|
// Index is a constant uint.
|
||||||
if let Some((Constant::Int(const_index), _)) = constant(cx, cx.tables, index) {
|
if let Some(..) = constant(cx, cx.tables, index) {
|
||||||
if size <= const_index {
|
// Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
|
||||||
utils::span_lint(
|
|
||||||
cx,
|
|
||||||
OUT_OF_BOUNDS_INDEXING,
|
|
||||||
expr.span,
|
|
||||||
"const index is out of bounds",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Else index is in bounds, ok.
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ fn main() {
|
|||||||
&x[..index];
|
&x[..index];
|
||||||
&x[index_from..index_to];
|
&x[index_from..index_to];
|
||||||
&x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
|
&x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
|
||||||
x[4];
|
x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
|
||||||
x[1 << 3];
|
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
|
||||||
&x[..=4];
|
&x[..=4];
|
||||||
&x[1..5];
|
&x[1..5];
|
||||||
&x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
|
&x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
|
||||||
@ -44,7 +44,7 @@ fn main() {
|
|||||||
&y[..]; // Ok, should not produce stderr.
|
&y[..]; // Ok, should not produce stderr.
|
||||||
|
|
||||||
let empty: [i8; 0] = [];
|
let empty: [i8; 0] = [];
|
||||||
empty[0];
|
empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
|
||||||
&empty[1..5];
|
&empty[1..5];
|
||||||
&empty[0..=4];
|
&empty[0..=4];
|
||||||
&empty[..=4];
|
&empty[..=4];
|
||||||
@ -75,7 +75,7 @@ fn main() {
|
|||||||
|
|
||||||
const N: usize = 15; // Out of bounds
|
const N: usize = 15; // Out of bounds
|
||||||
const M: usize = 3; // In bounds
|
const M: usize = 3; // In bounds
|
||||||
x[N];
|
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
|
||||||
x[M]; // Ok, should not produce stderr.
|
x[M]; // Ok, should not produce stderr.
|
||||||
v[N];
|
v[N];
|
||||||
v[M];
|
v[M];
|
||||||
|
@ -47,25 +47,13 @@ error: slicing may panic.
|
|||||||
|
|
|
|
||||||
= help: Consider using `.get(n..)` or .get_mut(n..)` instead
|
= help: Consider using `.get(n..)` or .get_mut(n..)` instead
|
||||||
|
|
||||||
error: const index is out of bounds
|
|
||||||
--> $DIR/indexing_slicing.rs:16:5
|
|
||||||
|
|
|
||||||
16 | x[4];
|
|
||||||
| ^^^^
|
|
||||||
|
|
|
||||||
= note: `-D out-of-bounds-indexing` implied by `-D warnings`
|
|
||||||
|
|
||||||
error: const index is out of bounds
|
|
||||||
--> $DIR/indexing_slicing.rs:17:5
|
|
||||||
|
|
|
||||||
17 | x[1 << 3];
|
|
||||||
| ^^^^^^^^^
|
|
||||||
|
|
||||||
error: range is out of bounds
|
error: range is out of bounds
|
||||||
--> $DIR/indexing_slicing.rs:18:6
|
--> $DIR/indexing_slicing.rs:18:6
|
||||||
|
|
|
|
||||||
18 | &x[..=4];
|
18 | &x[..=4];
|
||||||
| ^^^^^^^
|
| ^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `-D out-of-bounds-indexing` implied by `-D warnings`
|
||||||
|
|
||||||
error: range is out of bounds
|
error: range is out of bounds
|
||||||
--> $DIR/indexing_slicing.rs:19:6
|
--> $DIR/indexing_slicing.rs:19:6
|
||||||
@ -159,12 +147,6 @@ error: slicing may panic.
|
|||||||
|
|
|
|
||||||
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
||||||
|
|
||||||
error: const index is out of bounds
|
|
||||||
--> $DIR/indexing_slicing.rs:47:5
|
|
||||||
|
|
|
||||||
47 | empty[0];
|
|
||||||
| ^^^^^^^^
|
|
||||||
|
|
||||||
error: range is out of bounds
|
error: range is out of bounds
|
||||||
--> $DIR/indexing_slicing.rs:48:6
|
--> $DIR/indexing_slicing.rs:48:6
|
||||||
|
|
|
|
||||||
@ -269,12 +251,6 @@ error: slicing may panic.
|
|||||||
|
|
|
|
||||||
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
||||||
|
|
||||||
error: const index is out of bounds
|
|
||||||
--> $DIR/indexing_slicing.rs:78:5
|
|
||||||
|
|
|
||||||
78 | x[N];
|
|
||||||
| ^^^^
|
|
||||||
|
|
||||||
error: indexing may panic.
|
error: indexing may panic.
|
||||||
--> $DIR/indexing_slicing.rs:80:5
|
--> $DIR/indexing_slicing.rs:80:5
|
||||||
|
|
|
|
||||||
@ -291,5 +267,5 @@ error: indexing may panic.
|
|||||||
|
|
|
|
||||||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
error: aborting due to 41 previous errors
|
error: aborting due to 37 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user