diff --git a/src/gas/mod.rs b/src/gas/mod.rs index 6466e9e..57b2e95 100644 --- a/src/gas/mod.rs +++ b/src/gas/mod.rs @@ -109,7 +109,7 @@ impl Counter { self.finalize_metered_block(cursor)?; // Pop the control block stack. - let closing_control_block = self.stack.pop().ok_or_else(|| ())?; + let closing_control_block = self.stack.pop().ok_or(())?; let closing_control_index = self.stack.len(); if self.stack.is_empty() { @@ -118,7 +118,7 @@ impl Counter { // Update the lowest_forward_br_target for the control block now on top of the stack. { - let control_block = self.stack.last_mut().ok_or_else(|| ())?; + let control_block = self.stack.last_mut().ok_or(())?; control_block.lowest_forward_br_target = min( control_block.lowest_forward_br_target, closing_control_block.lowest_forward_br_target, @@ -140,7 +140,7 @@ impl Counter { /// Finalized blocks have final cost which will not change later. fn finalize_metered_block(&mut self, cursor: usize) -> Result<(), ()> { let closing_metered_block = { - let control_block = self.stack.last_mut().ok_or_else(|| ())?; + let control_block = self.stack.last_mut().ok_or(())?; mem::replace( &mut control_block.active_metered_block, MeteredBlock { start_pos: cursor + 1, cost: 0 }, @@ -181,14 +181,14 @@ impl Counter { // Update the lowest_forward_br_target of the current control block. for &index in indices { let target_is_loop = { - let target_block = self.stack.get(index).ok_or_else(|| ())?; + let target_block = self.stack.get(index).ok_or(())?; target_block.is_loop }; if target_is_loop { continue } - let control_block = self.stack.last_mut().ok_or_else(|| ())?; + let control_block = self.stack.last_mut().ok_or(())?; control_block.lowest_forward_br_target = min(control_block.lowest_forward_br_target, index); } @@ -203,14 +203,14 @@ impl Counter { /// Get a reference to the currently active metered block. fn active_metered_block(&mut self) -> Result<&mut MeteredBlock, ()> { - let top_block = self.stack.last_mut().ok_or_else(|| ())?; + let top_block = self.stack.last_mut().ok_or(())?; Ok(&mut top_block.active_metered_block) } /// Increment the cost of the current block by the specified value. fn increment(&mut self, val: u32) -> Result<(), ()> { let top_block = self.active_metered_block()?; - top_block.cost = top_block.cost.checked_add(val).ok_or_else(|| ())?; + top_block.cost = top_block.cost.checked_add(val).ok_or(())?; Ok(()) } } @@ -308,20 +308,20 @@ pub(crate) fn determine_metered_blocks( counter.increment(instruction_cost)?; // Label is a relative index into the control stack. - let active_index = counter.active_control_block_index().ok_or_else(|| ())?; - let target_index = active_index.checked_sub(*label as usize).ok_or_else(|| ())?; + let active_index = counter.active_control_block_index().ok_or(())?; + let target_index = active_index.checked_sub(*label as usize).ok_or(())?; counter.branch(cursor, &[target_index])?; }, BrTable(br_table_data) => { counter.increment(instruction_cost)?; - let active_index = counter.active_control_block_index().ok_or_else(|| ())?; + let active_index = counter.active_control_block_index().ok_or(())?; let target_indices = [br_table_data.default] .iter() .chain(br_table_data.table.iter()) .map(|label| active_index.checked_sub(*label as usize)) .collect::>>() - .ok_or_else(|| ())?; + .ok_or(())?; counter.branch(cursor, &target_indices)?; }, Return => { diff --git a/src/graph.rs b/src/graph.rs index bb0ac2f..0ea6bf6 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -723,7 +723,7 @@ fn custom_round( idx: &mut usize, sections: &mut Vec, ) { - while let Some(other_section) = map.get(&idx) { + while let Some(other_section) = map.get(idx) { sections.push(other_section.clone()); *idx += 1; } diff --git a/src/optimizer.rs b/src/optimizer.rs index 074fcda..909ac5c 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -43,14 +43,14 @@ pub fn optimize( } // If there is start function in module, it should stary - module.start_section().map(|ss| stay.insert(resolve_function(&module, ss))); + module.start_section().map(|ss| stay.insert(resolve_function(module, ss))); // All symbols used in data/element segments are also should be preserved let mut init_symbols = Vec::new(); if let Some(data_section) = module.data_section() { for segment in data_section.entries() { push_code_symbols( - &module, + module, segment .offset() .as_ref() @@ -63,7 +63,7 @@ pub fn optimize( if let Some(elements_section) = module.elements_section() { for segment in elements_section.entries() { push_code_symbols( - &module, + module, segment .offset() .as_ref() @@ -72,7 +72,7 @@ pub fn optimize( &mut init_symbols, ); for func_index in segment.members() { - stay.insert(resolve_function(&module, *func_index)); + stay.insert(resolve_function(module, *func_index)); } } } @@ -403,13 +403,9 @@ pub fn optimize( } // Also drop all custom sections - module.sections_mut().retain(|section| { - if let elements::Section::Custom(_) = section { - false - } else { - true - } - }); + module + .sections_mut() + .retain(|section| !matches!(section, elements::Section::Custom(_))); Ok(()) } diff --git a/src/pack.rs b/src/pack.rs index 7ca546e..cbed83b 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -120,7 +120,7 @@ pub fn pack_instance( mbuilder.push_import( builder::import() .module("env") - .field(&target.symbols().ret) + .field(target.symbols().ret) .external() .func(import_sig) .build(), @@ -172,11 +172,10 @@ pub fn pack_instance( let last_function_index = ctor_module.functions_space(); // We ensure here that module has the DataSection - if ctor_module + if !ctor_module .sections() .iter() - .find(|section| matches!(**section, Section::Data(_))) - .is_none() + .any(|section| matches!(*section, Section::Data(_))) { // DataSection has to be the last non-custom section according the to the spec ctor_module diff --git a/src/rules.rs b/src/rules.rs index b4b428e..6e33f95 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -340,10 +340,6 @@ impl Rules for Set { } fn memory_grow_cost(&self) -> Option { - if let Some(val) = NonZeroU32::new(self.grow) { - Some(MemoryGrowCost::Linear(val)) - } else { - None - } + NonZeroU32::new(self.grow).map(MemoryGrowCost::Linear) } } diff --git a/src/stack_height/max_height.rs b/src/stack_height/max_height.rs index 22ed253..8fb752c 100644 --- a/src/stack_height/max_height.rs +++ b/src/stack_height/max_height.rs @@ -80,10 +80,7 @@ impl Stack { /// Returns `Err` if the control stack is empty. fn pop_frame(&mut self) -> Result { trace!(target: "max_height", "pop_frame: {:?}", self.control_stack.last()); - Ok(self - .control_stack - .pop() - .ok_or_else(|| Error("stack must be non-empty".into()))?) + self.control_stack.pop().ok_or_else(|| Error("stack must be non-empty".into())) } /// Truncate the height of value stack to the specified height. diff --git a/src/stack_height/mod.rs b/src/stack_height/mod.rs index 7eae64d..d855cba 100644 --- a/src/stack_height/mod.rs +++ b/src/stack_height/mod.rs @@ -176,7 +176,7 @@ fn compute_stack_costs(module: &elements::Module) -> Result, Error> { // We can't calculate stack_cost of the import functions. Ok(0) } else { - compute_stack_cost(func_idx as u32, &module) + compute_stack_cost(func_idx as u32, module) } }) .collect()