Skip to content

Commit f45daec

Browse files
committed
fix test
1 parent 67a9413 commit f45daec

File tree

5 files changed

+119
-16
lines changed

5 files changed

+119
-16
lines changed
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 is 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 is should be linted, since `suppress-restriction-lint-in-const` default is false.
31+
const { &ARR[idx4()] }; // This is 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: 5 additions & 5 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 is 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 is 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 is should be linted, since `suppress-restriction-lint-in-const` default is false.
3030
| ^^^^^^^^^^^^^^^^^^^^^^
3131

3232
error: indexing may panic
@@ -56,7 +56,7 @@ LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint hand
5656
error: indexing may panic
5757
--> $DIR/indexing_slicing_index.rs:30:14
5858
|
59-
LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
59+
LL | const { &ARR[idx()] }; // This is should be linted, since `suppress-restriction-lint-in-const` default is false.
6060
| ^^^^^^^^^^
6161
|
6262
= help: consider using `.get(n)` or `.get_mut(n)` instead
@@ -65,7 +65,7 @@ LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
6565
error: indexing may panic
6666
--> $DIR/indexing_slicing_index.rs:31:14
6767
|
68-
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
68+
LL | const { &ARR[idx4()] }; // This is should be linted, since `suppress-restriction-lint-in-const` default is false.
6969
| ^^^^^^^^^^^
7070
|
7171
= help: consider using `.get(n)` or `.get_mut(n)` instead

0 commit comments

Comments
 (0)