rust-lang / rust-lang/rust-clippy
`missing_asserts_for_indexing`: false positive when length is compared with `const` value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
The lint warns even s.len() is compared to a const value.
Although my local environment is rustc 1.91.1, I also have tested on the playground(rustc 1.92.0) and got the same result.
Lint Name
missing_asserts_for_indexing
Reproducer
I tried this code:
#![warn(clippy::missing_asserts_for_indexing)]
const MIN_LEN: usize = 3;
pub fn test(s: &[u8]) {
assert!(s.len() >= MIN_LEN);
s[0];
s[1];
}
I saw this happen:
warning: indexing into a slice multiple times without an `assert`
--> src/main.rs:6:5
|
6 | / s[0];
7 | | s[1];
| |________^
|
= help: consider asserting the length before indexing: `assert!(s.len() > 1);`
note: slice indexed here
--> src/main.rs:6:5
|
6 | s[0];
| ^^^^
note: slice indexed here
--> src/main.rs:7:5
|
7 | s[1];
| ^^^^
= note: asserting the length before indexing will elide bounds checks
I expected to see this happen:
No warning because s has been checked with assert!(s.len() >= MIN_LEN);.
Version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-pc-windows-msvc
release: 1.91.1
LLVM version: 21.1.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the missing_asserts_for_indexing warning with the Rust snippet in the issue, then inspect the lint implementation and its existing regression tests. Add coverage for a length comparison against the MIN_LEN const and verify that the warning is no longer emitted while existing indexing cases remain covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100