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.03k stars 1.48k forks source link

`cast_lossless` does not effect on some cases #12991

Open CrazyboyQCD opened 1 week ago

CrazyboyQCD commented 1 week ago

Summary

cast_lossless does not effect on conversion of 8 or 16 bit integer to isize/usize.

Lint Name

cast_lossless

Reproducer

I tried this code:

fn main() {
    let x = 1u8;
    let y = 1i16;
    let a = x as usize;
    let b = y as isize;
    println!("{} {}", a, b);
}

cargo clippy --fix -- -D clippy::cast_lossless I expected to see this happen: Convert the code into:

fn main() {
    let x = 1u8;
    let y = 1i16;
    let a = usize::from(x);
    let b = isize::from(y);
    println!("{} {}", a, b);
}

Instead, this happened: Nothing changed.

Version

rustc 1.81.0-nightly (bcf94dec5 2024-06-23)
binary: rustc
commit-hash: bcf94dec5ba6838e435902120c0384c360126a26
commit-date: 2024-06-23
host: x86_64-pc-windows-msvc
release: 1.81.0-nightly
LLVM version: 18.1.7
CrazyboyQCD commented 1 week ago

[_ as usize] is a common statement and [usize::from(_)] is noisy with brackets sticked together, so it would be better to leave [_ as usize] unchanged.