Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexing_slicing should not fire if a valid array index comes from a constant function that is evaluated at compile-time #8588

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clippy_lints/src/indexing_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ declare_lint_pass!(IndexingSlicing => [INDEXING_SLICING, OUT_OF_BOUNDS_INDEXING]

impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
return;
}

if let ExprKind::Index(array, index) = &expr.kind {
let ty = cx.typeck_results().expr_ty(array).peel_refs();
if let Some(range) = higher::Range::hir(index) {
Expand Down Expand Up @@ -151,6 +155,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
} else {
// Catchall non-range index, i.e., [n] or [n << m]
if let ty::Array(..) = ty.kind() {
// Index is a const block.
if let ExprKind::ConstBlock(..) = index.kind {
return;
}
// Index is a constant uint.
if let Some(..) = constant(cx, cx.typeck_results(), index) {
// Let rustc's `const_err` lint handle constant `usize` indexing on arrays.
Expand Down
24 changes: 20 additions & 4 deletions tests/ui/indexing_slicing_index.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
#![feature(inline_const)]
#![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(clippy::no_effect, clippy::unnecessary_operation)]
#![allow(const_err, clippy::no_effect, clippy::unnecessary_operation)]

const ARR: [i32; 2] = [1, 2];
const REF: &i32 = &ARR[idx()]; // Ok, should not produce stderr.
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 `const_err` lint handle `usize` indexing on arrays.
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
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.
const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.

let y = &x;
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
Expand All @@ -25,7 +41,7 @@ fn main() {

const N: usize = 15; // Out of bounds
const M: usize = 3; // In bounds
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
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];
Expand Down
27 changes: 20 additions & 7 deletions tests/ui/indexing_slicing_index.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
error[E0080]: evaluation of `main::{constant#3}::<&i32>` failed
--> $DIR/indexing_slicing_index.rs:31:14
|
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4

error[E0080]: erroneous constant used
--> $DIR/indexing_slicing_index.rs:31:5
|
LL | const { &ARR[idx4()] }; // Ok, let rustc handle const contexts.
| ^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors

error: indexing may panic
--> $DIR/indexing_slicing_index.rs:10:5
--> $DIR/indexing_slicing_index.rs:22:5
|
LL | x[index];
| ^^^^^^^^
Expand All @@ -8,44 +20,45 @@ LL | x[index];
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> $DIR/indexing_slicing_index.rs:22:5
--> $DIR/indexing_slicing_index.rs:38:5
|
LL | v[0];
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> $DIR/indexing_slicing_index.rs:23:5
--> $DIR/indexing_slicing_index.rs:39:5
|
LL | v[10];
| ^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> $DIR/indexing_slicing_index.rs:24:5
--> $DIR/indexing_slicing_index.rs:40:5
|
LL | v[1 << 3];
| ^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> $DIR/indexing_slicing_index.rs:30:5
--> $DIR/indexing_slicing_index.rs:46:5
|
LL | v[N];
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic
--> $DIR/indexing_slicing_index.rs:31:5
--> $DIR/indexing_slicing_index.rs:47:5
|
LL | v[M];
| ^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead

error: aborting due to 6 previous errors
error: aborting due to 8 previous errors

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