Skip to content

Commit

Permalink
Use let-else and match to handle error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
valo committed Jun 27, 2023
1 parent a63b42d commit e9665d4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
13 changes: 2 additions & 11 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,7 @@ pub fn create<const IS_CREATE2: bool, SPEC: Spec>(
let mut create_input: Option<Box<CreateInputs>> = None;
prepare_create_inputs::<IS_CREATE2, SPEC>(interpreter, &mut create_input);

if create_input.is_none() {
return;
}

let mut create_input = create_input.unwrap();
let Some(mut create_input) = create_input else { return };

let (return_reason, address, gas, return_data) = host.create(&mut create_input);

Expand Down Expand Up @@ -521,12 +517,7 @@ pub fn call_inner<SPEC: Spec>(
&mut call_input,
);

if call_input.is_none() {
return;
}

// The Option is not needed form this point
let mut call_input = call_input.unwrap();
let Some(mut call_input) = call_input else { return };

// Call host to interact with target contract
let (reason, gas, return_data) = host.call(&mut call_input);
Expand Down
17 changes: 8 additions & 9 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,10 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
) -> (InstructionResult, Option<B160>, Gas, Bytes) {
let res = self.prepare_create(inputs);

if let Err(ret) = res {
return ret;
}

let (gas, created_address, checkpoint, contract) = res.unwrap();
let (gas, created_address, checkpoint, contract) = match res {
Ok(o) => o,
Err(e) => return e,
};

// Create new interpreter and execute initcode
let (exit_reason, mut interpreter) = self.run_interpreter(contract, gas.limit(), false);
Expand Down Expand Up @@ -572,11 +571,11 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
/// Main contract call of the EVM.
fn call_inner(&mut self, inputs: &mut CallInputs) -> (InstructionResult, Gas, Bytes) {
let res = self.prepare_call(inputs);
if let Err(ret) = res {
return ret;
}

let (gas, checkpoint, contract) = res.unwrap();
let (gas, checkpoint, contract) = match res {
Ok(o) => o,
Err(e) => return e,
};

let ret = if is_precompile(inputs.contract, self.precompiles.len()) {
self.call_precompile(inputs, gas)
Expand Down

0 comments on commit e9665d4

Please sign in to comment.