Skip to content

Commit 2ce2d6b

Browse files
committed
fix a FP in indexing_slicing
treat refs to arrays the same as arrays in `indexing_slicing` and `out_of_bounds_indexing`
1 parent 06f1902 commit 2ce2d6b

File tree

4 files changed

+16
-35
lines changed

4 files changed

+16
-35
lines changed

clippy_lints/src/indexing_slicing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]
8888
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
8989
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
9090
if let ExprKind::Index(ref array, ref index) = &expr.kind {
91-
let ty = cx.typeck_results().expr_ty(array);
91+
let ty = cx.typeck_results().expr_ty(array).peel_refs();
9292
if let Some(range) = higher::range(index) {
9393
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
9494
if let ty::Array(_, s) = ty.kind() {

tests/ui/indexing_slicing_index.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ fn main() {
1515
x[3]; // Ok, should not produce stderr.
1616

1717
let y = &x;
18-
y[0];
18+
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
19+
y[4]; // Ok, rustc will handle references too.
1920

2021
let v = vec![0; 5];
2122
v[0];

tests/ui/indexing_slicing_index.stderr

+6-14
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,44 @@ LL | x[index];
88
= help: Consider using `.get(n)` or `.get_mut(n)` instead
99

1010
error: indexing may panic.
11-
--> $DIR/indexing_slicing_index.rs:18:5
12-
|
13-
LL | y[0];
14-
| ^^^^
15-
|
16-
= help: Consider using `.get(n)` or `.get_mut(n)` instead
17-
18-
error: indexing may panic.
19-
--> $DIR/indexing_slicing_index.rs:21:5
11+
--> $DIR/indexing_slicing_index.rs:22:5
2012
|
2113
LL | v[0];
2214
| ^^^^
2315
|
2416
= help: Consider using `.get(n)` or `.get_mut(n)` instead
2517

2618
error: indexing may panic.
27-
--> $DIR/indexing_slicing_index.rs:22:5
19+
--> $DIR/indexing_slicing_index.rs:23:5
2820
|
2921
LL | v[10];
3022
| ^^^^^
3123
|
3224
= help: Consider using `.get(n)` or `.get_mut(n)` instead
3325

3426
error: indexing may panic.
35-
--> $DIR/indexing_slicing_index.rs:23:5
27+
--> $DIR/indexing_slicing_index.rs:24:5
3628
|
3729
LL | v[1 << 3];
3830
| ^^^^^^^^^
3931
|
4032
= help: Consider using `.get(n)` or `.get_mut(n)` instead
4133

4234
error: indexing may panic.
43-
--> $DIR/indexing_slicing_index.rs:29:5
35+
--> $DIR/indexing_slicing_index.rs:30:5
4436
|
4537
LL | v[N];
4638
| ^^^^
4739
|
4840
= help: Consider using `.get(n)` or `.get_mut(n)` instead
4941

5042
error: indexing may panic.
51-
--> $DIR/indexing_slicing_index.rs:30:5
43+
--> $DIR/indexing_slicing_index.rs:31:5
5244
|
5345
LL | v[M];
5446
| ^^^^
5547
|
5648
= help: Consider using `.get(n)` or `.get_mut(n)` instead
5749

58-
error: aborting due to 7 previous errors
50+
error: aborting due to 6 previous errors
5951

tests/ui/indexing_slicing_slice.stderr

+7-19
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,17 @@ LL | &x[1..][..5];
7171
|
7272
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
7373

74-
error: slicing may panic.
75-
--> $DIR/indexing_slicing_slice.rs:24:6
76-
|
77-
LL | &y[1..2];
78-
| ^^^^^^^
79-
|
80-
= help: Consider using `.get(n..m)` or `.get_mut(n..m)` instead
81-
82-
error: slicing may panic.
83-
--> $DIR/indexing_slicing_slice.rs:25:6
74+
error: range is out of bounds
75+
--> $DIR/indexing_slicing_slice.rs:25:12
8476
|
8577
LL | &y[0..=4];
86-
| ^^^^^^^^
87-
|
88-
= help: Consider using `.get(n..m)` or `.get_mut(n..m)` instead
78+
| ^
8979

90-
error: slicing may panic.
91-
--> $DIR/indexing_slicing_slice.rs:26:6
80+
error: range is out of bounds
81+
--> $DIR/indexing_slicing_slice.rs:26:11
9282
|
9383
LL | &y[..=4];
94-
| ^^^^^^^
95-
|
96-
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
84+
| ^
9785

9886
error: slicing may panic.
9987
--> $DIR/indexing_slicing_slice.rs:31:6
@@ -133,5 +121,5 @@ LL | &v[..100];
133121
|
134122
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
135123

136-
error: aborting due to 17 previous errors
124+
error: aborting due to 16 previous errors
137125

0 commit comments

Comments
 (0)