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

Warnings for clippy::unnecessary_cast for default values #60

Open
martiege opened this issue Jan 13, 2025 · 2 comments
Open

Warnings for clippy::unnecessary_cast for default values #60

martiege opened this issue Jan 13, 2025 · 2 comments

Comments

@martiege
Copy link

Thank you for this great package! I've been using it with success, but it generates some warnings for clippy::unnecessary_cast, e.g.

 28 |   #[bitfield(u16)]
    |  ____________^
 29 | | #[derive(PartialEq, Eq)]
 30 | | pub struct HardwareVersion {
 31 | |     #[bits(4, access = RO)]
 32 | |     pub hardware_version: u8,
 33 | |
 34 | |     #[bits(12, default = 0_u16)]
    | |______________________________^ help: try: `0_u16`
@wrenger
Copy link
Owner

wrenger commented Jan 13, 2025

Ah interesting. The cast is usually only necessary if the bitfield type differs from the struct field type.

@martiege
Copy link
Author

Yes, the strange thing is that explicitly casting does remove the warning for some data structures, but not from others...

This gives the warning:

#[bitfield(u16)]
#[derive(PartialEq, Eq)]
pub struct HardwareVersion {
    #[bits(4, access=RO)]
    pub hardware_version: u8,

    #[bits(12, default=0_u16)]
    __: u16,
}

While this does not:

#[bitfield(u16)]
#[derive(PartialEq, Eq)]
pub struct CalibrationDataChars {
    #[bits(10, access=RO)]
    pub calibration_data_chars: u16,

    #[bits(6, default=0_u8)]
    __: u8,
}

And just for fun, this gives the warning (but specifically for u16, not u8 as the empty bits should be):

#[bitfield(u16)]
#[derive(PartialEq, Eq)]
pub struct CalibrationDataChars {
    #[bits(10, access=RO)]
    pub calibration_data_chars: u16,

    #[bits(6, default=0)]
    __: u8,
}

I'm guessing this happens due to some internal way this structure is represented. The fact that the first data structure is a u16 bitfield, and the empty bits are also u16, while the second structure is a u16, and the empty bits are u8, is also likely related somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants