Skip to content

Commit f057961

Browse files
authored
chore: clippy lints (#737)
Fixes KILTprotocol/ticket#3325. Add some basic lints (can always be expanded for either runtime-specific code or all code via the configurable rustc flags in the new config file). I updated the code to address all places in which the new lints generate an error, including all weight files and weight templates. I also updated the CI check step to check for the specific WASM-targeted, no-std code that will be part of our runtime. ## How to test To check runtime code against the new lint, just run the same command as in the check workflow file: `cargo clippy --locked --target wasm32-unknown-unknown --no-default-features --workspace --exclude kilt-parachain --exclude standalone-node --exclude xcm-integration-tests --exclude 'dip-provider*' --exclude 'dip-consumer*'`
1 parent 17dbf5f commit f057961

File tree

139 files changed

+338
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+338
-126
lines changed

.cargo/config.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Deployment runtime lints
2+
[target.'cfg(all(target_arch = "wasm32", not(test)))']
3+
rustflags = [
4+
"-Dclippy::arithmetic_side_effects",
5+
"-Dclippy::as_conversions",
6+
"-Dclippy::assertions_on_result_states",
7+
"-Dclippy::cast_possible_wrap",
8+
"-Dclippy::dbg_macro",
9+
"-Dclippy::expect_used",
10+
"-Dclippy::float_arithmetic",
11+
"-Dclippy::float_cmp_const",
12+
"-Dclippy::index_refutable_slice",
13+
"-Dclippy::indexing_slicing",
14+
"-Dclippy::lossy_float_literal",
15+
"-Dclippy::panic",
16+
"-Dclippy::string_slice",
17+
"-Dclippy::todo",
18+
"-Dclippy::unimplemented",
19+
"-Dclippy::unreachable",
20+
"-Dclippy::unwrap_used",
21+
"-Funsafe_code",
22+
"-Wclippy::integer_division",
23+
"-Wclippy::modulo_arithmetic",
24+
"-Wclippy::print_stderr",
25+
"-Wclippy::print_stdout",
26+
]

.github/workflows/check-code.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ jobs:
4646

4747
strategy:
4848
matrix:
49-
features:
50-
-
51-
- --all-features
49+
cargo-flags:
50+
# Generic clippy checks for all features
51+
- --all-targets --all-features
52+
# Generic clippy checks for no features (catches some missing `no_std`-only lints)
53+
- --all-targets
54+
# Clippy lints specifically for all runtime code, excluding all test and binary crates
55+
- --target wasm32-unknown-unknown --no-default-features --workspace --exclude kilt-parachain --exclude standalone-node --exclude xcm-integration-tests --exclude 'dip-provider*' --exclude 'dip-consumer*'
5256
fail-fast: false
5357

5458
steps:
@@ -63,11 +67,11 @@ jobs:
6367
${{ env.CARGO_HOME }}/registry/index/
6468
${{ env.CARGO_HOME }}/registry/cache/
6569
${{ env.CARGO_HOME }}/git/db/
66-
key: ${{ github.job }}-${{ github.ref }}-${{ matrix.features }}-${{ hashFiles('**/Cargo.lock') }}
70+
key: ${{ github.job }}-${{ github.ref }}-${{ matrix.cargo-flags }}-${{ hashFiles('**/Cargo.lock') }}
6771
save-always: true
6872

6973
- name: Run `cargo clippy`
70-
run: cargo clippy --all-targets --locked ${{ matrix.features }}
74+
run: cargo clippy --locked ${{ matrix.cargo-flags }}
7175

7276
cargo-fmt:
7377
name: Check formatting

.gitlab-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ build-production-wasm:
2020
- export PARACHAIN_PALLET_ID=0x50
2121
- export AUTHORIZE_UPGRADE_PREFIX=0x02
2222
- export AUTHORIZE_UPGRADE_CHECK_VERSION=true
23+
- export VERBOSE=true
2324
- cp -r * /build
2425
- /srtool/build build
2526
- subwasm meta --format=json+scale /out/${RUNTIME}_runtime.compact.wasm > /out/${RUNTIME}-metadata.json

.maintain/runtime-weight-template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
{{/each}}
1414

1515
#![cfg_attr(rustfmt, rustfmt_skip)]
16-
#![allow(unused_parens)]
1716
#![allow(unused_imports)]
17+
#![allow(clippy::as_conversions)]
1818

1919
use frame_support::{traits::Get, weights::Weight};
2020
use sp_std::marker::PhantomData;

.maintain/weight-template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
{{/each}}
1515

1616
#![cfg_attr(rustfmt, rustfmt_skip)]
17-
#![allow(unused_parens)]
1817
#![allow(unused_imports)]
18+
#![allow(clippy::as_conversions)]
1919

2020
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
2121
use sp_std::marker::PhantomData;

crates/assets/src/asset.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ pub mod v1 {
4444
pub const MINIMUM_ASSET_NAMESPACE_LENGTH: usize = 3;
4545
/// The maximum length of a valid asset ID namespace.
4646
pub const MAXIMUM_NAMESPACE_LENGTH: usize = 8;
47+
#[allow(clippy::as_conversions)]
4748
const MAXIMUM_ASSET_NAMESPACE_LENGTH_U32: u32 = MAXIMUM_NAMESPACE_LENGTH as u32;
4849
/// The minimum length of a valid asset ID reference.
4950
pub const MINIMUM_ASSET_REFERENCE_LENGTH: usize = 1;
5051
/// The maximum length of a valid asset ID reference.
5152
pub const MAXIMUM_ASSET_REFERENCE_LENGTH: usize = 128;
53+
#[allow(clippy::as_conversions)]
5254
const MAXIMUM_ASSET_REFERENCE_LENGTH_U32: u32 = MAXIMUM_ASSET_REFERENCE_LENGTH as u32;
5355
/// The minimum length of a valid asset ID identifier.
5456
pub const MINIMUM_ASSET_IDENTIFIER_LENGTH: usize = 1;
5557
/// The maximum length of a valid asset ID reference.
5658
pub const MAXIMUM_ASSET_IDENTIFIER_LENGTH: usize = 78;
59+
#[allow(clippy::as_conversions)]
5760
const MAXIMUM_ASSET_IDENTIFIER_LENGTH_U32: u32 = MAXIMUM_ASSET_IDENTIFIER_LENGTH as u32;
5861

5962
/// Separator between asset namespace and asset reference.
@@ -168,6 +171,7 @@ pub mod v1 {
168171
}
169172

170173
impl Display for AssetId {
174+
#[allow(clippy::expect_used)]
171175
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
172176
match self {
173177
Self::Slip44(reference) => {
@@ -335,6 +339,7 @@ pub mod v1 {
335339
// TODO: This could be enforced at compilation time once constraints on generics
336340
// will be available.
337341
// https://rust-lang.github.io/rfcs/2000-const-generics.html
342+
#[allow(clippy::expect_used)]
338343
if value
339344
<= U256::from_str_radix("9999999999999999999999999999999999999999999999999999999999999999", 10)
340345
.expect("Casting the maximum value for a Slip44 reference into a U256 should never fail.")
@@ -480,6 +485,7 @@ pub mod v1 {
480485
}
481486

482487
impl Display for EvmSmartContractNonFungibleIdentifier {
488+
#[allow(clippy::expect_used)]
483489
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
484490
// We checked when the type is created that all characters are valid digits.
485491
write!(
@@ -578,6 +584,7 @@ pub mod v1 {
578584
}
579585

580586
impl Display for GenericAssetNamespace {
587+
#[allow(clippy::expect_used)]
581588
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
582589
// We checked when the type is created that all characters are valid UTF8
583590
// (actually ASCII) characters.
@@ -627,6 +634,7 @@ pub mod v1 {
627634
}
628635

629636
impl Display for GenericAssetReference {
637+
#[allow(clippy::expect_used)]
630638
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
631639
// We checked when the type is created that all characters are valid UTF8
632640
// (actually ASCII) characters.
@@ -676,6 +684,7 @@ pub mod v1 {
676684
}
677685

678686
impl Display for GenericAssetIdentifier {
687+
#[allow(clippy::expect_used)]
679688
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
680689
// We checked when the type is created that all characters are valid UTF8
681690
// (actually ASCII) characters.

crates/assets/src/chain.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ mod v1 {
4545
pub const MINIMUM_CHAIN_NAMESPACE_LENGTH: usize = 3;
4646
/// The maximum length of a valid chain ID namespace.
4747
pub const MAXIMUM_CHAIN_NAMESPACE_LENGTH: usize = 8;
48+
#[allow(clippy::as_conversions)]
4849
const MAXIMUM_CHAIN_NAMESPACE_LENGTH_U32: u32 = MAXIMUM_CHAIN_NAMESPACE_LENGTH as u32;
4950
/// The minimum length of a valid chain ID reference.
5051
pub const MINIMUM_CHAIN_REFERENCE_LENGTH: usize = 1;
5152
/// The maximum length of a valid chain ID reference.
5253
pub const MAXIMUM_CHAIN_REFERENCE_LENGTH: usize = 32;
54+
#[allow(clippy::as_conversions)]
5355
const MAXIMUM_CHAIN_REFERENCE_LENGTH_U32: u32 = MAXIMUM_CHAIN_REFERENCE_LENGTH as u32;
5456

5557
/// Separator between chain namespace and chain reference.
@@ -192,6 +194,7 @@ mod v1 {
192194
}
193195

194196
impl Display for ChainId {
197+
#[allow(clippy::expect_used)]
195198
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
196199
match self {
197200
Self::Bip122(reference) => {
@@ -438,6 +441,7 @@ mod v1 {
438441

439442
impl GenesisBase58Hash32Reference {
440443
/// The CAIP-2 reference for the Solana mainnet.
444+
#[allow(clippy::expect_used)]
441445
pub fn solana_mainnet() -> Self {
442446
// Base58 decoding of Solana genesis hash 4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ
443447
Self(
@@ -565,6 +569,7 @@ mod v1 {
565569
}
566570

567571
impl Display for GenericChainNamespace {
572+
#[allow(clippy::expect_used)]
568573
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
569574
// We checked when the type is created that all characters are valid UTF8
570575
// (actually ASCII) characters.
@@ -614,6 +619,7 @@ mod v1 {
614619
}
615620

616621
impl Display for GenericChainReference {
622+
#[allow(clippy::expect_used)]
617623
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
618624
// We checked when the type is created that all characters are valid UTF8
619625
// (actually ASCII) characters.

crates/assets/src/v1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl AssetDid {
146146
}
147147

148148
impl Display for AssetDid {
149+
#[allow(clippy::expect_used)]
149150
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
150151
write!(
151152
f,

crates/kilt-dip-primitives/src/merkle_proofs/v0/provider_state/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<
187187
if input.len() < 2 {
188188
return None;
189189
}
190+
#[allow(clippy::indexing_slicing)]
190191
let mut trimmed_input = &input[2..];
191192
ProviderHeader::decode(&mut trimmed_input).ok()
192193
},

crates/kilt-dip-primitives/src/verifier/parachain/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ impl<DidOriginError> From<DipParachainStateProofVerifierError<DidOriginError>> f
3232
where
3333
DidOriginError: Into<u8>,
3434
{
35+
#[allow(clippy::as_conversions)]
36+
#[allow(clippy::arithmetic_side_effects)]
3537
fn from(value: DipParachainStateProofVerifierError<DidOriginError>) -> Self {
3638
match value {
3739
// DO NOT USE 0

0 commit comments

Comments
 (0)