Skip to content

Commit

Permalink
Merge pull request #2942 from silwol/dev/2926-fix-clippy-warnings-in-…
Browse files Browse the repository at this point in the history
…wasmer3

Fix clippy lints
  • Loading branch information
syrusakbary authored Jun 9, 2022
2 parents 6c7ce0b + 5339f7c commit 66c1c00
Show file tree
Hide file tree
Showing 119 changed files with 864 additions and 831 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C

## **Unreleased**

### Fixed
- [#2942](https://github.com/wasmerio/wasmer/pull/2942) Fix clippy lints.

## 2.3.0 - 2022/06/06

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,11 @@ install-wasmer-headless-minimal:
update-testsuite:
git subtree pull --prefix tests/wast/spec https://github.com/WebAssembly/testsuite.git master --squash

lint-packages: RUSTFLAGS += -D dead-code -D nonstandard-style -D unused-imports -D unused-mut -D unused-variables -D unused-unsafe -D unreachable-patterns -D bad-style -D improper-ctypes -D unused-allocation -D unused-comparisons -D while-true -D unconditional-recursion -D bare-trait-objects # TODO: add `-D missing-docs` # TODO: add `-D function_item_references` (not available on Rust 1.47, try when upgrading)
lint-packages: RUSTFLAGS += -D dead-code -D nonstandard-style -D unused-imports -D unused-mut -D unused-variables -D unused-unsafe -D unreachable-patterns -D bad-style -D improper-ctypes -D unused-allocation -D unused-comparisons -D while-true -D unconditional-recursion -D bare-trait-objects -D function_item_references # TODO: add `-D missing-docs`
lint-packages:
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --all $(exclude_tests)
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path lib/cli/Cargo.toml $(compiler_features)
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path fuzz/Cargo.toml $(compiler_features)
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --all -- -D clippy::all
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path lib/cli/Cargo.toml $(compiler_features) -- -D clippy::all
RUSTFLAGS="${RUSTFLAGS}" cargo clippy --manifest-path fuzz/Cargo.toml $(compiler_features) -- -D clippy::all

lint-formatting:
cargo fmt --all -- --check
Expand Down
8 changes: 3 additions & 5 deletions lib/api/src/js/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ impl<T, M: MemorySize> WasmPtr<T, M> {
///
/// This method returns an error if an address overflow occurs.
#[inline]
#[must_use]
pub fn add(self, offset: M::Offset) -> Result<Self, MemoryAccessError> {
pub fn add_offset(self, offset: M::Offset) -> Result<Self, MemoryAccessError> {
let base = self.offset.into();
let index = offset.into();
let offset = index
Expand All @@ -164,8 +163,7 @@ impl<T, M: MemorySize> WasmPtr<T, M> {
///
/// This method returns an error if an address overflow occurs.
#[inline]
#[must_use]
pub fn sub(self, offset: M::Offset) -> Result<Self, MemoryAccessError> {
pub fn sub_offset(self, offset: M::Offset) -> Result<Self, MemoryAccessError> {
let base = self.offset.into();
let index = offset.into();
let offset = index
Expand Down Expand Up @@ -226,7 +224,7 @@ impl<T: ValueType, M: MemorySize> WasmPtr<T, M> {
let mut vec = Vec::new();
for i in 0u64.. {
let i = M::Offset::try_from(i).map_err(|_| MemoryAccessError::Overflow)?;
let val = self.add(i)?.deref(memory).read()?;
let val = self.add_offset(i)?.deref(memory).read()?;
if end(&val) {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
missing_docs,
trivial_numeric_casts,
unused_extern_crates,
broken_intra_doc_links
rustdoc::broken_intra_doc_links
)]
#![warn(unused_import_braces)]
#![cfg_attr(
feature = "cargo-clippy",
allow(clippy::new_without_default, vtable_address_comparisons)
allow(clippy::new_without_default, clippy::vtable_address_comparisons)
)]
#![cfg_attr(
feature = "cargo-clippy",
Expand Down
12 changes: 6 additions & 6 deletions lib/api/src/sys/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Exports {
T: ExportableWithGenerics<'a, Args, Rets>,
{
let mut out: T = self.get_with_generics(name)?;
out.into_weak_instance_ref();
out.convert_to_weak_instance_ref();
Ok(out)
}

Expand Down Expand Up @@ -279,7 +279,7 @@ impl IntoIterator for Exports {
type Item = (String, Extern);

fn into_iter(self) -> Self::IntoIter {
self.map.clone().into_iter()
self.map.into_iter()
}
}

Expand Down Expand Up @@ -311,7 +311,7 @@ pub trait Exportable<'a>: Sized {
/// Convert the extern internally to hold a weak reference to the `InstanceRef`.
/// This is useful for preventing cycles, for example for data stored in a
/// type implementing `WasmerEnv`.
fn into_weak_instance_ref(&mut self);
fn convert_to_weak_instance_ref(&mut self);
}

/// A trait for accessing exports (like [`Exportable`]) but it takes generic
Expand All @@ -323,7 +323,7 @@ pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Si
/// Convert the extern internally to hold a weak reference to the `InstanceRef`.
/// This is useful for preventing cycles, for example for data stored in a
/// type implementing `WasmerEnv`.
fn into_weak_instance_ref(&mut self);
fn convert_to_weak_instance_ref(&mut self);
}

/// We implement it for all concrete [`Exportable`] types (that are `Clone`)
Expand All @@ -333,7 +333,7 @@ impl<'a, T: Exportable<'a> + Clone + 'static> ExportableWithGenerics<'a, (), ()>
T::get_self_from_extern(_extern).map(|i| i.clone())
}

fn into_weak_instance_ref(&mut self) {
<Self as Exportable>::into_weak_instance_ref(self);
fn convert_to_weak_instance_ref(&mut self) {
<Self as Exportable>::convert_to_weak_instance_ref(self);
}
}
23 changes: 13 additions & 10 deletions lib/api/src/sys/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ pub struct Function {
impl wasmer_types::WasmValueType for Function {
/// Write the value.
unsafe fn write_value_to(&self, p: *mut i128) {
let func_ref =
Val::into_vm_funcref(&Val::FuncRef(Some(self.clone())), &self.store).unwrap();
let func_ref = Val::into_vm_funcref(Val::FuncRef(Some(self.clone())), &self.store).unwrap();
std::ptr::write(p as *mut VMFuncRef, func_ref);
}

Expand Down Expand Up @@ -533,7 +532,7 @@ impl Function {
VMFunctionKind::Dynamic => unsafe {
type VMContextWithEnv = VMDynamicFunctionContext<DynamicFunction<std::ffi::c_void>>;
let ctx = self.exported.vm_function.vmctx.host_env as *mut VMContextWithEnv;
Ok((*ctx).ctx.call(&params)?.into_boxed_slice())
Ok((*ctx).ctx.call(params)?.into_boxed_slice())
},
VMFunctionKind::Static => {
unimplemented!(
Expand Down Expand Up @@ -704,12 +703,10 @@ impl<'a> Exportable<'a> for Function {
}
}

fn into_weak_instance_ref(&mut self) {
self.exported
.vm_function
.instance_ref
.as_mut()
.map(|v| *v = v.downgrade());
fn convert_to_weak_instance_ref(&mut self) {
if let Some(v) = self.exported.vm_function.instance_ref.as_mut() {
*v = v.downgrade();
}
}
}

Expand Down Expand Up @@ -768,7 +765,7 @@ where
Env: Sized + 'static + Send + Sync,
{
fn call(&self, args: &[Val]) -> Result<Vec<Val>, RuntimeError> {
(*self.func)(&*self.env, &args)
(*self.func)(&*self.env, args)
}
fn function_type(&self) -> &FunctionType {
&self.function_type
Expand Down Expand Up @@ -864,6 +861,10 @@ mod inner {
/// `FromNativeWasmType` and `ToNativeWasmType` but it creates a
/// non-negligible complexity in the `WasmTypeList`
/// implementation.
///
/// # Safety
/// This trait is unsafe given the nature of how values are written and read from the native
/// stack
pub unsafe trait FromToNativeWasmType
where
Self: Sized,
Expand Down Expand Up @@ -1219,6 +1220,7 @@ mod inner {

type Array = [i128; count_idents!( $( $x ),* )];

#[allow(clippy::unused_unit)]
fn from_array(array: Self::Array) -> Self {
// Unpack items of the array.
#[allow(non_snake_case)]
Expand Down Expand Up @@ -1254,6 +1256,7 @@ mod inner {
[0; count_idents!( $( $x ),* )]
}

#[allow(clippy::unused_unit)]
fn from_c_struct(c_struct: Self::CStruct) -> Self {
// Unpack items of the C structure.
#[allow(non_snake_case)]
Expand Down
9 changes: 4 additions & 5 deletions lib/api/src/sys/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,9 @@ impl<'a> Exportable<'a> for Global {
}
}

fn into_weak_instance_ref(&mut self) {
self.vm_global
.instance_ref
.as_mut()
.map(|v| *v = v.downgrade());
fn convert_to_weak_instance_ref(&mut self) {
if let Some(v) = self.vm_global.instance_ref.as_mut() {
*v = v.downgrade();
}
}
}
15 changes: 7 additions & 8 deletions lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Memory {
.checked_add(buf.len() as u64)
.ok_or(MemoryAccessError::Overflow)?;
if end > def.current_length.try_into().unwrap() {
Err(MemoryAccessError::HeapOutOfBounds)?;
return Err(MemoryAccessError::HeapOutOfBounds);
}
unsafe {
volatile_memcpy_read(def.base.add(offset as usize), buf.as_mut_ptr(), buf.len());
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Memory {
.checked_add(buf.len() as u64)
.ok_or(MemoryAccessError::Overflow)?;
if end > def.current_length.try_into().unwrap() {
Err(MemoryAccessError::HeapOutOfBounds)?;
return Err(MemoryAccessError::HeapOutOfBounds);
}
let buf_ptr = buf.as_mut_ptr() as *mut u8;
unsafe {
Expand All @@ -269,7 +269,7 @@ impl Memory {
.checked_add(data.len() as u64)
.ok_or(MemoryAccessError::Overflow)?;
if end > def.current_length.try_into().unwrap() {
Err(MemoryAccessError::HeapOutOfBounds)?;
return Err(MemoryAccessError::HeapOutOfBounds);
}
unsafe {
volatile_memcpy_write(data.as_ptr(), def.base.add(offset as usize), data.len());
Expand Down Expand Up @@ -302,11 +302,10 @@ impl<'a> Exportable<'a> for Memory {
}
}

fn into_weak_instance_ref(&mut self) {
self.vm_memory
.instance_ref
.as_mut()
.map(|v| *v = v.downgrade());
fn convert_to_weak_instance_ref(&mut self) {
if let Some(v) = self.vm_memory.instance_ref.as_mut() {
*v = v.downgrade();
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/api/src/sys/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ impl<'a> Exportable<'a> for Extern {
Ok(_extern)
}

fn into_weak_instance_ref(&mut self) {
fn convert_to_weak_instance_ref(&mut self) {
match self {
Self::Function(f) => f.into_weak_instance_ref(),
Self::Global(g) => g.into_weak_instance_ref(),
Self::Memory(m) => m.into_weak_instance_ref(),
Self::Table(t) => t.into_weak_instance_ref(),
Self::Function(f) => f.convert_to_weak_instance_ref(),
Self::Global(g) => g.convert_to_weak_instance_ref(),
Self::Memory(m) => m.convert_to_weak_instance_ref(),
Self::Table(t) => t.convert_to_weak_instance_ref(),
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions lib/api/src/sys/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ impl<'a> Exportable<'a> for Table {
}
}

fn into_weak_instance_ref(&mut self) {
self.vm_table
.instance_ref
.as_mut()
.map(|v| *v = v.downgrade());
fn convert_to_weak_instance_ref(&mut self) {
if let Some(v) = self.vm_table.instance_ref.as_mut() {
*v = v.downgrade();
}
}
}
2 changes: 1 addition & 1 deletion lib/api/src/sys/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Instance {
/// * Runtime errors that happen when running the module `start` function.
pub fn new_by_index(module: &Module, externs: &[Extern]) -> Result<Self, InstantiationError> {
let store = module.store();
let imports = externs.iter().cloned().collect::<Vec<_>>();
let imports = externs.to_vec();
let handle = module.instantiate(&imports)?;
let exports = module
.exports()
Expand Down
14 changes: 10 additions & 4 deletions lib/api/src/sys/mem_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ pub enum MemoryAccessError {

impl From<MemoryAccessError> for RuntimeError {
fn from(err: MemoryAccessError) -> Self {
RuntimeError::new(err.to_string())
Self::new(err.to_string())
}
}
impl From<FromUtf8Error> for MemoryAccessError {
fn from(_err: FromUtf8Error) -> Self {
MemoryAccessError::NonUtf8String
Self::NonUtf8String
}
}

Expand Down Expand Up @@ -191,6 +191,12 @@ impl<'a, T: ValueType> WasmSlice<'a, T> {
self.len
}

/// Returns `true` if the number of elements is 0.
#[inline]
pub fn is_empty(self) -> bool {
self.len == 0
}

/// Get a reference to the Wasm memory backing this reference.
#[inline]
pub fn memory(self) -> &'a Memory {
Expand Down Expand Up @@ -343,7 +349,7 @@ impl<'a, T: ValueType> Iterator for WasmSliceIter<'a, T> {
type Item = WasmRef<'a, T>;

fn next(&mut self) -> Option<Self::Item> {
if self.slice.len() != 0 {
if !self.slice.is_empty() {
let elem = self.slice.index(0);
self.slice = self.slice.subslice(1..self.slice.len());
Some(elem)
Expand All @@ -359,7 +365,7 @@ impl<'a, T: ValueType> Iterator for WasmSliceIter<'a, T> {

impl<'a, T: ValueType> DoubleEndedIterator for WasmSliceIter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.slice.len() != 0 {
if !self.slice.is_empty() {
let elem = self.slice.index(self.slice.len() - 1);
self.slice = self.slice.subslice(0..self.slice.len() - 1);
Some(elem)
Expand Down
9 changes: 4 additions & 5 deletions lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,10 @@ impl Module {
pub fn set_name(&mut self, name: &str) -> bool {
Arc::get_mut(&mut self.artifact)
.and_then(|artifact| artifact.module_mut())
.map(|mut module_info| {
.map_or(false, |mut module_info| {
module_info.name = Some(name.to_string());
true
})
.unwrap_or(false)
}

/// Returns an iterator over the imported types in the Module.
Expand All @@ -376,7 +375,7 @@ impl Module {
/// # Ok(())
/// # }
/// ```
pub fn imports<'a>(&'a self) -> ImportsIterator<impl Iterator<Item = ImportType> + 'a> {
pub fn imports(&self) -> ImportsIterator<impl Iterator<Item = ImportType> + '_> {
self.artifact.module_ref().imports()
}

Expand All @@ -403,7 +402,7 @@ impl Module {
/// # Ok(())
/// # }
/// ```
pub fn exports<'a>(&'a self) -> ExportsIterator<impl Iterator<Item = ExportType> + 'a> {
pub fn exports(&self) -> ExportsIterator<impl Iterator<Item = ExportType> + '_> {
self.artifact.module_ref().exports()
}

Expand All @@ -430,7 +429,7 @@ impl Module {
/// However, the usage is highly discouraged.
#[doc(hidden)]
pub fn info(&self) -> &ModuleInfo {
&self.artifact.module_ref()
self.artifact.module_ref()
}

/// Gets the [`Artifact`] used internally by the Module.
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/sys/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ macro_rules! impl_native_traits {
Rets: WasmTypeList,
{
/// Call the typed func and return results.
#[allow(clippy::too_many_arguments)]
pub fn call(&self, $( $x: $x, )* ) -> Result<Rets, RuntimeError> {
if !self.is_host() {
// We assume the trampoline is always going to be present for
Expand Down Expand Up @@ -233,7 +234,7 @@ macro_rules! impl_native_traits {
crate::Function::get_self_from_extern(_extern)?.native().map_err(|_| crate::sys::exports::ExportError::IncompatibleType)
}

fn into_weak_instance_ref(&mut self) {
fn convert_to_weak_instance_ref(&mut self) {
self.exported.vm_function.instance_ref.as_mut().map(|v| *v = v.downgrade());
}
}
Expand Down
Loading

0 comments on commit 66c1c00

Please sign in to comment.