paritytech/parity-scale-codec

`parity-scale-codec = { version = "3.6.0" }` clippy warnings on `#[derive(Encode)]` using `#[repr(u8)]`

Closed this issue · 2 comments

The issue is pretty similar to #454

Repr case

// lib.rs
use parity_scale_codec::{Encode};

#[repr(u8)]
#[derive(Encode)]
enum CLike {
    Foo = 0,
    Bar = 1,
}
# Cargo.toml
[package]
name = "parity_scale_codec_clippy"
version = "0.1.0"
edition = "2021"
authors = ['breathx <novikov.dm.al@gmail.com>']

[dependencies]
parity-scale-codec = { version = "3.6.0", default-features = false, features = ["derive"] }

Clippy error

Run of cargo clippy ends up in following warning output:

warning: variants `Foo` and `Bar` are never constructed
 --> src/lib.rs:6:5
  |
5 | enum CLike {
  |      ----- variants in this enum
6 |     Foo = 0,
  |     ^^^
7 |     Bar = 1,
  |     ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: casting integer literal to `u8` is unnecessary
 --> src/lib.rs:6:5
  |
6 |     Foo = 0,
  |     ^^^^^^^ help: try: `0_u8`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
  = note: `#[warn(clippy::unnecessary_cast)]` on by default

warning: casting integer literal to `u8` is unnecessary
 --> src/lib.rs:7:5
  |
7 |     Bar = 1,
  |     ^^^^^^^ help: try: `1_u8`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast

warning: `parity_scale_codec_clippy` (lib) generated 3 warnings (run `cargo clippy --fix --lib -p parity_scale_codec_clippy` to apply 2 suggestions)
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s

Macro expand

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use parity_scale_codec::Encode;
#[repr(u8)]
enum CLike {
    Foo = 0,
    Bar = 1,
}
#[allow(deprecated)]
const _: () = {
    #[automatically_derived]
    impl ::parity_scale_codec::Encode for CLike {
        fn encode_to<
            __CodecOutputEdqy: ::parity_scale_codec::Output + ?::core::marker::Sized,
        >(&self, __codec_dest_edqy: &mut __CodecOutputEdqy) {
            match *self {
                CLike::Foo => {
                    __codec_dest_edqy.push_byte(0 as ::core::primitive::u8);
                }
                CLike::Bar => {
                    __codec_dest_edqy.push_byte(1 as ::core::primitive::u8);
                }
                _ => {}
            }
        }
    }
    #[automatically_derived]
    impl ::parity_scale_codec::EncodeLike for CLike {}
};

As we can see, following lines trigger warning:

// ...
__codec_dest_edqy.push_byte(0 as ::core::primitive::u8);
// ... 
__codec_dest_edqy.push_byte(1 as ::core::primitive::u8);
// ...

Possible solutions

Apply clippy suggestion

// ...
__codec_dest_edqy.push_byte(0_u8);
// ... 
__codec_dest_edqy.push_byte(1_u8);
// ...

Ignore lint

// ...
#[allow(clippy::unnecessary_cast)]
__codec_dest_edqy.push_byte(0 as ::core::primitive::u8);
// ... 
#[allow(clippy::unnecessary_cast)]
__codec_dest_edqy.push_byte(1 as ::core::primitive::u8);
// ...
bkchr commented

@koute could you also fix this in your pr?

koute commented

@koute could you also fix this in your pr?

Sure.