Skip to content

Clippy errros with "casting usize to u8 may truncate the value" #713

Closed
@cmichi

Description

@cmichi

IIRC there was a compilation error about the usize to u8 cast a while ago, the release was yanked at the time, and it was fixed. Clippy still errors because of it though:

main.rs:

use parity_scale_codec::{
    Decode,
    Encode,
};

#[derive(Encode, Decode)]
pub enum Foo {
    Bar,
}

fn main() { }

Cargo.toml:

[package]
name = "foobar"
edition = "2024"

[dependencies]
parity-scale-codec = { version = "=3.7.4", features = ["derive"] }

[workspace]

The following errors appear for clippy:

$ cargo +1.85 clippy -- -Dclippy::cast_possible_truncation  
    Checking foobar v0.0.0 (/tmp/foobar)
error: casting `usize` to `u8` may truncate the value
 --> src/main.rs:8:5
  |
8 |     Bar,
  |     ^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
  = note: requested on the command line with `-D clippy::cast-possible-truncation`
help: ... or use `try_from` and handle the error accordingly
  |
8 |     Bar::try_from(Encode),
  |     ~~~~~~~~~~~~~~~~~~~~~

error: casting `usize` to `u8` may truncate the value
 --> src/main.rs:8:5
  |
8 |     Bar,
  |     ^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
help: ... or use `try_from` and handle the error accordingly
  |
8 |     Bar::try_from(Encode),
  |     ~~~~~~~~~~~~~~~~~~~~~

error: casting `usize` to `u8` may truncate the value
 --> src/main.rs:8:5
  |
8 |     Bar,
  |     ^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
help: ... or use `try_from` and handle the error accordingly
  |
8 |     Bar::try_from(Decode),
  |     ~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions