rust-lang / rust-lang/rust-clippy

`missing_asserts_for_indexing` ignores `match` for length

Open
#17,398 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

lint ignores context of match slice.len()

Lint Name

missing_asserts_for_indexing

Reproducer

I tried this code:

// supported: &[u8] is a function parameter, value from enum variant or provided in any other way
// to compile this code u8 can be replaced with anything implementing Display, or Debug (in that case add `:?` to format string)

match supported.len() {
    0 => {}
    1 => println!("{}", supported[0]),
    _ => println!("{} or {}", supported[0], supported[1]),
}

I saw this happen:

supported[0]
supported[0], supported[1]

 indexing into a slice multiple times without an `assert`

I expected to see this happen:
as `usize is not negative and value 0 was explicitly checked, lint should never fire.

Version
rustc 1.96.0 (ac68faa20 2026-05-25)
binary: rustc
commit-hash: ac68faa20c58cbccd01ee7208bf3b6e93a7d7f96
commit-date: 2026-05-25
host: aarch64-apple-darwin
release: 1.96.0
LLVM version: 22.1.2
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation and tests for the missing_asserts_for_indexing lint, then run the Rust reproducer from the issue. Trace how the match on supported.len() is handled and add coverage showing that the indexed accesses are accepted when their lengths are matched; done means the reproducer emits no diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.