Lokathor/bytemuck

derive(Pod) generates code that triggers warn-by-default clippy::identity_op

Closed this issue · 2 comments

Hi! Thanks for bytemuck ❤️ It is a pleasure to work with (compared to my previous unsafe versions of bytes_of and from_bytes ).

When switching to it, I managed to trigger https://rust-lang.github.io/rust-clippy/master/index.html#identity_op in the code generated by the derives.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=%23%5Brepr(C)%5D%0A%23%5Bderive(Debug%2C%20Clone%2C%20Copy%2C%20PartialEq%2C%20Default%2C%20bytemuck%3A%3AZeroable%2C%20bytemuck%3A%3APod)%5D%0Apub%20struct%20Vec2%20%7B%0A%20%20%20%20x%3A%20f32%2C%0A%20%20%20%20y%3A%20f32%2C%0A%7D%0A%0Afn%20main()%20%7B%7D

(run clippy on tthe play pen)

The culprit seems to be this generated code, emitted as a static check before the unsafe impl is emitted:

const _: fn() =
    ||
        {
            struct TypeWithoutPadding([u8; 0 + ::core::mem::size_of::<f32>() +
                                               ::core::mem::size_of::<f32>()]);
            let _ = ::core::mem::transmute::<Vec2, TypeWithoutPadding>;
        };

Seems to be trivially fixable by checking the number of fields beforehand in https://github.com/Lokathor/bytemuck/blob/main/derive/src/traits.rs#L209

I can submit a fix, if you desired.

uh, sure if you wanna fix it up.

This OCD is going to be the end of me one day.