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 displays error when indexing reference to array #6021

Closed
Zenithsiz opened this issue Sep 9, 2020 · 0 comments · Fixed by #6034
Closed

indexing_slicing displays error when indexing reference to array #6021

Zenithsiz opened this issue Sep 9, 2020 · 0 comments · Fixed by #6034
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience.

Comments

@Zenithsiz
Copy link

I tried this code:

#![warn(clippy::indexing_slicing)]

pub fn f(arr: &[u32; 2]) {
    let _a = arr[0];
}

Playground

I expected to see this happen:

No warning displayed, as arr[0] is known to exist, because arr has 2 elements.

Instead, this happened:

I got the warning:

warning: indexing may panic.
 --> src/lib.rs:4:14
  |
4 |     let _a = arr[0];
  |              ^^^^^^
  |
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::indexing_slicing)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^
  = help: Consider using `.get(n)` or `.get_mut(n)` instead
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing

At first I thought that perhaps arr[0] would use <[u8]>::index and so it could panic and clippy was correct, but when building the assembly, even in debug mode, there is no panic code, and using an invalid index triggers a rustc error about the operation always panicking at runtime, so I believe clippy should also detect when this is valid.

Meta

  • cargo clippy -V: e.g. clippy 0.0.212 (2020-09-08 5099914)

As this is on the playground, I am unable to run rustc -Vv or provide a backtrace.

@Zenithsiz Zenithsiz added the C-bug Category: Clippy is not doing the correct thing label Sep 9, 2020
@flip1995 flip1995 added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Sep 10, 2020
@bors bors closed this as completed in 5af88e3 Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages E-medium Call for participation: Medium difficulty level problem and requires some initial experience.
Projects
None yet
2 participants