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

Struct that fully contains a signed integer causes clippy error. #22

Closed
snakehand opened this issue Oct 25, 2023 · 3 comments
Closed

Struct that fully contains a signed integer causes clippy error. #22

snakehand opened this issue Oct 25, 2023 · 3 comments

Comments

@snakehand
Copy link

snakehand commented Oct 25, 2023

This struct produces a clippy error:

#[bitfield(u16)]
/// X axis output value
pub struct OutX{
#[bits(16)]
val: i16,
}

error: &-masking with zero
--> src/lib.rs:564:1
|
564 | #[bitfield(u16)]
| ^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
= note: #[deny(clippy::bad_bit_mask)] on by default
= note: this error originates in the attribute macro bitfield (in Nightly builds, run with -Z macro-backtrace for more info)

I have debugged it and tracked it down to a debug assert, and propose this change that captures the same logic without causing errors in clippy:

index d0e0c39..669947b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -727,7 +727,7 @@ fn parse_field(attrs: &[syn::Attribute], ty: &syn::Type, ignore: bool) -> syn::R
// Bounds check and remove leading ones from negative values
ret.into = quote! {{
#[allow(unused_comparisons)]

  •            debug_assert!(if this >= 0 { this & !#mask == 0 } else { !this & !#mask == 0 }, "value out of bounds");
    
  •            debug_assert!(if this >= 0 { this | #mask == #mask } else { !(this | #mask) == 0 }, "value out of bounds");
               (this & #mask) as _
           }};
       }
    
snakehand added a commit to snakehand/bitfield-struct-rs that referenced this issue Oct 25, 2023
Avoid performing logical and on !mask as this will cause a clippy lint error if result is 0.

Reformulate boolean check to os logial or opereations instead.
@snakehand
Copy link
Author

bitfield_err.zip

Minimal example

@wrenger
Copy link
Owner

wrenger commented Nov 3, 2023

Hi @snakehand, thanks for your PR. I'm currently traveling but I'll look into this as soon as I'm back next week.

Can you also tell me the rustc/Clippy version you are using?

@wrenger
Copy link
Owner

wrenger commented Nov 11, 2023

The bounds check was error-prone and has been fixed (as discussed in #24). This also resolves this issue.

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