rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.27k stars 1.52k forks source link

indexing_slicing not warning about slicing when the type is a generic array returned from a generic function #10215

Open nabilwadih opened 1 year ago

nabilwadih commented 1 year ago

Summary

Sample Code: go to https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f52e65454f24681df967fd173371e07b and run clippy.

#![warn(clippy::indexing_slicing)]

fn foo<const X: usize, Y: Fn(&[u8]) -> [u8; X]>(a: &mut [u8], y: Y) {
    let b = y(a);
    a[0..].copy_from_slice(b[0..]);
}

clippy will warn for the a[0..] slice , but not for the &b[0..] slice

Lint Name

indexing_slicing

Reproducer

Run clippy on the code from this sample: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f52e65454f24681df967fd173371e07b

#![warn(clippy::indexing_slicing)]

fn foo<const X: usize, Y: Fn(&[u8]) -> [u8; X]>(a: &mut [u8], y: Y) {
    let b = y(a);
    a[0..].copy_from_slice(b[0..]);
}

I expected to see this happen: clippy should be able to detect the second slice on b and warn about it

Instead, this happened: clippy does not warn about the second slice on b

Version

Clippy version: 0.1.68 (2023-01-18 333ee6c)
Jarcho commented 1 year ago

x[0..] not linting on arrays is intentional. Really this shouldn't lint for slices either as it can never panic.

This should, however, probably be changed to lint on x[1..] for arrays of a generic size.