-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have
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
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't have