mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
fix test
This commit is contained in:
parent
67a94135cb
commit
eec5039f09
@ -166,7 +166,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
|||||||
// Catchall non-range index, i.e., [n] or [n << m]
|
// Catchall non-range index, i.e., [n] or [n << m]
|
||||||
if let ty::Array(..) = ty.kind() {
|
if let ty::Array(..) = ty.kind() {
|
||||||
// Index is a const block.
|
// Index is a const block.
|
||||||
if self.suppress_restriction_lint_in_const && let ExprKind::ConstBlock(..) = index.kind {
|
if let ExprKind::ConstBlock(..) = index.kind {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Index is a constant uint.
|
// Index is a constant uint.
|
||||||
|
@ -1 +1 @@
|
|||||||
suppress-restriction-lint-in-const = false
|
suppress-restriction-lint-in-const = true
|
||||||
|
@ -1,4 +1,51 @@
|
|||||||
|
#![feature(inline_const)]
|
||||||
#![warn(clippy::indexing_slicing)]
|
#![warn(clippy::indexing_slicing)]
|
||||||
|
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
|
||||||
|
// we want to avoid false positives.
|
||||||
|
#![warn(clippy::out_of_bounds_indexing)]
|
||||||
|
#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)]
|
||||||
|
|
||||||
|
const ARR: [i32; 2] = [1, 2];
|
||||||
|
const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
|
||||||
|
const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
|
||||||
|
|
||||||
|
const fn idx() -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
const fn idx4() -> usize {
|
||||||
|
4
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = [1, 2, 3, 4];
|
||||||
|
let index: usize = 1;
|
||||||
|
x[index];
|
||||||
|
x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
||||||
|
x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
||||||
|
|
||||||
|
x[0]; // Ok, should not produce stderr.
|
||||||
|
x[3]; // Ok, should not produce stderr.
|
||||||
|
x[const { idx() }]; // Ok, should not produce stderr.
|
||||||
|
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
||||||
|
const { &ARR[idx()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
|
||||||
|
const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
|
||||||
|
|
||||||
|
let y = &x;
|
||||||
|
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
|
||||||
|
y[4]; // Ok, rustc will handle references too.
|
||||||
|
|
||||||
|
let v = vec![0; 5];
|
||||||
|
v[0];
|
||||||
|
v[10];
|
||||||
|
v[1 << 3];
|
||||||
|
|
||||||
|
const N: usize = 15; // Out of bounds
|
||||||
|
const M: usize = 3; // In bounds
|
||||||
|
x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
||||||
|
x[M]; // Ok, should not produce stderr.
|
||||||
|
v[N];
|
||||||
|
v[M];
|
||||||
|
}
|
||||||
|
|
||||||
/// An opaque integer representation
|
/// An opaque integer representation
|
||||||
pub struct Integer<'a> {
|
pub struct Integer<'a> {
|
||||||
@ -11,5 +58,3 @@ impl<'a> Integer<'a> {
|
|||||||
self.value[0] & 0b1000_0000 != 0
|
self.value[0] & 0b1000_0000 != 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
|
||||||
|
@ -1,12 +1,70 @@
|
|||||||
error: indexing may panic
|
error[E0080]: evaluation of `main::{constant#3}` failed
|
||||||
--> $DIR/test.rs:11:9
|
--> $DIR/test.rs:31:14
|
||||||
|
|
|
|
||||||
LL | self.value[0] & 0b1000_0000 != 0
|
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
|
||||||
| ^^^^^^^^^^^^^
|
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
|
||||||
|
|
||||||
|
note: erroneous constant used
|
||||||
|
--> $DIR/test.rs:31:5
|
||||||
|
|
|
||||||
|
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: indexing may panic
|
||||||
|
--> $DIR/test.rs:22:5
|
||||||
|
|
|
||||||
|
LL | x[index];
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
= note: the suggestion might not be applicable in constant blocks
|
|
||||||
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
|
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: indexing may panic
|
||||||
|
--> $DIR/test.rs:38:5
|
||||||
|
|
|
||||||
|
LL | v[0];
|
||||||
|
| ^^^^
|
||||||
|
|
|
||||||
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
|
error: indexing may panic
|
||||||
|
--> $DIR/test.rs:39:5
|
||||||
|
|
|
||||||
|
LL | v[10];
|
||||||
|
| ^^^^^
|
||||||
|
|
|
||||||
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
|
error: indexing may panic
|
||||||
|
--> $DIR/test.rs:40:5
|
||||||
|
|
|
||||||
|
LL | v[1 << 3];
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
|
||||||
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
|
error: indexing may panic
|
||||||
|
--> $DIR/test.rs:46:5
|
||||||
|
|
|
||||||
|
LL | v[N];
|
||||||
|
| ^^^^
|
||||||
|
|
|
||||||
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
|
error: indexing may panic
|
||||||
|
--> $DIR/test.rs:47:5
|
||||||
|
|
|
||||||
|
LL | v[M];
|
||||||
|
| ^^^^
|
||||||
|
|
|
||||||
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
|
error[E0080]: evaluation of constant value failed
|
||||||
|
--> $DIR/test.rs:10:24
|
||||||
|
|
|
||||||
|
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
|
||||||
|
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
|
||||||
|
|
||||||
|
error: aborting due to 8 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0080`.
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)]
|
#![allow(unconditional_panic, clippy::no_effect, clippy::unnecessary_operation)]
|
||||||
|
|
||||||
const ARR: [i32; 2] = [1, 2];
|
const ARR: [i32; 2] = [1, 2];
|
||||||
const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
|
const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
|
const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
|
||||||
|
|
||||||
const fn idx() -> usize {
|
const fn idx() -> usize {
|
||||||
@ -27,8 +27,8 @@ fn main() {
|
|||||||
x[3]; // Ok, should not produce stderr.
|
x[3]; // Ok, should not produce stderr.
|
||||||
x[const { idx() }]; // Ok, should not produce stderr.
|
x[const { idx() }]; // Ok, should not produce stderr.
|
||||||
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
||||||
const { &ARR[idx()] }; // Ok, should not produce stderr.
|
const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
|
const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
|
|
||||||
let y = &x;
|
let y = &x;
|
||||||
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
|
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
error: indexing may panic
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:9:20
|
--> $DIR/indexing_slicing_index.rs:9:20
|
||||||
|
|
|
|
||||||
LL | const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
|
LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
= 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.
|
|||||||
error[E0080]: evaluation of `main::{constant#3}` failed
|
error[E0080]: evaluation of `main::{constant#3}` failed
|
||||||
--> $DIR/indexing_slicing_index.rs:31:14
|
--> $DIR/indexing_slicing_index.rs:31:14
|
||||||
|
|
|
|
||||||
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
|
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
|
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
|
||||||
|
|
||||||
note: erroneous constant used
|
note: erroneous constant used
|
||||||
--> $DIR/indexing_slicing_index.rs:31:5
|
--> $DIR/indexing_slicing_index.rs:31:5
|
||||||
|
|
|
|
||||||
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
|
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: indexing may panic
|
error: indexing may panic
|
||||||
@ -37,26 +37,10 @@ LL | x[index];
|
|||||||
|
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
|
|
||||||
error: indexing may panic
|
|
||||||
--> $DIR/indexing_slicing_index.rs:28:5
|
|
||||||
|
|
|
||||||
LL | x[const { idx() }]; // Ok, should not produce stderr.
|
|
||||||
| ^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
|
||||||
|
|
||||||
error: indexing may panic
|
|
||||||
--> $DIR/indexing_slicing_index.rs:29:5
|
|
||||||
|
|
|
||||||
LL | x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
|
||||||
|
|
||||||
error: indexing may panic
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:30:14
|
--> $DIR/indexing_slicing_index.rs:30:14
|
||||||
|
|
|
|
||||||
LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
|
LL | const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
@ -65,7 +49,7 @@ LL | const { &ARR[idx()] }; // Ok, should not produce stderr.
|
|||||||
error: indexing may panic
|
error: indexing may panic
|
||||||
--> $DIR/indexing_slicing_index.rs:31:14
|
--> $DIR/indexing_slicing_index.rs:31:14
|
||||||
|
|
|
|
||||||
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
|
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
|
||||||
| ^^^^^^^^^^^
|
| ^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
|
||||||
@ -117,6 +101,6 @@ error[E0080]: evaluation of constant value failed
|
|||||||
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
|
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
|
||||||
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
|
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
|
||||||
|
|
||||||
error: aborting due to 14 previous errors
|
error: aborting due to 12 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0080`.
|
For more information about this error, try `rustc --explain E0080`.
|
||||||
|
Loading…
Reference in New Issue
Block a user