Skip to content

missing_asserts_for_indexing: false positive when length is compared with const value #16237

@andjsrk

Description

@andjsrk

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

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions