Skip to content

Commit 1bcc160

Browse files
committed
lint: add clippy
1 parent 15cfad9 commit 1bcc160

File tree

17 files changed

+60
-64
lines changed

17 files changed

+60
-64
lines changed

.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
root = true
2+
23
[*]
34
indent_style=tab
45
indent_size=tab

.github/workflows/rust.yml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
- uses: actions/checkout@v2
1717
- name: Rustfmt
1818
run: cargo fmt --all -- --check
19+
- name: Clippy
20+
run: cargo clippy --all
1921
build:
2022
runs-on: ubuntu-latest
2123
steps:

core/src/error.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,22 @@ pub enum ExitReason {
3333
impl ExitReason {
3434
/// Whether the exit is succeeded.
3535
pub fn is_succeed(&self) -> bool {
36-
match self {
37-
Self::Succeed(_) => true,
38-
_ => false,
39-
}
36+
matches!(self, Self::Succeed(_))
4037
}
4138

4239
/// Whether the exit is error.
4340
pub fn is_error(&self) -> bool {
44-
match self {
45-
Self::Error(_) => true,
46-
_ => false,
47-
}
41+
matches!(self, Self::Error(_))
4842
}
4943

5044
/// Whether the exit is revert.
5145
pub fn is_revert(&self) -> bool {
52-
match self {
53-
Self::Revert(_) => true,
54-
_ => false,
55-
}
46+
matches!(self, Self::Revert(_))
5647
}
5748

5849
/// Whether the exit is fatal.
5950
pub fn is_fatal(&self) -> bool {
60-
match self {
61-
Self::Fatal(_) => true,
62-
_ => false,
63-
}
51+
matches!(self, Self::Fatal(_))
6452
}
6553
}
6654

@@ -120,8 +108,8 @@ pub enum ExitError {
120108
/// Create init code exceeds limit (runtime).
121109
CreateContractLimit,
122110

123-
/// An opcode accesses external information, but the request is off offset
124-
/// limit (runtime).
111+
/// An opcode accesses external information, but the request is off offset
112+
/// limit (runtime).
125113
OutOfOffset,
126114
/// Execution runs out of gas (runtime).
127115
OutOfGas,

core/src/eval/arithmetic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn exp(op1: U256, op2: U256) -> U256 {
8181
if op2 & 1.into() != 0.into() {
8282
r = r.overflowing_mul(op1).0;
8383
}
84-
op2 = op2 >> 1;
84+
op2 >>= 1;
8585
op1 = op1.overflowing_mul(op1).0;
8686
}
8787

core/src/eval/bitwise.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,29 @@ pub fn byte(op1: U256, op2: U256) -> U256 {
5858

5959
#[inline]
6060
pub fn shl(shift: U256, value: U256) -> U256 {
61-
let ret = if value == U256::zero() || shift >= U256::from(256) {
61+
if value == U256::zero() || shift >= U256::from(256) {
6262
U256::zero()
6363
} else {
6464
let shift: u64 = shift.as_u64();
6565
value << shift as usize
66-
};
67-
68-
ret
66+
}
6967
}
7068

7169
#[inline]
7270
pub fn shr(shift: U256, value: U256) -> U256 {
73-
let ret = if value == U256::zero() || shift >= U256::from(256) {
71+
if value == U256::zero() || shift >= U256::from(256) {
7472
U256::zero()
7573
} else {
7674
let shift: u64 = shift.as_u64();
7775
value >> shift as usize
78-
};
79-
80-
ret
76+
}
8177
}
8278

8379
#[inline]
8480
pub fn sar(shift: U256, value: U256) -> U256 {
8581
let value = I256::from(value);
8682

87-
let ret = if value == I256::zero() || shift >= U256::from(256) {
83+
if value == I256::zero() || shift >= U256::from(256) {
8884
let I256(sign, _) = value;
8985
match sign {
9086
// value is 0 or >=1, pushing 0
@@ -104,7 +100,5 @@ pub fn sar(shift: U256, value: U256) -> U256 {
104100
I256(Sign::Minus, shifted).into()
105101
}
106102
}
107-
};
108-
109-
ret
103+
}
110104
}

core/src/eval/misc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn calldataload(state: &mut Machine) -> Control {
2929
pop_u256!(state, index);
3030

3131
let mut load = [0u8; 32];
32+
#[allow(clippy::needless_range_loop)]
3233
for i in 0..32 {
3334
if let Some(p) = index.checked_add(U256::from(i)) {
3435
if p <= U256::from(usize::MAX) {
@@ -146,7 +147,7 @@ pub fn pc(state: &mut Machine, position: usize) -> Control {
146147

147148
#[inline]
148149
pub fn msize(state: &mut Machine) -> Control {
149-
push_u256!(state, U256::from(state.memory.effective_len()));
150+
push_u256!(state, state.memory.effective_len());
150151
Control::Continue(1)
151152
}
152153

core/src/memory.rs

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl Memory {
8585
let mut ret = Vec::new();
8686
ret.resize(size, 0);
8787

88+
#[allow(clippy::needless_range_loop)]
8889
for index in 0..size {
8990
let position = offset + index;
9091
if position >= self.data.len() {

core/src/opcode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl Opcode {
242242
/// Whether the opcode is a push opcode.
243243
pub fn is_push(&self) -> Option<u8> {
244244
let value = self.0;
245-
if value >= 0x60 && value <= 0x7f {
245+
if (0x60..=0x7f).contains(&value) {
246246
Some(value - 0x60 + 1)
247247
} else {
248248
None

core/src/stack.rs

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ impl Stack {
3030
self.data.len()
3131
}
3232

33+
#[inline]
34+
/// Whether the stack is empty.
35+
pub fn is_empty(&self) -> bool {
36+
self.data.is_empty()
37+
}
38+
3339
#[inline]
3440
/// Stack data.
3541
pub fn data(&self) -> &Vec<H256> {

core/src/utils.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,28 @@ impl Default for I256 {
5757
I256::zero()
5858
}
5959
}
60+
6061
impl From<U256> for I256 {
6162
fn from(val: U256) -> I256 {
6263
if val == U256::zero() {
6364
I256::zero()
64-
} else if val & SIGN_BIT_MASK.into() == val {
65+
} else if val & SIGN_BIT_MASK == val {
6566
I256(Sign::Plus, val)
6667
} else {
6768
I256(Sign::Minus, !val + U256::from(1u64))
6869
}
6970
}
7071
}
71-
impl Into<U256> for I256 {
72-
fn into(self) -> U256 {
73-
let sign = self.0;
72+
73+
impl From<I256> for U256 {
74+
fn from(value: I256) -> U256 {
75+
let sign = value.0;
7476
if sign == Sign::NoSign {
7577
U256::zero()
7678
} else if sign == Sign::Plus {
77-
self.1
79+
value.1
7880
} else {
79-
!self.1 + U256::from(1u64)
81+
!value.1 + U256::from(1u64)
8082
}
8183
}
8284
}
@@ -93,7 +95,7 @@ impl Div for I256 {
9395
return I256::min_value();
9496
}
9597

96-
let d = (self.1 / other.1) & SIGN_BIT_MASK.into();
98+
let d = (self.1 / other.1) & SIGN_BIT_MASK;
9799

98100
if d == U256::zero() {
99101
return I256::zero();
@@ -117,7 +119,7 @@ impl Rem for I256 {
117119
type Output = I256;
118120

119121
fn rem(self, other: I256) -> I256 {
120-
let r = (self.1 % other.1) & SIGN_BIT_MASK.into();
122+
let r = (self.1 % other.1) & SIGN_BIT_MASK;
121123

122124
if r == U256::zero() {
123125
return I256::zero();

gasometer/src/costs.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn suicide_refund(already_removed: bool) -> i64 {
1919
}
2020
}
2121

22+
#[allow(clippy::collapsible_else_if)]
2223
pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config) -> i64 {
2324
if config.sstore_gas_metering {
2425
if current == new {
@@ -28,6 +29,7 @@ pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config)
2829
config.refund_sstore_clears
2930
} else {
3031
let mut refund = 0;
32+
3133
if original != H256::default() {
3234
if current == H256::default() {
3335
refund -= config.refund_sstore_clears;
@@ -192,24 +194,20 @@ pub fn sstore_cost(
192194
config: &Config,
193195
) -> Result<u64, ExitError> {
194196
if config.sstore_gas_metering {
195-
if config.sstore_revert_under_stipend {
196-
if gas < config.call_stipend {
197-
return Err(ExitError::OutOfGas);
198-
}
197+
if config.sstore_revert_under_stipend && gas < config.call_stipend {
198+
return Err(ExitError::OutOfGas);
199199
}
200200

201201
Ok(if new == current {
202202
config.gas_sload
203-
} else {
204-
if original == current {
205-
if original == H256::zero() {
206-
config.gas_sstore_set
207-
} else {
208-
config.gas_sstore_reset
209-
}
203+
} else if original == current {
204+
if original == H256::zero() {
205+
config.gas_sstore_set
210206
} else {
211-
config.gas_sload
207+
config.gas_sstore_reset
212208
}
209+
} else {
210+
config.gas_sload
213211
})
214212
} else {
215213
Ok(if current == H256::zero() && new != H256::zero() {

gasometer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ pub fn static_opcode_cost(opcode: Opcode) -> Option<u64> {
406406
}
407407

408408
/// Calculate the opcode cost.
409+
#[allow(clippy::nonminimal_bool)]
409410
pub fn dynamic_opcode_cost<H: Handler>(
410411
address: H160,
411412
opcode: Opcode,

gasometer/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ pub fn log2floor(value: U256) -> u64 {
1616
}
1717
}
1818
}
19-
return l;
19+
l
2020
}

runtime/src/eval/system.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ pub fn create<H: Handler>(runtime: &mut Runtime, is_create2: bool, handler: &mut
294294

295295
match reason {
296296
ExitReason::Succeed(_) => {
297-
push!(runtime, create_address.into());
297+
push!(runtime, create_address);
298298
Control::Continue
299299
}
300300
ExitReason::Revert(_) => {
@@ -318,7 +318,7 @@ pub fn create<H: Handler>(runtime: &mut Runtime, is_create2: bool, handler: &mut
318318
}
319319
}
320320

321-
pub fn call<'config, H: Handler>(
321+
pub fn call<H: Handler>(
322322
runtime: &mut Runtime,
323323
scheme: CallScheme,
324324
handler: &mut H,
@@ -383,13 +383,13 @@ pub fn call<'config, H: Handler>(
383383
Some(Transfer {
384384
source: runtime.context.address,
385385
target: to.into(),
386-
value: value.into(),
386+
value,
387387
})
388388
} else if scheme == CallScheme::CallCode {
389389
Some(Transfer {
390390
source: runtime.context.address,
391391
target: runtime.context.address,
392-
value: value.into(),
392+
value,
393393
})
394394
} else {
395395
None

src/backend/memory.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
129129
fn storage(&self, address: H160, index: H256) -> H256 {
130130
self.state
131131
.get(&address)
132-
.map(|v| v.storage.get(&index).cloned().unwrap_or(H256::default()))
133-
.unwrap_or(H256::default())
132+
.map(|v| v.storage.get(&index).cloned().unwrap_or_default())
133+
.unwrap_or_default()
134134
}
135135

136136
fn original_storage(&self, address: H160, index: H256) -> Option<H256> {
@@ -155,7 +155,7 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
155155
reset_storage,
156156
} => {
157157
let is_empty = {
158-
let account = self.state.entry(address).or_insert(Default::default());
158+
let account = self.state.entry(address).or_insert_with(Default::default);
159159
account.balance = basic.balance;
160160
account.nonce = basic.nonce;
161161
if let Some(code) = code {
@@ -170,7 +170,7 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
170170
.storage
171171
.iter()
172172
.filter(|(_, v)| v == &&H256::default())
173-
.map(|(k, _)| k.clone())
173+
.map(|(k, _)| *k)
174174
.collect::<Vec<H256>>();
175175

176176
for zero in zeros {
@@ -187,7 +187,7 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
187187

188188
account.balance == U256::zero()
189189
&& account.nonce == U256::zero()
190-
&& account.code.len() == 0
190+
&& account.code.is_empty()
191191
};
192192

193193
if is_empty && delete_empty {

src/executor/stack/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
513513
}
514514
}
515515

516+
#[allow(clippy::too_many_arguments)]
516517
fn call_inner(
517518
&mut self,
518519
code_address: H160,

src/executor/stack/state.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'config> MemoryStackSubstate<'config> {
197197
return Some(
198198
account.basic.balance == U256::zero()
199199
&& account.basic.nonce == U256::zero()
200-
&& code.len() == 0,
200+
&& code.is_empty(),
201201
);
202202
}
203203
}
@@ -249,6 +249,7 @@ impl<'config> MemoryStackSubstate<'config> {
249249
false
250250
}
251251

252+
#[allow(clippy::map_entry)]
252253
fn account_mut<B: Backend>(&mut self, address: H160, backend: &B) -> &mut MemoryStackAccount {
253254
if !self.accounts.contains_key(&address) {
254255
let account = self

0 commit comments

Comments
 (0)