mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
fix a FP in indexing_slicing
treat refs to arrays the same as arrays in `indexing_slicing` and `out_of_bounds_indexing`
This commit is contained in:
parent
06f1902878
commit
2ce2d6b40e
4 changed files with 16 additions and 35 deletions
|
@ -88,7 +88,7 @@ declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]
|
|||
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if let ExprKind::Index(ref array, ref index) = &expr.kind {
|
||||
let ty = cx.typeck_results().expr_ty(array);
|
||||
let ty = cx.typeck_results().expr_ty(array).peel_refs();
|
||||
if let Some(range) = higher::range(index) {
|
||||
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
|
||||
if let ty::Array(_, s) = ty.kind() {
|
||||
|
|
|
@ -15,7 +15,8 @@ fn main() {
|
|||
x[3]; // Ok, should not produce stderr.
|
||||
|
||||
let y = &x;
|
||||
y[0];
|
||||
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
|
||||
y[4]; // Ok, rustc will handle references too.
|
||||
|
||||
let v = vec![0; 5];
|
||||
v[0];
|
||||
|
|
|
@ -8,15 +8,7 @@ LL | x[index];
|
|||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: indexing may panic.
|
||||
--> $DIR/indexing_slicing_index.rs:18:5
|
||||
|
|
||||
LL | y[0];
|
||||
| ^^^^
|
||||
|
|
||||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: indexing may panic.
|
||||
--> $DIR/indexing_slicing_index.rs:21:5
|
||||
--> $DIR/indexing_slicing_index.rs:22:5
|
||||
|
|
||||
LL | v[0];
|
||||
| ^^^^
|
||||
|
@ -24,7 +16,7 @@ LL | v[0];
|
|||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: indexing may panic.
|
||||
--> $DIR/indexing_slicing_index.rs:22:5
|
||||
--> $DIR/indexing_slicing_index.rs:23:5
|
||||
|
|
||||
LL | v[10];
|
||||
| ^^^^^
|
||||
|
@ -32,7 +24,7 @@ LL | v[10];
|
|||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: indexing may panic.
|
||||
--> $DIR/indexing_slicing_index.rs:23:5
|
||||
--> $DIR/indexing_slicing_index.rs:24:5
|
||||
|
|
||||
LL | v[1 << 3];
|
||||
| ^^^^^^^^^
|
||||
|
@ -40,7 +32,7 @@ LL | v[1 << 3];
|
|||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: indexing may panic.
|
||||
--> $DIR/indexing_slicing_index.rs:29:5
|
||||
--> $DIR/indexing_slicing_index.rs:30:5
|
||||
|
|
||||
LL | v[N];
|
||||
| ^^^^
|
||||
|
@ -48,12 +40,12 @@ LL | v[N];
|
|||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: indexing may panic.
|
||||
--> $DIR/indexing_slicing_index.rs:30:5
|
||||
--> $DIR/indexing_slicing_index.rs:31:5
|
||||
|
|
||||
LL | v[M];
|
||||
| ^^^^
|
||||
|
|
||||
= help: Consider using `.get(n)` or `.get_mut(n)` instead
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
|
|
|
@ -71,29 +71,17 @@ LL | &x[1..][..5];
|
|||
|
|
||||
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
||||
|
||||
error: slicing may panic.
|
||||
--> $DIR/indexing_slicing_slice.rs:24:6
|
||||
|
|
||||
LL | &y[1..2];
|
||||
| ^^^^^^^
|
||||
|
|
||||
= help: Consider using `.get(n..m)` or `.get_mut(n..m)` instead
|
||||
|
||||
error: slicing may panic.
|
||||
--> $DIR/indexing_slicing_slice.rs:25:6
|
||||
error: range is out of bounds
|
||||
--> $DIR/indexing_slicing_slice.rs:25:12
|
||||
|
|
||||
LL | &y[0..=4];
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= help: Consider using `.get(n..m)` or `.get_mut(n..m)` instead
|
||||
| ^
|
||||
|
||||
error: slicing may panic.
|
||||
--> $DIR/indexing_slicing_slice.rs:26:6
|
||||
error: range is out of bounds
|
||||
--> $DIR/indexing_slicing_slice.rs:26:11
|
||||
|
|
||||
LL | &y[..=4];
|
||||
| ^^^^^^^
|
||||
|
|
||||
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
||||
| ^
|
||||
|
||||
error: slicing may panic.
|
||||
--> $DIR/indexing_slicing_slice.rs:31:6
|
||||
|
@ -133,5 +121,5 @@ LL | &v[..100];
|
|||
|
|
||||
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
|
||||
|
||||
error: aborting due to 17 previous errors
|
||||
error: aborting due to 16 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue