Skip to content

clippy::indexing_slicing does not report slicing on &str #13703

@link2xt

Description

@link2xt

Summary

indexing_slicing does not report slicing of &str and it breaks.

Lint Name

clippy::indexing_slicing

Reproducer

I tried this code:

#![warn(clippy::indexing_slicing)]

fn main() {
    let s = "foobar";
    let _ = s[..500];
    println!("Hello, world!");
}

I expected clippy to report a warning, but it does not. The code crashes when I run it:

$ cargo run
   Compiling clippy-debug v0.1.0 (/home/user/clippy-debug)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/clippy-debug`
thread 'main' panicked at src/main.rs:5:14:
byte index 500 is out of bounds of `foobar`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Version

$ rustc -Vv
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

$ cargo clippy --version
clippy 0.1.82 (f6e511e 2024-10-15)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documentationArea: Adding or improving documentationC-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions