Skip to content

Stricter Linting with Rust 1.81+ and Clippy #2346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 21, 2025
Merged

Stricter Linting with Rust 1.81+ and Clippy #2346

merged 10 commits into from
Apr 21, 2025

Conversation

wilwade
Copy link
Collaborator

@wilwade wilwade commented Apr 21, 2025

Goal

The goal of this PR is to update and enable a bunch of Clippy lints.

Closes #1855

Discussion

  • Most of these things are normal clippy fixes
  • Highlighted the ones that are strange
  • Added make lint-fix that runs clippy fix

Checklist

  • Spec version incremented?

wilwade added 3 commits April 21, 2025 09:32
Additional Fixes

Formatting

More fixes

More fixes

#[allow(dead_code)] in weights

Small fixes

Clippy is happy
Comment on lines +76 to 77
#[allow(dead_code)]
struct BlockWeights;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This isn't always dead code, but sometimes is and we just don't need generated files throwing lints

@@ -190,13 +190,13 @@ pub fn get_schema_data_map<'a>(
/// let schema_fingerprint = avro::fingerprint_raw_schema(raw_schema);
/// assert!(schema_fingerprint.is_ok());
/// ```
pub fn validate_raw_avro_schema(json_schema: &Vec<u8>) -> Result<(), AvroError> {
match String::from_utf8(json_schema.clone()) {
pub fn validate_raw_avro_schema(json_schema: &[u8]) -> Result<(), AvroError> {
Copy link
Collaborator Author

@wilwade wilwade Apr 21, 2025

Choose a reason for hiding this comment

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

This is the new syntax that is preferred and reduces a step for Rust referencing. I also like it better.

assert_eq!(false, is_full);
assert!(!is_full);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok. I didn't like this until I learned about how rust is improving the assert! macro. See rust-lang/rust#44838 for more info. Although not all of it works now, it is the future.

vec![MSA_ACCOUNT_LOCK_NAME_PREFIX, msa_id.numtoa(10, &mut buff)].concat()
[MSA_ACCOUNT_LOCK_NAME_PREFIX, msa_id.numtoa(10, &mut buff)].concat()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are a lot of these. Don't need to vec! everywhere anymore 🎉

Comment on lines -71 to 73
pub fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Vec<u8>>, D::Error>
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
where
D: Deserializer<'de>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't double your wheres

where
D: Deserializer<'de>,
{
impl_serde::serialize::deserialize(deserializer).map(|r| Some(r))
impl_serde::serialize::deserialize(deserializer).map(Some)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Functions!

dest: self.dest.clone().into(),
value: self.value.into(),
}
.into(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You'll see a lot of unneeded into()s being removed.

@@ -47,6 +47,7 @@ impl IdentifyChain for dyn sc_service::ChainSpec {

impl PartialEq for ChainIdentity {
fn eq(&self, other: &Self) -> bool {
#[allow(clippy::match_like_matches_macro)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I almost disabled this lint entirely, but the new matches! macro is nice. Just not in a few more complex places.

Comment on lines +21 to 22
#[allow(clippy::useless_conversion)]
let name = Vec::from(name).try_into().expect("error");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This conversion is not useless as it is testing the error

amount,
capacity_details.total_tokens_staked,
capacity_details.total_capacity_issued,
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If else (if else) -> if elseif else

@@ -1162,10 +1156,12 @@ impl<T: Config> Pallet<T> {
/// - Arrange the chunks such that we overwrite a complete chunk only when it is not needed
/// - The cycle is thus era modulo (history limit + chunk length)
/// - `[0,1,2],[3,4,5],[6,7,8],[]`
///
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

New line is "needed" after a list now to help make sure that you don't miss a -

while let Some((era, balance)) = bmap_iter.next() {
for (era, balance) in bmap_iter {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the suggested use instead of while.

InitialPayment::Free => true,
_ => false,
}
matches!(*self, InitialPayment::Free)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here's a great example of a good use of the new matches! macro

// for all the languages supported.
#[test]
#[ignore = "use only to regenerate compacted ALLOWED_UNICODE_CHARACTER_RANGES"]
fn test_build_allowed_char_ranges() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved this here as the original location pallets/handles/src/handles-utils/src/tests/constants_tests.rs was not able to locate build_allowed_char_ranges correctly

@@ -647,12 +647,12 @@ pub mod pallet {
model_type: &ModelType,
model: &BoundedVec<u8, T::SchemaModelMaxBytesBoundedVecLimit>,
) -> DispatchResult {
match model_type {
&ModelType::Parquet => {
match *model_type {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This * makes it so we don't have to & every match

pub struct ReleaseSchedule<BlockNumber, Balance: MaxEncodedLen + HasCompact> {
pub struct ReleaseSchedule<BlockNumber, Balance>
where
Balance: MaxEncodedLen + HasCompact,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Anytime you have more than one, it is now suggested to do a where to reduce the complexity of the signature. I agree.

@@ -17,8 +17,6 @@

//! Treasury pallet tests.

#![cfg(test)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At the level up

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Apr 21, 2025
@github-actions github-actions bot removed the metadata-changed Metadata has changed since the latest full release label Apr 21, 2025
Copy link

codecov bot commented Apr 21, 2025

Codecov Report

Attention: Patch coverage is 90.97222% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pallets/handles/src/handles-utils/constants.rs 0.00% 8 Missing ⚠️
pallets/msa/src/offchain_storage.rs 60.00% 2 Missing ⚠️
pallets/messages/src/lib.rs 75.00% 1 Missing ⚠️
pallets/msa/src/lib.rs 93.75% 1 Missing ⚠️
pallets/treasury/src/lib.rs 85.71% 1 Missing ⚠️
Files with missing lines Coverage Δ
common/helpers/src/avro.rs 92.59% <100.00%> (ø)
common/primitives/src/messages.rs 98.13% <100.00%> (ø)
common/primitives/src/offchain.rs 98.80% <100.00%> (ø)
common/primitives/src/schema.rs 49.29% <ø> (ø)
common/primitives/src/signatures.rs 58.93% <100.00%> (ø)
common/primitives/src/utils.rs 92.72% <100.00%> (-0.09%) ⬇️
pallets/capacity/src/lib.rs 89.22% <100.00%> (-0.07%) ⬇️
pallets/capacity/src/types.rs 92.85% <100.00%> (ø)
pallets/frequency-tx-payment/src/lib.rs 86.66% <100.00%> (-0.36%) ⬇️
pallets/handles/src/handles-utils/build.rs 100.00% <100.00%> (ø)
... and 19 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Apr 21, 2025
@wilwade wilwade marked this pull request as ready for review April 21, 2025 15:50
@wilwade wilwade requested review from a team, shannonwells, mattheworris, enddynayn, aramikm, claireclark1 and JoeCap08055 and removed request for a team April 21, 2025 15:50
@@ -90,15 +90,15 @@ fn set_target_details_is_successful() {
let staker = 100;
let target: MessageSourceId = 1;

assert_eq!(StakingTargetLedger::<Test>::get(&staker, target), None);
assert_eq!(StakingTargetLedger::<Test>::get(staker, target), None);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: does the new assert! apply here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps? But Clippy didn't think so.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah I see it. If would if I converted to == None, but I'd rather not do that until the assert macro is more setup

@github-actions github-actions bot removed the metadata-changed Metadata has changed since the latest full release label Apr 21, 2025
Copy link
Collaborator

@aramikm aramikm left a comment

Choose a reason for hiding this comment

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

Looks good and a lot of nice cleanups, a few nits and questions for me.

@@ -129,8 +127,8 @@ pub mod as_string_option {
}
}

const PREFIX: &'static str = "<Bytes>";
const POSTFIX: &'static str = "</Bytes>";
const PREFIX: &str = "<Bytes>";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting! So static is going to get assigned by default

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was surprised as well.

@@ -1,4 +1,3 @@
#![cfg(feature = "runtime-benchmarks")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Any reason why this is removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is put on the mod level, so shouldn't be here as well.

@@ -704,7 +701,7 @@ pub mod pallet {

// Validation: `BaseHandle` character length must be within range
ensure!(
len >= HANDLE_CHARS_MIN && len <= HANDLE_CHARS_MAX,
(HANDLE_CHARS_MIN..=HANDLE_CHARS_MAX).contains(&len),
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit, it's weird that this is preferred since it should be slower than the old expression. (but maybe the compiler knows that the difference between max and min to be close enough)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was the auto fix, so I guess so? Perhaps because HANDLE_CHARS_MIN and HANDLE_CHARS_MAX are consts?

@@ -1,4 +1,3 @@
#![cfg(feature = "runtime-benchmarks")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: why removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is put on the mod level, so shouldn't be here as well.

Copy link
Collaborator

@mattheworris mattheworris left a comment

Choose a reason for hiding this comment

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

  • Read through all changes.
  • Ran all local tests and benchmarks.
    🚢 it!!

@github-actions github-actions bot added the metadata-changed Metadata has changed since the latest full release label Apr 21, 2025
@wilwade wilwade merged commit a7d80af into main Apr 21, 2025
30 checks passed
@wilwade wilwade deleted the chore/fix-lints branch April 21, 2025 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metadata-changed Metadata has changed since the latest full release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Review new clippy linting
3 participants