jam1garner / binrw

A Rust crate for helping parse and rebuild binary data using ✨macro magic✨.
https://binrw.rs
MIT License
545 stars 35 forks source link

Clippy warning `unnecessary_fallible_conversions` in generated code for `count` directive #249

Closed rafalh closed 3 months ago

rafalh commented 4 months ago

Example code:

use binrw::BinRead;

#[derive(BinRead)]
pub struct Test {
    pub num: u16,
    #[br(count = num)]
    pub vec: Vec<u32>,
}

Clippy warning:

--> crates/binrw_test/src/lib.rs:6:18
  |
6 |     #[br(count = num)]
  |                  ^^^ help: use: `From::from`
  |
  = note: converting `u16` to `usize` cannot fail
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fallible_conversions
  = note: `#[warn(clippy::unnecessary_fallible_conversions)]` on by default

If I do the conversion manually by using as operator or usize::from, it no longer shows the warning. Maybe it would make sense to add #[allow(clippy::unnecessary_fallible_conversions)] in the generated code or avoid fallible conversion for u16 and u8.

csnover commented 3 months ago

This was actually fixed last December in 2f6d9faefd70d9f6e4b0a6a0920f6419c7573f38 but then just never tagged a new version. You can temporarily point your dependency to git until a new version is released. Sorry about that!