diff --git a/CHANGELOG.md b/CHANGELOG.md index 94fd1c95..a0a4aa70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this crate adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h ## Unreleased +### Fixed + +- Derived `Encode`/`Decode` for enums no longer fails under `#![deny(trivial_numeric_casts)]` ([#740](https://github.com/paritytech/parity-scale-codec/issues/740)) + ### [3.7.5] - 2025-05-20 ### Fixed diff --git a/derive/src/decode.rs b/derive/src/decode.rs index 5403a730..15316e06 100644 --- a/derive/src/decode.rs +++ b/derive/src/decode.rs @@ -60,6 +60,7 @@ pub fn quote( quote_spanned! { v.span() => #[allow(clippy::unnecessary_cast)] #[allow(clippy::cast_possible_truncation)] + #[allow(trivial_numeric_casts)] __codec_x_edqy if __codec_x_edqy == (#index) as ::core::primitive::u8 => { // NOTE: This lambda is necessary to work around an upstream bug // where each extra branch results in excessive stack usage: diff --git a/derive/src/encode.rs b/derive/src/encode.rs index d4a5257d..771cc0d9 100644 --- a/derive/src/encode.rs +++ b/derive/src/encode.rs @@ -337,6 +337,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenS let encoding = quote_spanned! { f.span() => #type_name :: #name { #( ref #encoding_names, )* } => { #[allow(clippy::unnecessary_cast)] + #[allow(trivial_numeric_casts)] #dest.push_byte((#index) as ::core::primitive::u8); #encode_fields } @@ -370,6 +371,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenS let encoding = quote_spanned! { f.span() => #type_name :: #name ( #( ref #encoding_names, )* ) => { #[allow(clippy::unnecessary_cast)] + #[allow(trivial_numeric_casts)] #dest.push_byte((#index) as ::core::primitive::u8); #encode_fields } @@ -388,6 +390,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenS #type_name :: #name => { #[allow(clippy::unnecessary_cast)] #[allow(clippy::cast_possible_truncation)] + #[allow(trivial_numeric_casts)] #dest.push_byte((#index) as ::core::primitive::u8); } }; diff --git a/derive/src/utils.rs b/derive/src/utils.rs index 075ee9e5..acf23d1a 100644 --- a/derive/src/utils.rs +++ b/derive/src/utils.rs @@ -64,6 +64,7 @@ pub fn const_eval_check_variant_indexes( const _: () = { #[allow(clippy::unnecessary_cast)] #[allow(clippy::cast_possible_truncation)] + #[allow(trivial_numeric_casts)] const indices: [(usize, &'static str); #len] = [#( #recurse_indices ,)*]; const fn search_for_invalid_index(array: &[(usize, &'static str); #len]) -> (bool, usize) { diff --git a/tests/clippy.rs b/tests/clippy.rs index 3b552645..03b57899 100644 --- a/tests/clippy.rs +++ b/tests/clippy.rs @@ -16,6 +16,8 @@ //! This file is checked by clippy to make sure that the code generated by the derive macro //! doesn't spew out warnings/errors in users' code. +#![deny(trivial_numeric_casts)] + use parity_scale_codec_derive::{Decode, DecodeWithMemTracking, Encode}; #[repr(u8)] @@ -24,3 +26,9 @@ pub enum CLike { Foo = 0, Bar = 1, } + +#[derive(Decode, DecodeWithMemTracking, Encode)] +pub enum UnitVariants { + One, + Two, +}