Skip to content

Commit

Permalink
lint: add clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Jul 27, 2021
1 parent 15cfad9 commit 1bcc160
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 64 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
root = true

[*]
indent_style=tab
indent_size=tab
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
- uses: actions/checkout@v2
- name: Rustfmt
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all
build:
runs-on: ubuntu-latest
steps:
Expand Down
24 changes: 6 additions & 18 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,22 @@ pub enum ExitReason {
impl ExitReason {
/// Whether the exit is succeeded.
pub fn is_succeed(&self) -> bool {
match self {
Self::Succeed(_) => true,
_ => false,
}
matches!(self, Self::Succeed(_))
}

/// Whether the exit is error.
pub fn is_error(&self) -> bool {
match self {
Self::Error(_) => true,
_ => false,
}
matches!(self, Self::Error(_))
}

/// Whether the exit is revert.
pub fn is_revert(&self) -> bool {
match self {
Self::Revert(_) => true,
_ => false,
}
matches!(self, Self::Revert(_))
}

/// Whether the exit is fatal.
pub fn is_fatal(&self) -> bool {
match self {
Self::Fatal(_) => true,
_ => false,
}
matches!(self, Self::Fatal(_))
}
}

Expand Down Expand Up @@ -120,8 +108,8 @@ pub enum ExitError {
/// Create init code exceeds limit (runtime).
CreateContractLimit,

/// An opcode accesses external information, but the request is off offset
/// limit (runtime).
/// An opcode accesses external information, but the request is off offset
/// limit (runtime).
OutOfOffset,
/// Execution runs out of gas (runtime).
OutOfGas,
Expand Down
2 changes: 1 addition & 1 deletion core/src/eval/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn exp(op1: U256, op2: U256) -> U256 {
if op2 & 1.into() != 0.into() {
r = r.overflowing_mul(op1).0;
}
op2 = op2 >> 1;
op2 >>= 1;
op1 = op1.overflowing_mul(op1).0;
}

Expand Down
18 changes: 6 additions & 12 deletions core/src/eval/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,29 @@ pub fn byte(op1: U256, op2: U256) -> U256 {

#[inline]
pub fn shl(shift: U256, value: U256) -> U256 {
let ret = if value == U256::zero() || shift >= U256::from(256) {
if value == U256::zero() || shift >= U256::from(256) {
U256::zero()
} else {
let shift: u64 = shift.as_u64();
value << shift as usize
};

ret
}
}

#[inline]
pub fn shr(shift: U256, value: U256) -> U256 {
let ret = if value == U256::zero() || shift >= U256::from(256) {
if value == U256::zero() || shift >= U256::from(256) {
U256::zero()
} else {
let shift: u64 = shift.as_u64();
value >> shift as usize
};

ret
}
}

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

let ret = if value == I256::zero() || shift >= U256::from(256) {
if value == I256::zero() || shift >= U256::from(256) {
let I256(sign, _) = value;
match sign {
// value is 0 or >=1, pushing 0
Expand All @@ -104,7 +100,5 @@ pub fn sar(shift: U256, value: U256) -> U256 {
I256(Sign::Minus, shifted).into()
}
}
};

ret
}
}
3 changes: 2 additions & 1 deletion core/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn calldataload(state: &mut Machine) -> Control {
pop_u256!(state, index);

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

#[inline]
pub fn msize(state: &mut Machine) -> Control {
push_u256!(state, U256::from(state.memory.effective_len()));
push_u256!(state, state.memory.effective_len());
Control::Continue(1)
}

Expand Down
1 change: 1 addition & 0 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl Memory {
let mut ret = Vec::new();
ret.resize(size, 0);

#[allow(clippy::needless_range_loop)]
for index in 0..size {
let position = offset + index;
if position >= self.data.len() {
Expand Down
2 changes: 1 addition & 1 deletion core/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl Opcode {
/// Whether the opcode is a push opcode.
pub fn is_push(&self) -> Option<u8> {
let value = self.0;
if value >= 0x60 && value <= 0x7f {
if (0x60..=0x7f).contains(&value) {
Some(value - 0x60 + 1)
} else {
None
Expand Down
6 changes: 6 additions & 0 deletions core/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl Stack {
self.data.len()
}

#[inline]
/// Whether the stack is empty.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}

#[inline]
/// Stack data.
pub fn data(&self) -> &Vec<H256> {
Expand Down
18 changes: 10 additions & 8 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,28 @@ impl Default for I256 {
I256::zero()
}
}

impl From<U256> for I256 {
fn from(val: U256) -> I256 {
if val == U256::zero() {
I256::zero()
} else if val & SIGN_BIT_MASK.into() == val {
} else if val & SIGN_BIT_MASK == val {
I256(Sign::Plus, val)
} else {
I256(Sign::Minus, !val + U256::from(1u64))
}
}
}
impl Into<U256> for I256 {
fn into(self) -> U256 {
let sign = self.0;

impl From<I256> for U256 {
fn from(value: I256) -> U256 {
let sign = value.0;
if sign == Sign::NoSign {
U256::zero()
} else if sign == Sign::Plus {
self.1
value.1
} else {
!self.1 + U256::from(1u64)
!value.1 + U256::from(1u64)
}
}
}
Expand All @@ -93,7 +95,7 @@ impl Div for I256 {
return I256::min_value();
}

let d = (self.1 / other.1) & SIGN_BIT_MASK.into();
let d = (self.1 / other.1) & SIGN_BIT_MASK;

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

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

if r == U256::zero() {
return I256::zero();
Expand Down
22 changes: 10 additions & 12 deletions gasometer/src/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn suicide_refund(already_removed: bool) -> i64 {
}
}

#[allow(clippy::collapsible_else_if)]
pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config) -> i64 {
if config.sstore_gas_metering {
if current == new {
Expand All @@ -28,6 +29,7 @@ pub fn sstore_refund(original: H256, current: H256, new: H256, config: &Config)
config.refund_sstore_clears
} else {
let mut refund = 0;

if original != H256::default() {
if current == H256::default() {
refund -= config.refund_sstore_clears;
Expand Down Expand Up @@ -192,24 +194,20 @@ pub fn sstore_cost(
config: &Config,
) -> Result<u64, ExitError> {
if config.sstore_gas_metering {
if config.sstore_revert_under_stipend {
if gas < config.call_stipend {
return Err(ExitError::OutOfGas);
}
if config.sstore_revert_under_stipend && gas < config.call_stipend {
return Err(ExitError::OutOfGas);
}

Ok(if new == current {
config.gas_sload
} else {
if original == current {
if original == H256::zero() {
config.gas_sstore_set
} else {
config.gas_sstore_reset
}
} else if original == current {
if original == H256::zero() {
config.gas_sstore_set
} else {
config.gas_sload
config.gas_sstore_reset
}
} else {
config.gas_sload
})
} else {
Ok(if current == H256::zero() && new != H256::zero() {
Expand Down
1 change: 1 addition & 0 deletions gasometer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ pub fn static_opcode_cost(opcode: Opcode) -> Option<u64> {
}

/// Calculate the opcode cost.
#[allow(clippy::nonminimal_bool)]
pub fn dynamic_opcode_cost<H: Handler>(
address: H160,
opcode: Opcode,
Expand Down
2 changes: 1 addition & 1 deletion gasometer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ pub fn log2floor(value: U256) -> u64 {
}
}
}
return l;
l
}
8 changes: 4 additions & 4 deletions runtime/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn create<H: Handler>(runtime: &mut Runtime, is_create2: bool, handler: &mut

match reason {
ExitReason::Succeed(_) => {
push!(runtime, create_address.into());
push!(runtime, create_address);
Control::Continue
}
ExitReason::Revert(_) => {
Expand All @@ -318,7 +318,7 @@ pub fn create<H: Handler>(runtime: &mut Runtime, is_create2: bool, handler: &mut
}
}

pub fn call<'config, H: Handler>(
pub fn call<H: Handler>(
runtime: &mut Runtime,
scheme: CallScheme,
handler: &mut H,
Expand Down Expand Up @@ -383,13 +383,13 @@ pub fn call<'config, H: Handler>(
Some(Transfer {
source: runtime.context.address,
target: to.into(),
value: value.into(),
value,
})
} else if scheme == CallScheme::CallCode {
Some(Transfer {
source: runtime.context.address,
target: runtime.context.address,
value: value.into(),
value,
})
} else {
None
Expand Down
10 changes: 5 additions & 5 deletions src/backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
fn storage(&self, address: H160, index: H256) -> H256 {
self.state
.get(&address)
.map(|v| v.storage.get(&index).cloned().unwrap_or(H256::default()))
.unwrap_or(H256::default())
.map(|v| v.storage.get(&index).cloned().unwrap_or_default())
.unwrap_or_default()
}

fn original_storage(&self, address: H160, index: H256) -> Option<H256> {
Expand All @@ -155,7 +155,7 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
reset_storage,
} => {
let is_empty = {
let account = self.state.entry(address).or_insert(Default::default());
let account = self.state.entry(address).or_insert_with(Default::default);
account.balance = basic.balance;
account.nonce = basic.nonce;
if let Some(code) = code {
Expand All @@ -170,7 +170,7 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> {
.storage
.iter()
.filter(|(_, v)| v == &&H256::default())
.map(|(k, _)| k.clone())
.map(|(k, _)| *k)
.collect::<Vec<H256>>();

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

account.balance == U256::zero()
&& account.nonce == U256::zero()
&& account.code.len() == 0
&& account.code.is_empty()
};

if is_empty && delete_empty {
Expand Down
1 change: 1 addition & 0 deletions src/executor/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ impl<'config, S: StackState<'config>> StackExecutor<'config, S> {
}
}

#[allow(clippy::too_many_arguments)]
fn call_inner(
&mut self,
code_address: H160,
Expand Down
3 changes: 2 additions & 1 deletion src/executor/stack/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'config> MemoryStackSubstate<'config> {
return Some(
account.basic.balance == U256::zero()
&& account.basic.nonce == U256::zero()
&& code.len() == 0,
&& code.is_empty(),
);
}
}
Expand Down Expand Up @@ -249,6 +249,7 @@ impl<'config> MemoryStackSubstate<'config> {
false
}

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

0 comments on commit 1bcc160

Please sign in to comment.