Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast-lossless suggests calling macro in itself #12695

Closed
matthiaskrgr opened this issue Apr 19, 2024 · 0 comments · Fixed by #13146
Closed

cast-lossless suggests calling macro in itself #12695

matthiaskrgr opened this issue Apr 19, 2024 · 0 comments · Fixed by #13146
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

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

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 19, 2024
@bors bors closed this as completed in 5e6540f Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant