Skip to content

Commit eec5039

Browse files
committed
fix test
1 parent 67a9413 commit eec5039

File tree

6 files changed

+121
-34
lines changed

6 files changed

+121
-34
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
166166
// Catchall non-range index, i.e., [n] or [n << m]
167167
if let ty::Array(..) = ty.kind() {
168168
// Index is a const block.
169-
if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
169+
if let ExprKind::ConstBlock(..) = index.kind {
170170
return;
171171
}
172172
// Index is a constant uint.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
suppress-restriction-lint-in-const = false
1+
suppress-restriction-lint-in-const = true

tests/ui-toml/suppress_lint_in_const/test.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1+
#![feature(inline_const)]
12
#![warn(clippy::indexing_slicing)]
3+
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
4+
// we want to avoid false positives.
5+
#![warn(clippy::out_of_bounds_indexing)]
6+
#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)]
7+
8+
const ARR: [i32; 2] = [1, 2];
9+
const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
10+
const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
11+
12+
const fn idx() -> usize {
13+
1
14+
}
15+
const fn idx4() -> usize {
16+
4
17+
}
18+
19+
fn main() {
20+
let x = [1, 2, 3, 4];
21+
let index: usize = 1;
22+
x[index];
23+
x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
24+
x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
25+
26+
x[0]; // Ok, should not produce stderr.
27+
x[3]; // Ok, should not produce stderr.
28+
x[const { idx() }]; // Ok, should not produce stderr.
29+
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
30+
const { &ARR[idx()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
31+
const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
32+
33+
let y = &x;
34+
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
35+
y[4]; // Ok, rustc will handle references too.
36+
37+
let v = vec![0; 5];
38+
v[0];
39+
v[10];
40+
v[1 << 3];
41+
42+
const N: usize = 15; // Out of bounds
43+
const M: usize = 3; // In bounds
44+
x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
45+
x[M]; // Ok, should not produce stderr.
46+
v[N];
47+
v[M];
48+
}
249

350
/// An opaque integer representation
451
pub struct Integer<'a> {
@@ -11,5 +58,3 @@ impl<'a> Integer<'a> {
1158
self.value[0] & 0b1000_0000 != 0
1259
}
1360
}
14-
15-
fn main() {}
Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,70 @@
1+
error[E0080]: evaluation of `main::{constant#3}` failed
2+
--> $DIR/test.rs:31:14
3+
|
4+
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
5+
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
6+
7+
note: erroneous constant used
8+
--> $DIR/test.rs:31:5
9+
|
10+
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
113
error: indexing may panic
2-
--> $DIR/test.rs:11:9
14+
--> $DIR/test.rs:22:5
315
|
4-
LL | self.value[0] & 0b1000_0000 != 0
5-
| ^^^^^^^^^^^^^
16+
LL | x[index];
17+
| ^^^^^^^^
618
|
719
= help: consider using `.get(n)` or `.get_mut(n)` instead
8-
= note: the suggestion might not be applicable in constant blocks
920
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
1021

11-
error: aborting due to previous error
22+
error: indexing may panic
23+
--> $DIR/test.rs:38:5
24+
|
25+
LL | v[0];
26+
| ^^^^
27+
|
28+
= help: consider using `.get(n)` or `.get_mut(n)` instead
29+
30+
error: indexing may panic
31+
--> $DIR/test.rs:39:5
32+
|
33+
LL | v[10];
34+
| ^^^^^
35+
|
36+
= help: consider using `.get(n)` or `.get_mut(n)` instead
37+
38+
error: indexing may panic
39+
--> $DIR/test.rs:40:5
40+
|
41+
LL | v[1 << 3];
42+
| ^^^^^^^^^
43+
|
44+
= help: consider using `.get(n)` or `.get_mut(n)` instead
45+
46+
error: indexing may panic
47+
--> $DIR/test.rs:46:5
48+
|
49+
LL | v[N];
50+
| ^^^^
51+
|
52+
= help: consider using `.get(n)` or `.get_mut(n)` instead
53+
54+
error: indexing may panic
55+
--> $DIR/test.rs:47:5
56+
|
57+
LL | v[M];
58+
| ^^^^
59+
|
60+
= help: consider using `.get(n)` or `.get_mut(n)` instead
61+
62+
error[E0080]: evaluation of constant value failed
63+
--> $DIR/test.rs:10:24
64+
|
65+
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
66+
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
67+
68+
error: aborting due to 8 previous errors
1269

70+
For more information about this error, try `rustc --explain E0080`.

tests/ui/indexing_slicing_index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)]
77

88
const ARR: [i32; 2] = [1, 2];
9-
const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
9+
const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
1010
const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
1111

1212
const fn idx() -> usize {
@@ -27,8 +27,8 @@ fn main() {
2727
x[3]; // Ok, should not produce stderr.
2828
x[const { idx() }]; // Ok, should not produce stderr.
2929
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
30-
const { &ARR[idx()] }; // Ok, should not produce stderr.
31-
const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
30+
const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
31+
const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
3232

3333
let y = &x;
3434
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021

tests/ui/indexing_slicing_index.stderr

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: indexing may panic
22
--> $DIR/indexing_slicing_index.rs:9:20
33
|
4-
LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
4+
LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
55
| ^^^^^^^^^^
66
|
77
= help: consider using `.get(n)` or `.get_mut(n)` instead
@@ -20,13 +20,13 @@ LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
2020
error[E0080]: evaluation of `main::{constant#3}` failed
2121
--> $DIR/indexing_slicing_index.rs:31:14
2222
|
23-
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
23+
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
2424
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
2525

2626
note: erroneous constant used
2727
--> $DIR/indexing_slicing_index.rs:31:5
2828
|
29-
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
29+
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
3030
| ^^^^^^^^^^^^^^^^^^^^^^
3131

3232
error: indexing may panic
@@ -37,26 +37,10 @@ LL | x[index];
3737
|
3838
= help: consider using `.get(n)` or `.get_mut(n)` instead
3939

40-
error: indexing may panic
41-
--> $DIR/indexing_slicing_index.rs:28:5
42-
|
43-
LL | x[const { idx() }]; // Ok, should not produce stderr.
44-
| ^^^^^^^^^^^^^^^^^^
45-
|
46-
= help: consider using `.get(n)` or `.get_mut(n)` instead
47-
48-
error: indexing may panic
49-
--> $DIR/indexing_slicing_index.rs:29:5
50-
|
51-
LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
52-
| ^^^^^^^^^^^^^^^^^^^
53-
|
54-
= help: consider using `.get(n)` or `.get_mut(n)` instead
55-
5640
error: indexing may panic
5741
--> $DIR/indexing_slicing_index.rs:30:14
5842
|
59-
LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
43+
LL | const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
6044
| ^^^^^^^^^^
6145
|
6246
= help: consider using `.get(n)` or `.get_mut(n)` instead
@@ -65,7 +49,7 @@ LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
6549
error: indexing may panic
6650
--> $DIR/indexing_slicing_index.rs:31:14
6751
|
68-
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
52+
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
6953
| ^^^^^^^^^^^
7054
|
7155
= help: consider using `.get(n)` or `.get_mut(n)` instead
@@ -117,6 +101,6 @@ error[E0080]: evaluation of constant value failed
117101
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
118102
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
119103

120-
error: aborting due to 14 previous errors
104+
error: aborting due to 12 previous errors
121105

122106
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)