Skip to content

don't trigger indexing_slicing if the vector is infallible #16673

@amab8901

Description

@amab8901

Summary

there's many cases where a vector is clearly infallible but the indexing_slicing lint still complains that it might fall. Would be nice if clippy better recognize cases that are deterministically known to be infallible

Lint Name

indexing_slicing

Reproducer

I tried this code:

fn main() {
    let vec = vec();

    dbg!(vec[1]);
}

fn vec() -> Vec<i32> {
    vec![0, 0, 0]
}

I saw this happen:

indexing may panic
consider using `.get(n)` or `.get_mut(n)` instead
for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#indexing_slicing
requested on the command line with `-W clippy::indexing-slicing

I expected to see this happen:
silence

Version

rustc 1.93.0 (254b59607 2026-01-19)
binary: rustc
commit-hash: 254b59607d4417e9dffbc307138ae5c86280fe4c
commit-date: 2026-01-19
host: x86_64-unknown-linux-gnu
release: 1.93.0
LLVM version: 21.1.8

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