Skip to content

Commit 4cf321e

Browse files
authored
Use Clippy in CI (#162)
* Configure CI to run clippy * Fix up clippy issues in vesting * Fix up clippy issues in utilities * Fix up clippy issues in tokens * Fix up clippy issues in schedule-update * Fix up clippy issues in oracle * Fix up clippy issues in gradually-update * Fix up clippy issues in currencies * Fix up clippy issues in auction * Fix formatting * Deny warnings when running clippy on CI * Fix clippy issues in benchmarking
1 parent 1710692 commit 4cf321e

File tree

12 files changed

+56
-42
lines changed

12 files changed

+56
-42
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ jobs:
3636
run: rustup target add wasm32-unknown-unknown
3737
- name: Check format
3838
run: make dev-format-check
39+
- name: Install clippy
40+
run: rustup component add clippy
41+
- name: Run clippy
42+
run: cargo clippy -- -D warnings
3943
- name: Update
4044
run: cargo update
4145
- name: Check for Wasm

auction/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2+
// Disable the following two lints since they originate from an external macro (namely decl_storage)
3+
#![allow(clippy::string_lit_as_bytes)]
24

35
use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, IterableStorageDoubleMap, Parameter};
46
use frame_system::{self as system, ensure_signed};

benchmarking/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ pub use sp_runtime::traits::{Dispatchable, One, Zero};
133133
///
134134
/// ```ignore
135135
/// sort_vector {
136-
/// let x in 1 .. 10000;
137-
/// let mut m = Vec::<u32>::new();
138-
/// for i in (0..x).rev() {
139-
/// m.push(i);
140-
/// }
136+
/// let x in 1 .. 10000;
137+
/// let mut m = Vec::<u32>::new();
138+
/// for i in (0..x).rev() {
139+
/// m.push(i);
140+
/// }
141141
/// }: {
142-
/// m.sort();
142+
/// m.sort();
143143
/// } verify {
144-
/// ensure!(m[0] == 0, "You forgot to sort!")
144+
/// ensure!(m[0] == 0, "You forgot to sort!")
145145
/// }
146146
/// ```
147147
///

currencies/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,24 +585,24 @@ where
585585

586586
fn set_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) {
587587
Currency::set_lock(
588-
lock_id.into(),
588+
lock_id,
589589
who,
590590
BalanceConvert::from(amount).into(),
591-
(WithdrawReason::Transfer | WithdrawReason::Reserve).into(),
591+
WithdrawReason::Transfer | WithdrawReason::Reserve,
592592
);
593593
}
594594

595595
fn extend_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) {
596596
Currency::extend_lock(
597-
lock_id.into(),
597+
lock_id,
598598
who,
599599
BalanceConvert::from(amount).into(),
600-
(WithdrawReason::Transfer | WithdrawReason::Reserve).into(),
600+
WithdrawReason::Transfer | WithdrawReason::Reserve,
601601
);
602602
}
603603

604604
fn remove_lock(lock_id: LockIdentifier, who: &AccountId) {
605-
Currency::remove_lock(lock_id.into(), who);
605+
Currency::remove_lock(lock_id, who);
606606
}
607607
}
608608

@@ -644,7 +644,7 @@ where
644644
value: Self::Balance,
645645
status: BalanceStatus,
646646
) -> result::Result<Self::Balance, DispatchError> {
647-
Currency::repatriate_reserved(slashed, beneficiary, BalanceConvert::from(value).into(), status.into())
647+
Currency::repatriate_reserved(slashed, beneficiary, BalanceConvert::from(value).into(), status)
648648
.map(|a| BalanceConvert::from(a).into())
649649
}
650650
}

gradually-update/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2+
// Disable the following two lints since they originate from an external macro (namely decl_storage)
3+
#![allow(clippy::string_lit_as_bytes)]
24

35
use codec::{Decode, Encode};
46
use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, storage, traits::Get};
@@ -116,6 +118,8 @@ impl<T: Trait> Module<T> {
116118
}
117119

118120
let mut gradually_updates = GraduallyUpdates::get();
121+
122+
#[allow(clippy::redundant_clone)] // FIXME: This looks like a bug in clippy
119123
for (i, update) in gradually_updates.clone().iter().enumerate() {
120124
let current_value = storage::unhashed::get::<StorageValue>(&update.key).unwrap_or_default();
121125
let current_value_u128 = u128::from_le_bytes(Self::convert_vec_to_u8(&current_value));
@@ -127,12 +131,11 @@ impl<T: Trait> Module<T> {
127131

128132
let target_u128 = u128::from_le_bytes(Self::convert_vec_to_u8(&update.target_value));
129133

130-
let new_value_u128: u128;
131-
if current_value_u128 > target_u128 {
132-
new_value_u128 = (current_value_u128.checked_sub(step_u128).unwrap()).max(target_u128);
134+
let new_value_u128 = if current_value_u128 > target_u128 {
135+
(current_value_u128.checked_sub(step_u128).unwrap()).max(target_u128)
133136
} else {
134-
new_value_u128 = (current_value_u128.checked_add(step_u128).unwrap()).min(target_u128);
135-
}
137+
(current_value_u128.checked_add(step_u128).unwrap()).min(target_u128)
138+
};
136139

137140
// current_value equal target_value, remove gradually_update
138141
if new_value_u128 == target_u128 {
@@ -155,10 +158,11 @@ impl<T: Trait> Module<T> {
155158
GraduallyUpdateBlockNumber::<T>::put(now);
156159
}
157160

161+
#[allow(clippy::ptr_arg)]
158162
fn convert_vec_to_u8(input: &StorageValue) -> [u8; 16] {
159163
let mut array: [u8; 16] = [0; 16];
160164
for (i, v) in input.iter().enumerate() {
161-
array[i] = v.clone();
165+
array[i] = *v;
162166
}
163167
array
164168
}

oracle/src/default_combine_data.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ where
2424
let now = T::Time::now();
2525
let mut valid_values = values
2626
.into_iter()
27-
.filter_map(|x| {
28-
if x.timestamp + expires_in > now {
29-
return Some(x);
30-
}
31-
None
32-
})
27+
.filter(|x| x.timestamp + expires_in > now)
3328
.collect::<Vec<TimestampedValueOf<T>>>();
3429

3530
let count = valid_values.len() as u32;
@@ -41,6 +36,6 @@ where
4136
valid_values.sort_by(|a, b| a.value.cmp(&b.value));
4237

4338
let median_index = count / 2;
44-
return Some(valid_values[median_index as usize].clone());
39+
Some(valid_values[median_index as usize].clone())
4540
}
4641
}

oracle/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2+
// Disable the following two lints since they originate from an external macro (namely decl_storage)
3+
#![allow(clippy::string_lit_as_bytes)]
24

35
mod default_combine_data;
46
mod mock;
@@ -149,7 +151,7 @@ impl<T: Trait> Module<T> {
149151
}
150152
}
151153

152-
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
154+
#[derive(Encode, Decode, Clone, Eq, PartialEq, Default)]
153155
pub struct CheckOperator<T: Trait + Send + Sync>(marker::PhantomData<T>);
154156

155157
impl<T: Trait + Send + Sync> CheckOperator<T> {
@@ -204,7 +206,7 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckOperator<T> {
204206
..Default::default()
205207
});
206208
}
207-
return Ok(ValidTransaction::default());
209+
Ok(ValidTransaction::default())
208210
}
209211
}
210212

schedule-update/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
2+
// Disable the following two lints since they originate from an external macro (namely decl_storage)
3+
#![allow(clippy::string_lit_as_bytes)]
24

35
use codec::{Decode, Encode};
46
use frame_support::{
@@ -160,7 +162,7 @@ decl_module! {
160162
origin = frame_system::RawOrigin::Root.into();
161163
}
162164

163-
let result = call.dispatch(origin.clone());
165+
let result = call.dispatch(origin);
164166
if let Err(e) = result {
165167
Self::deposit_event(RawEvent::ScheduleDispatchFail(id, e.error));
166168
} else {
@@ -184,7 +186,7 @@ decl_module! {
184186
origin = frame_system::RawOrigin::Root.into();
185187
}
186188

187-
let result = call.dispatch(origin.clone());
189+
let result = call.dispatch(origin);
188190
if let Err(e) = result {
189191
Self::deposit_event(RawEvent::ScheduleDispatchFail(id, e.error));
190192
} else {

tokens/src/lib.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
//! The tokens module depends on the `GenesisConfig`. Endowed accounts could be configured in genesis configs.
3333
3434
#![cfg_attr(not(feature = "std"), no_std)]
35+
// Disable the following two lints since they originate from an external macro (namely decl_storage)
36+
#![allow(clippy::redundant_closure_call, clippy::string_lit_as_bytes)]
3537

3638
use codec::{Decode, Encode};
3739
use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, traits::Get, Parameter};
@@ -429,10 +431,7 @@ impl<T: Trait> MultiLockableCurrency<T::AccountId> for Module<T> {
429431
if amount.is_zero() {
430432
return;
431433
}
432-
let mut new_lock = Some(BalanceLock {
433-
id: lock_id,
434-
amount: amount,
435-
});
434+
let mut new_lock = Some(BalanceLock { id: lock_id, amount });
436435
let mut locks = Self::locks(currency_id, who)
437436
.into_iter()
438437
.filter_map(|lock| {
@@ -455,10 +454,7 @@ impl<T: Trait> MultiLockableCurrency<T::AccountId> for Module<T> {
455454
if amount.is_zero() {
456455
return;
457456
}
458-
let mut new_lock = Some(BalanceLock {
459-
id: lock_id,
460-
amount: amount,
461-
});
457+
let mut new_lock = Some(BalanceLock { id: lock_id, amount });
462458
let mut locks = Self::locks(currency_id, who)
463459
.into_iter()
464460
.filter_map(|lock| {

utilities/src/fixed_u128.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl<'de> Deserialize<'de> for FixedU128 {
274274
D: Deserializer<'de>,
275275
{
276276
let s = String::deserialize(deserializer)?;
277-
FixedU128::try_from_u128_str(&s).map_err(|err_str| de::Error::custom(err_str))
277+
FixedU128::try_from_u128_str(&s).map_err(de::Error::custom)
278278
}
279279
}
280280

utilities/src/linked_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
}
3333

3434
fn read(key: &Key, value: Option<Value>) -> LinkedItem<Value> {
35-
Storage::get(&(key.clone(), value)).unwrap_or_else(|| Default::default())
35+
Storage::get(&(key.clone(), value)).unwrap_or_else(Default::default)
3636
}
3737

3838
fn take(key: &Key, value: Value) -> LinkedItem<Value> {

vesting/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
//! - `update_vesting_schedules` - Update all vesting schedules under an account, `root` origin required.
2222
2323
#![cfg_attr(not(feature = "std"), no_std)]
24+
// Disable the following two lints since they originate from an external macro (namely decl_storage)
25+
#![allow(clippy::redundant_closure_call, clippy::string_lit_as_bytes)]
2426

2527
use codec::{Decode, Encode, HasCompact};
2628
use frame_support::{
@@ -87,6 +89,13 @@ impl<BlockNumber: AtLeast32Bit + Copy, Balance: AtLeast32Bit + Copy> VestingSche
8789

8890
pub type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
8991
pub type VestingScheduleOf<T> = VestingSchedule<<T as frame_system::Trait>::BlockNumber, BalanceOf<T>>;
92+
pub type ScheduledItem<T> = (
93+
<T as frame_system::Trait>::AccountId,
94+
<T as frame_system::Trait>::BlockNumber,
95+
<T as frame_system::Trait>::BlockNumber,
96+
u32,
97+
BalanceOf<T>,
98+
);
9099

91100
pub trait Trait: frame_system::Trait {
92101
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
@@ -106,7 +115,7 @@ decl_storage! {
106115
}
107116

108117
add_extra_genesis {
109-
config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, u32, BalanceOf<T>)>;
118+
config(vesting): Vec<ScheduledItem<T>>;
110119
}
111120
}
112121

@@ -206,7 +215,7 @@ impl<T: Trait> Module<T> {
206215
) -> DispatchResult {
207216
let schedule_amount = Self::ensure_valid_vesting_schedule(&schedule)?;
208217
let total_amount = Self::locked_balance(to)
209-
.checked_add(&schedule_amount.into())
218+
.checked_add(&schedule_amount)
210219
.ok_or(Error::<T>::NumOverflow)?;
211220

212221
T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?;

0 commit comments

Comments
 (0)