diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f38feb1f2..8370b51ea 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -13,13 +13,19 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Rustfmt run: cargo fmt --all -- --check + - name: clippy + run: cargo clippy --workspace -- -D warnings + - name: clippy no-std + run: cargo clippy --workspace --no-default-features -- -D warnings + - name: clippy with features + run: cargo clippy --workspace --features=with-codec,with-serde -- -D warnings build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Build run: cargo build --verbose - name: Run tests @@ -27,8 +33,8 @@ jobs: jsontests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - uses: actions/checkout@v4 with: path: jsontests/res/ethtests repository: ethereum/tests diff --git a/interpreter/src/call_create.rs b/interpreter/src/call_create.rs index eb13813a9..453063292 100644 --- a/interpreter/src/call_create.rs +++ b/interpreter/src/call_create.rs @@ -140,6 +140,7 @@ pub struct CallTrapData { } impl CallTrapData { + #[allow(clippy::too_many_arguments)] fn new_from_params + AsMut>( scheme: CallScheme, memory: &mut Memory, diff --git a/interpreter/src/eval/mod.rs b/interpreter/src/eval/mod.rs index 57ebbcd22..739623dfd 100644 --- a/interpreter/src/eval/mod.rs +++ b/interpreter/src/eval/mod.rs @@ -51,7 +51,7 @@ where self.0.map(|f| { let fr = wrapper(f, current_opcode); if current_opcode != Opcode(255) { - current_opcode.0 = current_opcode.0 + 1; + current_opcode.0 += current_opcode.0; } fr }), diff --git a/interpreter/src/memory.rs b/interpreter/src/memory.rs index c363a37ed..9b5f99b4c 100644 --- a/interpreter/src/memory.rs +++ b/interpreter/src/memory.rs @@ -82,9 +82,7 @@ impl Memory { /// Resize to range. Used for return value. pub fn resize_to_range(&mut self, return_range: Range) { let ret = if return_range.start > U256::from(usize::MAX) { - let mut ret = Vec::new(); - ret.resize((return_range.end - return_range.start).as_usize(), 0); - ret + vec![0; (return_range.end - return_range.start).as_usize()] } else if return_range.end > U256::from(usize::MAX) { let mut ret = self.get( return_range.start.as_usize(), @@ -111,8 +109,7 @@ impl Memory { /// Value of `size` is considered trusted. If they're too large, /// the program can run out of memory, or it can overflow. pub fn get(&self, offset: usize, size: usize) -> Vec { - let mut ret = Vec::new(); - ret.resize(size, 0); + let mut ret = vec![0; size]; #[allow(clippy::needless_range_loop)] for index in 0..size { diff --git a/jsontests/src/error.rs b/jsontests/src/error.rs index e10e31b33..125f07d26 100644 --- a/jsontests/src/error.rs +++ b/jsontests/src/error.rs @@ -1,3 +1,4 @@ +#![allow(clippy::upper_case_acronyms)] use thiserror::Error; #[derive(Error, Debug)] diff --git a/jsontests/src/main.rs b/jsontests/src/main.rs index 88cd5228a..d733c6908 100644 --- a/jsontests/src/main.rs +++ b/jsontests/src/main.rs @@ -48,7 +48,7 @@ fn run_file(filename: &str, debug: bool) -> Result<(), Error> { } } if debug { - println!(""); + println!(); } } } @@ -57,14 +57,14 @@ fn run_file(filename: &str, debug: bool) -> Result<(), Error> { } fn run_single(filename: &str, debug: bool) -> Result<(), Error> { - if fs::metadata(&filename)?.is_dir() { - for filename in fs::read_dir(&filename)? { + if fs::metadata(filename)?.is_dir() { + for filename in fs::read_dir(filename)? { let filepath = filename?.path(); let filename = filepath.to_str().ok_or(Error::NonUtf8Filename)?; run_file(filename, debug)?; } } else { - run_file(&filename, debug)?; + run_file(filename, debug)?; } Ok(()) diff --git a/jsontests/src/types.rs b/jsontests/src/types.rs index 0d3d317dc..bdeda8cf5 100644 --- a/jsontests/src/types.rs +++ b/jsontests/src/types.rs @@ -26,7 +26,7 @@ impl TestMulti { tests.push(Test { info: self.info.clone(), env: self.env.clone(), - fork: fork.clone(), + fork: *fork, index, post: post_state.clone(), pre: self.pre.clone(), diff --git a/precompile/src/modexp.rs b/precompile/src/modexp.rs index d002da153..d1d1f6fc9 100644 --- a/precompile/src/modexp.rs +++ b/precompile/src/modexp.rs @@ -179,6 +179,7 @@ impl PurePrecompile for Modexp { // always true except in the case of zero-length modulus, which leads to // output of length and value 1. + #[allow(clippy::comparison_chain)] if bytes.len() == mod_len { (ExitSucceed::Returned.into(), bytes.to_vec()) } else if bytes.len() < mod_len { diff --git a/precompile/src/simple.rs b/precompile/src/simple.rs index 114edfc4c..d2efae35f 100644 --- a/precompile/src/simple.rs +++ b/precompile/src/simple.rs @@ -65,7 +65,7 @@ impl PurePrecompile for Sha256 { ))))); let mut ret = [0u8; 32]; - let hash = ripemd::Ripemd160::digest(&input[..]); + let hash = ripemd::Ripemd160::digest(input); ret[12..32].copy_from_slice(&hash); (ExitSucceed::Returned.into(), ret.to_vec()) @@ -89,7 +89,7 @@ impl PurePrecompile for Ripemd160 { COST_WORD ))))); - let hash = sha2::Sha256::digest(&input[..]); + let hash = sha2::Sha256::digest(input); (ExitSucceed::Returned.into(), hash.to_vec()) } diff --git a/src/call_stack.rs b/src/call_stack.rs index 06fbb26ba..6a6a92696 100644 --- a/src/call_stack.rs +++ b/src/call_stack.rs @@ -40,7 +40,7 @@ where backend: &'backend mut H, invoker: &'invoker I, ) -> Self { - let call_stack = Self { + Self { stack: Vec::new(), last: Some(LastSubstack { machine, @@ -49,11 +49,10 @@ where initial_depth, backend, invoker, - }; - - call_stack + } } + #[allow(clippy::type_complexity)] pub fn run(&mut self) -> Capture, I::Interrupt> { loop { let step_ret = self.step_run(); @@ -64,6 +63,7 @@ where } } + #[allow(clippy::type_complexity)] pub fn step( &mut self, ) -> Result<(), Capture, I::Interrupt>> { @@ -76,6 +76,7 @@ where }) } + #[allow(clippy::type_complexity)] pub fn step_run( &mut self, ) -> Result<(), Capture, I::Interrupt>> { @@ -85,6 +86,7 @@ where }) } + #[allow(clippy::type_complexity)] fn step_with( &mut self, fs: FS, @@ -96,7 +98,7 @@ where self.last = match self.last.take() { None => { - step_ret = Some(Capture::Exit(Err(ExitFatal::AlreadyExited.into()))); + step_ret = Some(Capture::Exit(Err(ExitFatal::AlreadyExited))); None } Some(LastSubstack { @@ -320,6 +322,7 @@ where }))) } + #[allow(clippy::type_complexity)] fn step_with( &mut self, fs: FS, @@ -393,6 +396,7 @@ where } /// Step the call stack, but run the interpreter inside. + #[allow(clippy::type_complexity)] pub fn step_run( &mut self, ) -> Result<(), Capture, I::Interrupt>> { @@ -400,6 +404,7 @@ where } /// Step the call stack, and step the interpreter inside. + #[allow(clippy::type_complexity)] pub fn step( &mut self, ) -> Result<(), Capture, I::Interrupt>> { @@ -435,45 +440,37 @@ where I: Invoker, { fn drop(&mut self) { - if let Some(state) = self.0.take() { - match state { - HeapTransactState::Running { - mut call_stack, - transact_invoke, - } => { - if let Some(mut last) = call_stack.last.take() { - loop { - if let Some(mut parent) = call_stack.stack.pop() { - let last_machine = last.machine.deconstruct(); - let _ = call_stack.invoker.exit_substack( - ExitFatal::Unfinished.into(), - last_machine, - parent.invoke, - &mut parent.machine, - call_stack.backend, - ); - - last = LastSubstack { - machine: parent.machine, - status: LastSubstackStatus::Exited(Capture::Exit( - ExitFatal::Unfinished.into(), - )), - }; - } else { - break; - } - } + if let Some(HeapTransactState::Running { + mut call_stack, + transact_invoke, + }) = self.0.take() + { + if let Some(mut last) = call_stack.last.take() { + while let Some(mut parent) = call_stack.stack.pop() { + let last_machine = last.machine.deconstruct(); + let _ = call_stack.invoker.exit_substack( + ExitFatal::Unfinished.into(), + last_machine, + parent.invoke, + &mut parent.machine, + call_stack.backend, + ); - let last_machine = last.machine.deconstruct(); - let _ = call_stack.invoker.finalize_transact( - &transact_invoke, + last = LastSubstack { + machine: parent.machine, + status: LastSubstackStatus::Exited(Capture::Exit( ExitFatal::Unfinished.into(), - last_machine, - call_stack.backend, - ); - } + )), + }; } - _ => (), + + let last_machine = last.machine.deconstruct(); + let _ = call_stack.invoker.finalize_transact( + &transact_invoke, + ExitFatal::Unfinished.into(), + last_machine, + call_stack.backend, + ); } } } diff --git a/src/color.rs b/src/color.rs index 5c2f1284f..c2c6b445c 100644 --- a/src/color.rs +++ b/src/color.rs @@ -96,12 +96,12 @@ where is_static: bool, handler: &mut H, ) -> Result<(), Capture> { - match gasometer.record_step(&machine, is_static, handler) { + match gasometer.record_step(machine, is_static, handler) { Ok(()) => { - machine.state.as_mut().gas = gasometer.gas().into(); + machine.state.as_mut().gas = gasometer.gas(); machine.step(handler, self) } - Err(e) => return Err(Capture::Exit(Err(e))), + Err(e) => Err(Capture::Exit(Err(e))), } } @@ -113,9 +113,9 @@ where handler: &mut H, ) -> Capture { loop { - match gasometer.record_stepn(&machine, is_static, handler) { + match gasometer.record_stepn(machine, is_static, handler) { Ok(stepn) => { - machine.state.as_mut().gas = gasometer.gas().into(); + machine.state.as_mut().gas = gasometer.gas(); match machine.stepn(stepn, handler, self) { Ok(()) => (), Err(c) => return c, diff --git a/src/invoker.rs b/src/invoker.rs index d3d5d7587..9ae829bef 100644 --- a/src/invoker.rs +++ b/src/invoker.rs @@ -45,6 +45,7 @@ pub trait Invoker { type SubstackInvoke; /// Create a new transaction with the given transaction arguments. + #[allow(clippy::type_complexity)] fn new_transact( &self, args: Self::TransactArgs, @@ -73,6 +74,7 @@ pub trait Invoker { ) -> Result; /// Enter a sub-layer call stack. + #[allow(clippy::type_complexity)] fn enter_substack( &self, trap: Tr, diff --git a/src/standard/gasometer/mod.rs b/src/standard/gasometer/mod.rs index b84d7227e..9e2f0ff64 100644 --- a/src/standard/gasometer/mod.rs +++ b/src/standard/gasometer/mod.rs @@ -19,7 +19,7 @@ pub trait TransactGasometer<'config, S: AsRef>: Sized { fn new_transact_call( gas_limit: U256, data: &[u8], - access_list: &Vec<(H160, Vec)>, + access_list: &[(H160, Vec)], config: &'config Config, ) -> Result; @@ -27,7 +27,7 @@ pub trait TransactGasometer<'config, S: AsRef>: Sized { fn new_transact_create( gas_limit: U256, code: &[u8], - access_list: &Vec<(H160, Vec)>, + access_list: &[(H160, Vec)], config: &'config Config, ) -> Result; @@ -134,7 +134,7 @@ impl<'config, S: AsRef> TransactGasometer<'config, S> for Gasomete fn new_transact_call( gas_limit: U256, data: &[u8], - access_list: &Vec<(H160, Vec)>, + access_list: &[(H160, Vec)], config: &'config Config, ) -> Result { let gas_limit = if gas_limit > U256::from(u64::MAX) { @@ -153,7 +153,7 @@ impl<'config, S: AsRef> TransactGasometer<'config, S> for Gasomete fn new_transact_create( gas_limit: U256, code: &[u8], - access_list: &Vec<(H160, Vec)>, + access_list: &[(H160, Vec)], config: &'config Config, ) -> Result { let gas_limit = if gas_limit > U256::from(u64::MAX) { @@ -881,7 +881,7 @@ impl TransactionCost { } pub fn cost(&self, config: &Config) -> u64 { - let gas_cost = match self { + match self { TransactionCost::Call { zero_data_len, non_zero_data_len, @@ -915,9 +915,7 @@ impl TransactionCost { cost } - }; - - gas_cost + } } } diff --git a/src/standard/invoker/mod.rs b/src/standard/invoker/mod.rs index fbc5cbd02..e72e4f963 100644 --- a/src/standard/invoker/mod.rs +++ b/src/standard/invoker/mod.rs @@ -207,7 +207,7 @@ where Some(salt) => { let scheme = CreateScheme::Create2 { caller: *caller, - code_hash: H256::from_slice(Keccak256::digest(&init_code).as_slice()), + code_hash: H256::from_slice(Keccak256::digest(init_code).as_slice()), salt: *salt, }; scheme.address(handler) @@ -278,7 +278,7 @@ where context, transaction_context: Rc::new(transaction_context), retbuf: Vec::new(), - gas: U256::from(gas_limit), + gas: gas_limit, }), gasometer, handler, @@ -316,7 +316,7 @@ where context, transaction_context: Rc::new(transaction_context), retbuf: Vec::new(), - gas: U256::from(gas_limit), + gas: gas_limit, }), gasometer, handler, @@ -429,10 +429,8 @@ where let target_gas = trap_data.target_gas().unwrap_or(after_gas); let gas_limit = min(after_gas, target_gas); - let call_has_value = match &trap_data { - CallCreateTrapData::Call(call) if call.has_value() => true, - _ => false, - }; + let call_has_value = + matches!(&trap_data, CallCreateTrapData::Call(call) if call.has_value()); let is_static = if machine.is_static { true @@ -457,9 +455,9 @@ where context: call_trap_data.context.clone(), transaction_context, retbuf: Vec::new(), - gas: U256::from(gas_limit), + gas: gas_limit, }, - &machine, + machine, ); let target = call_trap_data.target; @@ -493,9 +491,9 @@ where }, transaction_context, retbuf: Vec::new(), - gas: U256::from(gas_limit), + gas: gas_limit, }, - &machine, + machine, ); Capture::Exit(routines::enter_create_substack( diff --git a/src/standard/invoker/resolver.rs b/src/standard/invoker/resolver.rs index 93d3854e1..ef4f16836 100644 --- a/src/standard/invoker/resolver.rs +++ b/src/standard/invoker/resolver.rs @@ -21,6 +21,7 @@ pub trait Resolver { type Color: Color; /// Resolve a call (with the target code address). + #[allow(clippy::type_complexity)] fn resolve_call( &self, code_address: H160, @@ -35,6 +36,7 @@ pub trait Resolver { >; /// Resolve a create (with the init code). + #[allow(clippy::type_complexity)] fn resolve_create( &self, init_code: Vec, diff --git a/src/standard/invoker/routines.rs b/src/standard/invoker/routines.rs index 5071b0c63..efc8b031a 100644 --- a/src/standard/invoker/routines.rs +++ b/src/standard/invoker/routines.rs @@ -10,17 +10,19 @@ use crate::{ }; use primitive_types::{H160, U256}; +#[allow(clippy::type_complexity)] pub fn maybe_analyse_code<'config, S: AsRef, G: TransactGasometer<'config, S>, C>( result: &mut InvokerControl, (ExitResult, (S, G, Vec))>, ) { if let InvokerControl::Enter(machine) = result { - machine.gasometer.analyse_code(&machine.machine.code()) + machine.gasometer.analyse_code(machine.machine.code()) } } -pub fn make_enter_call_machine<'config, 'resolver, S, G, H, R, Tr>( - _config: &'config Config, - resolver: &'resolver R, +#[allow(clippy::too_many_arguments, clippy::type_complexity)] +pub fn make_enter_call_machine( + _config: &Config, + resolver: &R, code_address: H160, input: Vec, is_static: bool, @@ -44,9 +46,10 @@ where resolver.resolve_call(code_address, input, is_static, state, gasometer, handler) } -pub fn make_enter_create_machine<'config, 'resolver, S, G, H, R, Tr>( - config: &'config Config, - resolver: &'resolver R, +#[allow(clippy::type_complexity, clippy::too_many_arguments)] +pub fn make_enter_create_machine( + config: &Config, + resolver: &R, caller: H160, init_code: Vec, is_static: bool, @@ -87,6 +90,7 @@ where resolver.resolve_create(init_code, is_static, state, gasometer, handler) } +#[allow(clippy::type_complexity, clippy::too_many_arguments)] pub fn enter_call_substack<'config, 'resolver, S, G, H, R, Tr>( config: &'config Config, resolver: &'resolver R, @@ -136,6 +140,7 @@ where } } +#[allow(clippy::type_complexity, clippy::too_many_arguments)] pub fn enter_create_substack<'config, 'resolver, S, G, H, R, Tr>( config: &'config Config, resolver: &'resolver R, @@ -205,8 +210,8 @@ fn check_first_byte(config: &Config, code: &[u8]) -> Result<(), ExitError> { Ok(()) } -pub fn deploy_create_code<'config, S, G, H>( - config: &'config Config, +pub fn deploy_create_code( + config: &Config, address: H160, retbuf: &Vec, gasometer: &mut G,