From 86c5b581de5489ad05c33deebd520c31f97417de Mon Sep 17 00:00:00 2001 From: nerodesu017 <46645625+nerodesu017@users.noreply.github.com> Date: Tue, 25 Jun 2024 12:00:47 +0300 Subject: [PATCH] fix: fixed clippy --- examples/stuff/main.rs | 2 +- src/core/error.rs | 3 +-- src/core/reader/mod.rs | 4 +-- src/core/reader/types/mod.rs | 10 +++---- src/core/reader/types/values.rs | 6 ++--- src/execution/assert_validated.rs | 2 +- src/execution/mod.rs | 16 +++++------ src/execution/value.rs | 2 +- src/validation/code.rs | 4 +-- src/validation/mod.rs | 44 +++++++++++++++---------------- tests/add_one.rs | 2 +- tests/basic_memory.rs | 2 +- tests/globals.rs | 2 +- tests/start_function.rs | 2 +- 14 files changed, 50 insertions(+), 51 deletions(-) diff --git a/examples/stuff/main.rs b/examples/stuff/main.rs index 73523c37..edda53f7 100644 --- a/examples/stuff/main.rs +++ b/examples/stuff/main.rs @@ -35,7 +35,7 @@ fn main() -> ExitCode { (export "add" (func $add)) ) "#; - let wasm_bytes = wat::parse_str(&wat).unwrap(); + let wasm_bytes = wat::parse_str(wat).unwrap(); let validation_info = match validate(&wasm_bytes) { Ok(table) => table, diff --git a/src/core/error.rs b/src/core/error.rs index f5f0009d..8ccc1403 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -1,10 +1,9 @@ use crate::core::indices::GlobalIdx; -use core::fmt::{Display, Formatter, Write}; +use core::fmt::{Display, Formatter}; use core::str::Utf8Error; use crate::core::reader::section_header::SectionTy; use crate::core::reader::types::ValType; -use crate::execution::store::GlobalInst; #[derive(Debug, PartialEq, Eq, Clone)] pub enum Error { diff --git a/src/core/reader/mod.rs b/src/core/reader/mod.rs index 5481e61a..b70024b9 100644 --- a/src/core/reader/mod.rs +++ b/src/core/reader/mod.rs @@ -27,7 +27,7 @@ impl<'a> WasmReader<'a> { } pub fn remaining_bytes(&self) -> &[u8] { - &self.current + self.current } pub fn current_idx(&self) -> usize { @@ -48,7 +48,7 @@ impl<'a> WasmReader<'a> { Ok(bytes.try_into().expect("the slice length to be exactly N")) } pub fn peek_u8(&self) -> Result { - self.current.get(0).copied().ok_or(Error::Eof) + self.current.first().copied().ok_or(Error::Eof) } pub fn measure_num_read_bytes( diff --git a/src/core/reader/types/mod.rs b/src/core/reader/types/mod.rs index 10cd6476..07c4a4f5 100644 --- a/src/core/reader/types/mod.rs +++ b/src/core/reader/types/mod.rs @@ -126,9 +126,9 @@ impl ValType { impl WasmReadable for ValType { fn read(wasm: &mut WasmReader) -> Result { - let numtype = NumType::read(wasm).map(|ty| ValType::NumType(ty)); + let numtype = NumType::read(wasm).map(ValType::NumType); let vectype = VecType::read(wasm).map(|_ty| ValType::VecType); - let reftype = RefType::read(wasm).map(|ty| ValType::RefType(ty)); + let reftype = RefType::read(wasm).map(ValType::RefType); numtype .or(vectype) @@ -137,9 +137,9 @@ impl WasmReadable for ValType { } fn read_unvalidated(wasm: &mut WasmReader) -> Self { - let numtype = NumType::read(wasm).map(|ty| ValType::NumType(ty)); + let numtype = NumType::read(wasm).map(ValType::NumType); let vectype = VecType::read(wasm).map(|_ty| ValType::VecType); - let reftype = RefType::read(wasm).map(|ty| ValType::RefType(ty)); + let reftype = RefType::read(wasm).map(ValType::RefType); numtype.or(vectype).or(reftype).unwrap_validated() } @@ -153,7 +153,7 @@ pub struct ResultType { impl WasmReadable for ResultType { fn read(wasm: &mut WasmReader) -> Result { - let valtypes = wasm.read_vec(|wasm| ValType::read(wasm))?; + let valtypes = wasm.read_vec(ValType::read)?; Ok(ResultType { valtypes }) } diff --git a/src/core/reader/types/values.rs b/src/core/reader/types/values.rs index 46b2b47e..35a1eece 100644 --- a/src/core/reader/types/values.rs +++ b/src/core/reader/types/values.rs @@ -14,9 +14,9 @@ use crate::{Error, Result}; impl WasmReader<'_> { /// Note: If `Err`, the [Wasm] object is no longer guaranteed to be in a valid state pub fn read_u8(&mut self) -> Result { - let value = *self.current.get(0).ok_or(Error::Eof)?; + let value = *self.current.first().ok_or(Error::Eof)?; - self.current = &self + self.current = self .current .get(1..) .expect("slice to contain at least 1 element"); @@ -72,7 +72,7 @@ impl WasmReader<'_> { let (utf8_str, rest) = self.current.split_at(len); // Cannot panic because check is done above self.current = rest; - core::str::from_utf8(utf8_str).map_err(|err| Error::MalformedUtf8String(err)) + core::str::from_utf8(utf8_str).map_err(Error::MalformedUtf8String) } pub fn read_vec_enumerated(&mut self, mut read_element: F) -> Result> diff --git a/src/execution/assert_validated.rs b/src/execution/assert_validated.rs index 5a141b96..7102e14d 100644 --- a/src/execution/assert_validated.rs +++ b/src/execution/assert_validated.rs @@ -1,6 +1,6 @@ //! Helpers for assertions due to prior validation of a WASM program. -use core::fmt::{Debug, Display}; +use core::fmt::{Debug}; pub(crate) trait UnwrapValidatedExt { fn unwrap_validated(self) -> T; diff --git a/src/execution/mod.rs b/src/execution/mod.rs index c7797309..fa8159b0 100644 --- a/src/execution/mod.rs +++ b/src/execution/mod.rs @@ -11,7 +11,7 @@ use crate::execution::locals::Locals; use crate::execution::store::{FuncInst, GlobalInst, MemInst, Store}; use crate::execution::value::Value; use crate::validation::code::read_declared_locals; -use crate::value::{InteropValue, InteropValueList}; +use crate::value::InteropValueList; use crate::{Result, ValidationInfo}; // TODO @@ -32,7 +32,7 @@ impl<'b> RuntimeInstance<'b> { pub fn new(validation_info: &'_ ValidationInfo<'b>) -> Result { trace!("Starting instantiation of bytecode"); - let store = Self::init_store(&validation_info); + let store = Self::init_store(validation_info); let mut instance = RuntimeInstance { wasm_bytecode: validation_info.wasm, @@ -50,7 +50,7 @@ impl<'b> RuntimeInstance<'b> { pub fn invoke_func( &mut self, func_idx: FuncIdx, - mut param: Param, + param: Param, ) -> Returns { let func_inst = self.store.funcs.get(func_idx).expect("valid FuncIdx"); let func_ty = self.types.get(func_inst.ty).unwrap_validated(); @@ -73,9 +73,9 @@ impl<'b> RuntimeInstance<'b> { self.function(func_idx, &mut stack); // Pop return values from stack - let mut return_values = Returns::TYS + let return_values = Returns::TYS .iter() - .map(|ty| stack.pop_value(ty.clone())) + .map(|ty| stack.pop_value(*ty)) .collect::>(); // Values are reversed because they were popped from stack one-by-one. Now reverse them back @@ -147,7 +147,7 @@ impl<'b> RuntimeInstance<'b> { let relative_address: u32 = stack.pop_value(ValType::NumType(NumType::I32)).into(); - let mem = self.store.mems.get(0).unwrap_validated(); // there is only one memory allowed as of now + let mem = self.store.mems.first().unwrap_validated(); // there is only one memory allowed as of now let data: u32 = { // The spec states that this should be a 33 bit integer @@ -227,7 +227,7 @@ impl<'b> RuntimeInstance<'b> { wasm_reader.move_start_to(*func); let (locals, bytes_read) = wasm_reader - .measure_num_read_bytes(|wasm| read_declared_locals(wasm)) + .measure_num_read_bytes(read_declared_locals) .unwrap_validated(); let code_expr = wasm_reader.make_span(func.len() - bytes_read); @@ -244,7 +244,7 @@ impl<'b> RuntimeInstance<'b> { let memory_instances: Vec = validation_info .memories .iter() - .map(|ty| MemInst::new(ty.clone())) + .map(|ty| MemInst::new(*ty)) .collect(); let global_instances: Vec = validation_info diff --git a/src/execution/value.rs b/src/execution/value.rs index 0da198e7..a00dd1cd 100644 --- a/src/execution/value.rs +++ b/src/execution/value.rs @@ -131,7 +131,7 @@ impl InteropValueList for () { } fn from_values(_values: impl Iterator) -> Self { - () + } } diff --git a/src/validation/code.rs b/src/validation/code.rs index a9bfd43c..4a6ba992 100644 --- a/src/validation/code.rs +++ b/src/validation/code.rs @@ -97,7 +97,7 @@ fn read_instructions( 0x20 => { let local_idx = wasm.read_var_u32()? as LocalIdx; let local_ty = locals.get(local_idx).ok_or(Error::InvalidLocalIdx)?; - value_stack.push_back(local_ty.clone()); + value_stack.push_back(*local_ty); } // local.set [t] -> [0] 0x21 => { @@ -180,7 +180,7 @@ fn read_instructions( } } -fn validate_value_stack(return_ty: ResultType, mut f: F) -> Result<()> +fn validate_value_stack(return_ty: ResultType, f: F) -> Result<()> where F: FnOnce(&mut VecDeque) -> Result<()>, { diff --git a/src/validation/mod.rs b/src/validation/mod.rs index da1c06b5..aecb0b7d 100644 --- a/src/validation/mod.rs +++ b/src/validation/mod.rs @@ -46,89 +46,89 @@ pub fn validate(wasm: &[u8]) -> Result { let mut header = None; read_next_header(&mut wasm, &mut header)?; - let mut skip_section = |wasm: &mut WasmReader, section_header: &mut Option| { + let skip_section = |wasm: &mut WasmReader, section_header: &mut Option| { handle_section(wasm, section_header, SectionTy::Custom, |wasm, h| { wasm.skip(h.contents.len()) }) }; - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let types = handle_section(&mut wasm, &mut header, SectionTy::Type, |wasm, _| { - wasm.read_vec(|wasm| FuncType::read(wasm)) + wasm.read_vec(FuncType::read) })? .unwrap_or_default(); - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let imports = handle_section(&mut wasm, &mut header, SectionTy::Import, |wasm, _| { - wasm.read_vec(|wasm| Import::read(wasm)) + wasm.read_vec(Import::read) })? .unwrap_or_default(); - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let functions = handle_section(&mut wasm, &mut header, SectionTy::Function, |wasm, _| { wasm.read_vec(|wasm| wasm.read_var_u32().map(|u| u as usize)) })? .unwrap_or_default(); - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let tables = handle_section(&mut wasm, &mut header, SectionTy::Table, |wasm, _| { - wasm.read_vec(|wasm| TableType::read(wasm)) + wasm.read_vec(TableType::read) })? .unwrap_or_default(); - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let memories = handle_section(&mut wasm, &mut header, SectionTy::Memory, |wasm, _| { - wasm.read_vec(|wasm| MemType::read(wasm)) + wasm.read_vec(MemType::read) })? .unwrap_or_default(); if memories.len() > 1 { return Err(Error::MoreThanOneMemory); } - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let globals = handle_section(&mut wasm, &mut header, SectionTy::Global, |wasm, _| { wasm.read_vec(|wasm| { - let global = Global::read(wasm); + // TODO validate instructions in `global.init_expr`. Furthermore all of these instructions need to be constant. // See https://webassembly.github.io/spec/core/valid/instructions.html#valid-constant // Maybe we can also execute constant expressions right here so they do not even exist in the runtime environment. <-- Needs further research to check if this is even possible - global + Global::read(wasm) }) })? .unwrap_or_default(); - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let exports = handle_section(&mut wasm, &mut header, SectionTy::Export, |wasm, _| { - wasm.read_vec(|wasm| Export::read(wasm)) + wasm.read_vec(Export::read) })? .unwrap_or_default(); - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let start = handle_section(&mut wasm, &mut header, SectionTy::Start, |wasm, _| { wasm.read_var_u32().map(|idx| idx as FuncIdx) })?; - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let _: Option<()> = handle_section(&mut wasm, &mut header, SectionTy::Element, |_, _| { todo!("element section not yet supported") })?; - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let _: Option<()> = handle_section(&mut wasm, &mut header, SectionTy::DataCount, |_, _| { todo!("data count section not yet supported") })?; - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let func_blocks = handle_section(&mut wasm, &mut header, SectionTy::Code, |wasm, h| { code::validate_code_section(wasm, h, &types, &globals) @@ -137,13 +137,13 @@ pub fn validate(wasm: &[u8]) -> Result { assert_eq!(func_blocks.len(), functions.len(), "these should be equal"); // TODO check if this is in the spec - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} let _: Option<()> = handle_section(&mut wasm, &mut header, SectionTy::Data, |_, _| { todo!("data section not yet supported") })?; - while let Some(_) = skip_section(&mut wasm, &mut header)? {} + while (skip_section(&mut wasm, &mut header)?).is_some() {} // All sections should have been handled if let Some(header) = header { @@ -166,7 +166,7 @@ pub fn validate(wasm: &[u8]) -> Result { } fn read_next_header(wasm: &mut WasmReader, header: &mut Option) -> Result<()> { - if header.is_none() && wasm.remaining_bytes().len() > 0 { + if header.is_none() && !wasm.remaining_bytes().is_empty() { *header = Some(SectionHeader::read(wasm)?); } Ok(()) diff --git a/tests/add_one.rs b/tests/add_one.rs index 94444b4a..d8e766bd 100644 --- a/tests/add_one.rs +++ b/tests/add_one.rs @@ -11,7 +11,7 @@ fn add_one() { i32.add) ) "#; - let wasm_bytes = wat::parse_str(&wat).unwrap(); + let wasm_bytes = wat::parse_str(wat).unwrap(); let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); diff --git a/tests/basic_memory.rs b/tests/basic_memory.rs index f9c60ca8..a7ae37ba 100644 --- a/tests/basic_memory.rs +++ b/tests/basic_memory.rs @@ -15,7 +15,7 @@ fn basic_memory() { i32.load) ) "#; - let wasm_bytes = wat::parse_str(&wat).unwrap(); + let wasm_bytes = wat::parse_str(wat).unwrap(); let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); diff --git a/tests/globals.rs b/tests/globals.rs index b2abf7ac..181676df 100644 --- a/tests/globals.rs +++ b/tests/globals.rs @@ -22,7 +22,7 @@ fn globals() { global.get $my_global) ) "#; - let wasm_bytes = wat::parse_str(&wat).unwrap(); + let wasm_bytes = wat::parse_str(wat).unwrap(); let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); diff --git a/tests/start_function.rs b/tests/start_function.rs index 03b945df..50c3ab4e 100644 --- a/tests/start_function.rs +++ b/tests/start_function.rs @@ -20,7 +20,7 @@ fn start_function() { i32.load) ) "#; - let wasm_bytes = wat::parse_str(&wat).unwrap(); + let wasm_bytes = wat::parse_str(wat).unwrap(); let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");