From 5a7ad9f7cd793db4a1f52e764c55de7c9b58ab22 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sat, 13 Mar 2021 20:01:25 +0000 Subject: [PATCH] Fix some clippy warnings & minor changes --- src/ffi/compat53.rs | 1 + src/lua.rs | 9 +++++---- src/scope.rs | 11 ++++++----- src/userdata.rs | 8 ++++++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ffi/compat53.rs b/src/ffi/compat53.rs index ecaa21d..93c24c8 100644 --- a/src/ffi/compat53.rs +++ b/src/ffi/compat53.rs @@ -250,6 +250,7 @@ end #[cfg(any(feature = "lua51", feature = "luajit"))] pub unsafe fn lua_arith(L: *mut lua_State, op: c_int) { + #[allow(clippy::manual_range_contains)] if op < LUA_OPADD || op > LUA_OPUNM { luaL_error(L, cstr!("invalid 'op' argument for lua_arith")); } diff --git a/src/lua.rs b/src/lua.rs index e06635c..d00dc47 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -1557,7 +1557,7 @@ impl Lua { let mut extra_tables_count = 0; let mut field_getters_index = None; - let has_field_getters = fields.field_getters.len() > 0; + let has_field_getters = !fields.field_getters.is_empty(); if has_field_getters { protect_lua_closure(self.state, 0, 1, |state| { ffi::lua_newtable(state); @@ -1575,7 +1575,7 @@ impl Lua { } let mut field_setters_index = None; - let has_field_setters = fields.field_setters.len() > 0; + let has_field_setters = !fields.field_setters.is_empty(); if has_field_setters { protect_lua_closure(self.state, 0, 1, |state| { ffi::lua_newtable(state); @@ -1594,9 +1594,9 @@ impl Lua { let mut methods_index = None; #[cfg(feature = "async")] - let has_methods = methods.methods.len() > 0 || methods.async_methods.len() > 0; + let has_methods = !methods.methods.is_empty() || !methods.async_methods.is_empty(); #[cfg(not(feature = "async"))] - let has_methods = methods.methods.len() > 0; + let has_methods = !methods.methods.is_empty(); if has_methods { protect_lua_closure(self.state, 0, 1, |state| { ffi::lua_newtable(state); @@ -2546,6 +2546,7 @@ impl<'lua, T: 'static + UserData> StaticUserDataMethods<'lua, T> { struct StaticUserDataFields<'lua, T: 'static + UserData> { field_getters: Vec<(Vec, Callback<'lua, 'static>)>, field_setters: Vec<(Vec, Callback<'lua, 'static>)>, + #[allow(clippy::type_complexity)] meta_fields: Vec<( MetaMethod, Box Result> + 'static>, diff --git a/src/scope.rs b/src/scope.rs index 6ebd521..40ad590 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -258,7 +258,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { if ffi::lua_getmetatable(lua.state, -1) == 0 { return Err(Error::UserDataTypeMismatch); } - ffi::lua_pushstring(lua.state, cstr!("__mlua")); + ffi::lua_pushstring(lua.state, cstr!("__mlua_ptr")); if ffi::lua_rawget(lua.state, -2) == ffi::LUA_TLIGHTUSERDATA { let ud_ptr = ffi::lua_touserdata(lua.state, -1); if ud_ptr == check_data.as_ptr() as *mut c_void { @@ -330,7 +330,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { ffi::lua_newtable(state); // Add internal metamethod to store reference to the data - ffi::lua_pushstring(state, cstr!("__mlua")); + ffi::lua_pushstring(state, cstr!("__mlua_ptr")); ffi::lua_pushlightuserdata(lua.state, data.as_ptr() as *mut c_void); ffi::lua_rawset(state, -3); })?; @@ -353,7 +353,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { let metatable_index = ffi::lua_absindex(lua.state, -1); let mut field_getters_index = None; - if ud_fields.field_getters.len() > 0 { + if !ud_fields.field_getters.is_empty() { protect_lua_closure(lua.state, 0, 1, |state| { ffi::lua_newtable(state); })?; @@ -369,7 +369,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { } let mut field_setters_index = None; - if ud_fields.field_setters.len() > 0 { + if !ud_fields.field_setters.is_empty() { protect_lua_closure(lua.state, 0, 1, |state| { ffi::lua_newtable(state); })?; @@ -385,7 +385,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { } let mut methods_index = None; - if ud_methods.methods.len() > 0 { + if !ud_methods.methods.is_empty() { // Create table used for methods lookup protect_lua_closure(lua.state, 0, 1, |state| { ffi::lua_newtable(state); @@ -738,6 +738,7 @@ impl<'lua, T: UserData> UserDataMethods<'lua, T> for NonStaticUserDataMethods<'l struct NonStaticUserDataFields<'lua, T: UserData> { field_getters: Vec<(Vec, NonStaticMethod<'lua, T>)>, field_setters: Vec<(Vec, NonStaticMethod<'lua, T>)>, + #[allow(clippy::type_complexity)] meta_fields: Vec<(MetaMethod, Box Result>>)>, } diff --git a/src/userdata.rs b/src/userdata.rs index ef8cbf4..12b4518 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -187,7 +187,9 @@ impl MetaMethod { MetaMethod::Custom(name) if name == "__metatable" => { Err(Error::MetaMethodRestricted(name)) } - MetaMethod::Custom(name) if name == "__mlua" => Err(Error::MetaMethodRestricted(name)), + MetaMethod::Custom(name) if name.starts_with("__mlua") => { + Err(Error::MetaMethodRestricted(name)) + } _ => Ok(self), } } @@ -714,7 +716,9 @@ impl<'lua> AnyUserData<'lua> { /// Returns a metatable of this `UserData`. /// /// Returned [`UserDataMetatable`] object wraps the original metatable and - /// allows to provide safe access to it methods. + /// provides safe access to it methods. + /// + /// For `T: UserData + 'static` returned metatable is shared among all instances of type `T`. /// /// [`UserDataMetatable`]: struct.UserDataMetatable.html pub fn get_metatable(&self) -> Result> {