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
10.89k stars 1.46k forks source link

cast-lossless suggests calling macro in itself #12695

Open matthiaskrgr opened 3 weeks ago

matthiaskrgr commented 3 weeks ago

Summary

.

Lint Name

cast_lossless

Reproducer

I tried this code:

#![warn(clippy::cast_lossless)]
//@ build-pass
#![crate_type = "lib"]
#![allow(arithmetic_overflow)]

pub trait BitSplit {
    type Half;
    fn merge(halves: [Self::Half; 2]) -> Self;
}

macro_rules! impl_ints {
    ($int:ty => $half:ty; $mask:expr) => {
        impl BitSplit for $int {
            type Half = $half;
            #[inline]
            fn merge(halves: [Self::Half; 2]) -> Self {
                const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
                (halves[0] << HALF_SIZE) as $int | halves[1] as $int
            }
        }
    };
}

impl_ints!(u128 => u64; 0x0000_0000_0000_0000_FFFF_FFFF_FFFF_FFFF);
impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF);
impl_ints!( u32 => u16;                               0x0000_FFFF);
impl_ints!( u16 =>  u8;                                    0x00FF);

I saw this happen:

warning: casting `u32` to `u64` may become silently lossy if you later change the type
  --> ./tests/ui/const_prop/ice-issue-96944.rs:18:17
   |
18 |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u64::from(impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF))`
...
25 | impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF);
   | ------------------------------------------------------------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
note: the lint level is defined here
  --> ./tests/ui/const_prop/ice-issue-96944.rs:1:9
   |
1  | #![warn(clippy::cast_lossless)]
   |         ^^^^^^^^^^^^^^^^^^^^^
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: casting `u32` to `u64` may become silently lossy if you later change the type
  --> ./tests/ui/const_prop/ice-issue-96944.rs:18:52
   |
18 |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |                                                    ^^^^^^^^^ help: try: `u64::from(impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF))`
...
25 | impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF);
   | ------------------------------------------------------------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: casting `u16` to `u32` may become silently lossy if you later change the type
  --> ./tests/ui/const_prop/ice-issue-96944.rs:18:17
   |
18 |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::from(impl_ints!( u32 => u16;                               0x0000_FFFF))`
...
26 | impl_ints!( u32 => u16;                               0x0000_FFFF);
   | ------------------------------------------------------------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: casting `u16` to `u32` may become silently lossy if you later change the type
  --> ./tests/ui/const_prop/ice-issue-96944.rs:18:52
   |
18 |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |                                                    ^^^^^^^^^ help: try: `u32::from(impl_ints!( u32 => u16;                               0x0000_FFFF))`
...
26 | impl_ints!( u32 => u16;                               0x0000_FFFF);
   | ------------------------------------------------------------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: casting `u8` to `u16` may become silently lossy if you later change the type
  --> ./tests/ui/const_prop/ice-issue-96944.rs:18:17
   |
18 |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::from(impl_ints!( u16 =>  u8;                                    0x00FF))`
...
27 | impl_ints!( u16 =>  u8;                                    0x00FF);
   | ------------------------------------------------------------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: casting `u8` to `u16` may become silently lossy if you later change the type
  --> ./tests/ui/const_prop/ice-issue-96944.rs:18:52
   |
18 |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |                                                    ^^^^^^^^^ help: try: `u16::from(impl_ints!( u16 =>  u8;                                    0x00FF))`
...
27 | impl_ints!( u16 =>  u8;                                    0x00FF);
   | ------------------------------------------------------------------ in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 6 warnings emitted

I expected to see this happen:

Version

rustc 1.79.0-nightly (07d0d7ce3 2024-04-19)
binary: rustc
commit-hash: 07d0d7ce3fd22e4fadd61206034af6fadcdb3e4f
commit-date: 2024-04-19
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4

Additional Labels

No response