Skip to content

cast-lossless breaks macro #15348

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::cast-lossless

this code:

//@ 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);

caused the following diagnostics:

    Checking _ice-issue-96944 v0.1.0 (/tmp/icemaker_global_tempdir.0gE5D6KlClUx/icemaker_clippyfix_tempdir.ArOqbDGioPwO/_ice-issue-96944)
warning: casts from `u64` to `u128` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |________________________________________^
...
23 |   impl_ints!(u128 => u64; 0x0000_0000_0000_0000_FFFF_FFFF_FFFF_FFFF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
   = note: requested on the command line with `--force-warn clippy::cast-lossless`
   = note: this warning originates in the macro `impl_ints` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `<u128>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u128>::from(halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |

warning: casts from `u64` to `u128` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |____________________________________________________________^
...
23 |   impl_ints!(u128 => u64; 0x0000_0000_0000_0000_FFFF_FFFF_FFFF_FFFF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u128>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u128>::from(halves[1]) as $int
   |

warning: casts from `u32` to `u64` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |________________________________________^
...
24 |   impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u64>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u64>::from(halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |

warning: casts from `u32` to `u64` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |____________________________________________________________^
...
24 |   impl_ints!( u64 => u32;                     0x0000_0000_FFFF_FFFF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u64>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u64>::from(halves[1]) as $int
   |

warning: casts from `u16` to `u32` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |________________________________________^
...
25 |   impl_ints!( u32 => u16;                               0x0000_FFFF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u32>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u32>::from(halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |

warning: casts from `u16` to `u32` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |____________________________________________________________^
...
25 |   impl_ints!( u32 => u16;                               0x0000_FFFF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u32>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u32>::from(halves[1]) as $int
   |

warning: casts from `u8` to `u16` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |________________________________________^
...
26 |   impl_ints!( u16 =>  u8;                                    0x00FF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u16>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u16>::from(halves[0] << HALF_SIZE) as $int | halves[1] as $int
   |

warning: casts from `u8` to `u16` can be expressed infallibly using `From`
  --> src/lib.rs:12:27
   |
12 |           impl BitSplit for $int {
   |  ___________________________^
13 | |             type Half = $half;
14 | |             #[inline]
15 | |             fn merge(halves: [Self::Half; 2]) -> Self {
16 | |                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 | |                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
   | |____________________________________________________________^
...
26 |   impl_ints!( u16 =>  u8;                                    0x00FF);
   |   ------------------------------------------------------------------ in this macro invocation
   |
   = help: an `as` cast can become silently lossy if the types change in the future
   = 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)
help: use `<u16>::from` instead
   |
12 -         impl BitSplit for $int {
13 -             type Half = $half;
14 -             #[inline]
15 -             fn merge(halves: [Self::Half; 2]) -> Self {
16 -                 const HALF_SIZE: usize = std::mem::size_of::<$half>() * 8;
17 -                 (halves[0] << HALF_SIZE) as $int | halves[1] as $int
12 +         impl BitSplit for <u16>::from(halves[1]) as $int
   |

warning: `_ice-issue-96944` (lib) generated 8 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.44s

However after applying these diagnostics, the resulting code:

//@ 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 <u16>::from(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);

no longer compiled:

    Checking _ice-issue-96944 v0.1.0 (/tmp/icemaker_global_tempdir.0gE5D6KlClUx/icemaker_clippyfix_tempdir.ArOqbDGioPwO/_ice-issue-96944)
error: unexpected closing delimiter: `}`
  --> src/lib.rs:15:5
   |
11 |     ($int:ty => $half:ty; $mask:expr) => {
   |                                          - this delimiter might not be properly closed...
12 |         impl BitSplit for <u16>::from(halves[1]) as $int
13 |             }
   |             - ...as it matches this but it has different indentation
14 |         }
15 |     };
   |     ^ unexpected closing delimiter

error: could not compile `_ice-issue-96944` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_ice-issue-96944` (lib) due to 1 previous error

Version:

rustc 1.90.0-nightly (f32b23204 2025-07-26)
binary: rustc
commit-hash: f32b23204a0efe2fe8383ed4be1a30b56c1bbf94
commit-date: 2025-07-26
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedT-macrosType: Issues with macros and macro expansion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions