-
Notifications
You must be signed in to change notification settings - Fork 20
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
Conversation
#[allow(dead_code)] | ||
struct BlockWeights; |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 🎉
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>, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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.
#[allow(clippy::useless_conversion)] | ||
let name = Vec::from(name).try_into().expect("error"); |
There was a problem hiding this comment.
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, | ||
) |
There was a problem hiding this comment.
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],[]` | |||
/// |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the level up
@@ -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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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>"; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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")] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why removed?
There was a problem hiding this comment.
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.
There was a problem hiding this 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!!
Goal
The goal of this PR is to update and enable a bunch of Clippy lints.
Closes #1855
Discussion
make lint-fix
that runs clippy fixChecklist