rust-lang / rust-lang/rust-clippy

`indexing_slicing` should not trigger if the `get` method expects a different type

Open
#16,384 1 comment 0 reactions 1 assignee View on GitHub

@borngraced is already working on this.

Since Jan 13, 2026.

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

Description

Summary

As implemented in #12488, the indexing_slicing lint checks if there's a get method before suggesting using it. However, it doesn't check if that method can be used with the type being used as the index.

Lint Name

indexing_slicing

Reproducer

I tried this code (simplified from the linearize docs):

use linearize::{Linearize, static_map};

#[derive(Linearize)]
enum Keys {
    A,
    B,
}

fn main() {
    let map = static_map! {
        Keys::A => "a",
        Keys::B => "b",
    };
    assert_eq!(map[Keys::A], "a");
}

I saw this happen:

    = help: consider using `.get(n)` or `.get_mut(n)` instead
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#indexing_slicing

This is because StaticMap<Keys, &str> derefs to [&str], which has a get(usize) method. However, this cannot be used to index by something of type Keys, so I expected the lint not to trigger.

Version

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.