Skip to content

Commit c479b3b

Browse files
committed
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.
1 parent e63f5df commit c479b3b

File tree

3 files changed

+13
-49
lines changed

3 files changed

+13
-49
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ declare_clippy_lint! {
3434
"out of bounds constant indexing"
3535
}
3636

37-
/// **What it does:** Checks for usage of indexing or slicing. Does not report
38-
/// on arrays if we can tell that the indexing or slicing operations are in
39-
/// bounds.
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.
4040
///
4141
/// **Why is this bad?** Indexing and slicing can panic at runtime and there are
4242
/// safe alternatives.
@@ -64,13 +64,11 @@ declare_clippy_lint! {
6464
/// let y = [0, 1, 2, 3];
6565
///
6666
/// // Bad
67-
/// y[10];
6867
/// &y[10..100];
6968
/// &y[10..];
7069
/// &y[..100];
7170
///
7271
/// // Good
73-
/// y[2];
7472
/// &y[2..];
7573
/// &y[..2];
7674
/// &y[0..3];
@@ -132,20 +130,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
132130
);
133131
} else {
134132
// Catchall non-range index, i.e. [n] or [n << m]
135-
if let ty::TyArray(_, s) = ty.sty {
136-
let size: u128 = s.assert_usize(cx.tcx).unwrap().into();
133+
if let ty::TyArray(..) = ty.sty {
137134
// Index is a constant uint.
138-
if let Some((Constant::Int(const_index), _)) = constant(cx, cx.tables, index) {
139-
if size <= const_index {
140-
utils::span_lint(
141-
cx,
142-
OUT_OF_BOUNDS_INDEXING,
143-
expr.span,
144-
"const index is out of bounds",
145-
);
146-
}
147-
// Else index is in bounds, ok.
148-
135+
if let Some(..) = constant(cx, cx.tables, index) {
136+
// Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
149137
return;
150138
}
151139
}

tests/ui/indexing_slicing.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ fn main() {
1313
&x[..index];
1414
&x[index_from..index_to];
1515
&x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
16-
x[4];
17-
x[1 << 3];
16+
x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
17+
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
1818
&x[..=4];
1919
&x[1..5];
2020
&x[5..][..10]; // Two lint reports, one for [5..] and another for [..10].
@@ -44,7 +44,7 @@ fn main() {
4444
&y[..]; // Ok, should not produce stderr.
4545

4646
let empty: [i8; 0] = [];
47-
empty[0];
47+
empty[0]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
4848
&empty[1..5];
4949
&empty[0..=4];
5050
&empty[..=4];
@@ -75,7 +75,7 @@ fn main() {
7575

7676
const N: usize = 15; // Out of bounds
7777
const M: usize = 3; // In bounds
78-
x[N];
78+
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
7979
x[M]; // Ok, should not produce stderr.
8080
v[N];
8181
v[M];

tests/ui/indexing_slicing.stderr

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,13 @@ error: slicing may panic.
4747
|
4848
= help: Consider using `.get(n..)` or .get_mut(n..)` instead
4949

50-
error: const index is out of bounds
51-
--> $DIR/indexing_slicing.rs:16:5
52-
|
53-
16 | x[4];
54-
| ^^^^
55-
|
56-
= note: `-D out-of-bounds-indexing` implied by `-D warnings`
57-
58-
error: const index is out of bounds
59-
--> $DIR/indexing_slicing.rs:17:5
60-
|
61-
17 | x[1 << 3];
62-
| ^^^^^^^^^
63-
6450
error: range is out of bounds
6551
--> $DIR/indexing_slicing.rs:18:6
6652
|
6753
18 | &x[..=4];
6854
| ^^^^^^^
55+
|
56+
= note: `-D out-of-bounds-indexing` implied by `-D warnings`
6957

7058
error: range is out of bounds
7159
--> $DIR/indexing_slicing.rs:19:6
@@ -159,12 +147,6 @@ error: slicing may panic.
159147
|
160148
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
161149

162-
error: const index is out of bounds
163-
--> $DIR/indexing_slicing.rs:47:5
164-
|
165-
47 | empty[0];
166-
| ^^^^^^^^
167-
168150
error: range is out of bounds
169151
--> $DIR/indexing_slicing.rs:48:6
170152
|
@@ -269,12 +251,6 @@ error: slicing may panic.
269251
|
270252
= help: Consider using `.get(..n)`or `.get_mut(..n)` instead
271253

272-
error: const index is out of bounds
273-
--> $DIR/indexing_slicing.rs:78:5
274-
|
275-
78 | x[N];
276-
| ^^^^
277-
278254
error: indexing may panic.
279255
--> $DIR/indexing_slicing.rs:80:5
280256
|
@@ -291,5 +267,5 @@ error: indexing may panic.
291267
|
292268
= help: Consider using `.get(n)` or `.get_mut(n)` instead
293269

294-
error: aborting due to 41 previous errors
270+
error: aborting due to 37 previous errors
295271

0 commit comments

Comments
 (0)