Skip to content

Make clippy happy for some casts#715

Merged
bkchr merged 4 commits intomasterfrom
bkchr-for-the-clippy-overlord
Mar 12, 2025
Merged

Make clippy happy for some casts#715
bkchr merged 4 commits intomasterfrom
bkchr-for-the-clippy-overlord

Conversation

@bkchr
Copy link
Copy Markdown
Member

@bkchr bkchr commented Mar 7, 2025

Closes: #713

@bkchr bkchr requested a review from gui1117 March 7, 2025 23:54
recurse_indices.push(quote_spanned! { ident.span() =>
(
(#index) as ::core::primitive::u8,
(#index) as ::core::primitive::usize,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think u8 is more accurate, in the implementation of decode and encode, the type is cast to u8, if user writes:

enum Foo {
  A = 256,
  B = 0,
}

then we want to give an error for conflicts.

The code in scale codec could be refactored to put the cast as part of the expression for variant indices but I think we are fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an extra check for this here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't take the discriminant into account no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point. I will add a check there, still think this is better than having it fail somewhere with a weird error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha we have this :D

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gui1117 please have another look

const indices: [(u8, &'static str); #len] = [#( #recurse_indices ,)*];
const indices: [(usize, &'static str); #len] = [#( #recurse_indices ,)*];

const fn search_for_invalid_index(array: &[(usize, &'static str); #len]) -> (bool, usize) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way we are future proofs for const variant indices.

recurse_indices.push(quote_spanned! { ident.span() =>
(
(#index) as ::core::primitive::u8,
(#index) as ::core::primitive::usize,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in some very rare case it could still trigger truncation if type is like repr (u128) and he is using u128 discriminant, doesn't harm to also have the flag here: cast_possible_truncation

}) if path.get_ident().map_or(false, |i| i == "index") => lit_int
.base10_parse::<u8>()
.map(|_| ())
.map_err(|_| syn::Error::new(lit_int.span(), "Index must be in 0..255")),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory this check is still necessary, when we call variant_index:

let byte = v
.base10_parse::<usize>()
.expect("Internal error, index attribute must have been checked");

somebody writing an index more than 2^64 will make the macro panic.

Though quite unlikely.

bkchr and others added 2 commits March 12, 2025 22:55
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
@bkchr bkchr merged commit df4f439 into master Mar 12, 2025
17 checks passed
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

Successfully merging this pull request may close these issues.

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

2 participants