Skip to content

Commit 536c255

Browse files
committed
Auto merge of #5130 - JohnTitor:split-up-index-slice, r=flip1995
Split up `indexing_slicing` ui test Closes #2038 Now all the stderrs are less than 200 lines 🎉 changelog: none
2 parents a395894 + 49934e7 commit 536c255

5 files changed

+130
-117
lines changed

clippy_dev/src/stderr_length_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::prelude::*;
77
// The maximum length allowed for stderr files.
88
//
99
// We limit this because small files are easier to deal with than bigger files.
10-
const LIMIT: usize = 220;
10+
const LIMIT: usize = 200;
1111

1212
pub fn check() {
1313
let stderr_files = stderr_files();

tests/ui/indexing_slicing_index.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![warn(clippy::indexing_slicing)]
2+
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
3+
// we want to avoid false positives.
4+
#![warn(clippy::out_of_bounds_indexing)]
5+
#![allow(clippy::no_effect, clippy::unnecessary_operation)]
6+
7+
fn main() {
8+
let x = [1, 2, 3, 4];
9+
let index: usize = 1;
10+
x[index];
11+
x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
12+
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
13+
14+
x[0]; // Ok, should not produce stderr.
15+
x[3]; // Ok, should not produce stderr.
16+
17+
let y = &x;
18+
y[0];
19+
20+
let v = vec![0; 5];
21+
v[0];
22+
v[10];
23+
v[1 << 3];
24+
25+
const N: usize = 15; // Out of bounds
26+
const M: usize = 3; // In bounds
27+
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
28+
x[M]; // Ok, should not produce stderr.
29+
v[N];
30+
v[M];
31+
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
error: index out of bounds: the len is 4 but the index is 4
2+
--> $DIR/indexing_slicing_index.rs:11:5
3+
|
4+
LL | x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
5+
| ^^^^
6+
|
7+
= note: `#[deny(const_err)]` on by default
8+
9+
error: index out of bounds: the len is 4 but the index is 8
10+
--> $DIR/indexing_slicing_index.rs:12:5
11+
|
12+
LL | x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
13+
| ^^^^^^^^^
14+
15+
error: index out of bounds: the len is 4 but the index is 15
16+
--> $DIR/indexing_slicing_index.rs:27:5
17+
|
18+
LL | x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
19+
| ^^^^
20+
21+
error: indexing may panic.
22+
--> $DIR/indexing_slicing_index.rs:10:5
23+
|
24+
LL | x[index];
25+
| ^^^^^^^^
26+
|
27+
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
28+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
29+
30+
error: indexing may panic.
31+
--> $DIR/indexing_slicing_index.rs:18:5
32+
|
33+
LL | y[0];
34+
| ^^^^
35+
|
36+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
37+
38+
error: indexing may panic.
39+
--> $DIR/indexing_slicing_index.rs:21:5
40+
|
41+
LL | v[0];
42+
| ^^^^
43+
|
44+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
45+
46+
error: indexing may panic.
47+
--> $DIR/indexing_slicing_index.rs:22:5
48+
|
49+
LL | v[10];
50+
| ^^^^^
51+
|
52+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
53+
54+
error: indexing may panic.
55+
--> $DIR/indexing_slicing_index.rs:23:5
56+
|
57+
LL | v[1 << 3];
58+
| ^^^^^^^^^
59+
|
60+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
61+
62+
error: indexing may panic.
63+
--> $DIR/indexing_slicing_index.rs:29:5
64+
|
65+
LL | v[N];
66+
| ^^^^
67+
|
68+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
69+
70+
error: indexing may panic.
71+
--> $DIR/indexing_slicing_index.rs:30:5
72+
|
73+
LL | v[M];
74+
| ^^^^
75+
|
76+
= help: Consider using `.get(n)` or `.get_mut(n)` instead
77+
78+
error: aborting due to 10 previous errors
79+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(plugin)]
21
#![warn(clippy::indexing_slicing)]
32
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
43
// we want to avoid false positives.
@@ -10,49 +9,29 @@ fn main() {
109
let index: usize = 1;
1110
let index_from: usize = 2;
1211
let index_to: usize = 3;
13-
x[index];
1412
&x[index..];
1513
&x[..index];
1614
&x[index_from..index_to];
1715
&x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
18-
x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
19-
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
2016
&x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
2117
&x[0..][..3];
2218
&x[1..][..5];
2319

2420
&x[0..].get(..3); // Ok, should not produce stderr.
25-
x[0]; // Ok, should not produce stderr.
26-
x[3]; // Ok, should not produce stderr.
2721
&x[0..3]; // Ok, should not produce stderr.
2822

2923
let y = &x;
30-
y[0];
3124
&y[1..2];
3225
&y[0..=4];
3326
&y[..=4];
3427

3528
&y[..]; // Ok, should not produce stderr.
3629

3730
let v = vec![0; 5];
38-
v[0];
39-
v[10];
40-
v[1 << 3];
4131
&v[10..100];
4232
&x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
4333
&v[10..];
4434
&v[..100];
4535

4636
&v[..]; // Ok, should not produce stderr.
47-
48-
//
49-
// Continue tests at end function to minimize the changes to this file's corresponding stderr.
50-
//
51-
52-
const N: usize = 15; // Out of bounds
53-
const M: usize = 3; // In bounds
54-
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
55-
x[M]; // Ok, should not produce stderr.
56-
v[N];
57-
v[M];
5837
}

0 commit comments

Comments
 (0)