Add clippy check and fix clippy warnings

This commit is contained in:
Alex Orlenko 2020-05-12 23:34:42 +01:00
parent 1492790ece
commit 9f2d598517
13 changed files with 64 additions and 52 deletions

View File

@ -82,3 +82,18 @@ jobs:
components: rustfmt
override: true
- run: cargo fmt -- --check
clippy:
name: Clippy check
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: clippy
override: true
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --features async,send,vendored

View File

@ -1,14 +1,13 @@
use std::env;
use std::ffi::OsString;
use std::fs::File;
use std::io::{BufRead, BufReader, Result};
use std::ops::Bound;
use std::path::{Path, PathBuf};
pub fn probe_lua() -> PathBuf {
let include_dir = env::var_os("LUA_INC").unwrap_or(OsString::new());
let lib_dir = env::var_os("LUA_LIB").unwrap_or(OsString::new());
let lua_lib = env::var_os("LUA_LIB_NAME").unwrap_or(OsString::new());
let include_dir = env::var_os("LUA_INC").unwrap_or_default();
let lib_dir = env::var_os("LUA_LIB").unwrap_or_default();
let lua_lib = env::var_os("LUA_LIB_NAME").unwrap_or_default();
println!("cargo:rerun-if-env-changed=LUA_INC");
println!("cargo:rerun-if-env-changed=LUA_LIB");
@ -32,7 +31,7 @@ pub fn probe_lua() -> PathBuf {
lua = pkg_config::Config::new().probe("lua5.4");
}
return lua.unwrap().include_paths[0].clone();
lua.unwrap().include_paths[0].clone()
}
#[cfg(feature = "lua53")]
@ -45,7 +44,7 @@ pub fn probe_lua() -> PathBuf {
lua = pkg_config::Config::new().probe("lua5.3");
}
return lua.unwrap().include_paths[0].clone();
lua.unwrap().include_paths[0].clone()
}
#[cfg(feature = "lua52")]
@ -58,7 +57,7 @@ pub fn probe_lua() -> PathBuf {
lua = pkg_config::Config::new().probe("lua5.2");
}
return lua.unwrap().include_paths[0].clone();
lua.unwrap().include_paths[0].clone()
}
#[cfg(feature = "lua51")]
@ -71,7 +70,7 @@ pub fn probe_lua() -> PathBuf {
lua = pkg_config::Config::new().probe("lua5.1");
}
return lua.unwrap().include_paths[0].clone();
lua.unwrap().include_paths[0].clone()
}
#[cfg(feature = "luajit")]
@ -80,7 +79,7 @@ pub fn probe_lua() -> PathBuf {
.range_version((Bound::Included("2.1.0"), Bound::Unbounded))
.probe("luajit");
return lua.unwrap().include_paths[0].clone();
lua.unwrap().include_paths[0].clone()
}
}
@ -100,10 +99,10 @@ fn use_custom_lua<S: AsRef<Path>>(include_dir: &S, lib_dir: &S, lua_lib: &S) ->
}
}
let mut link_lib = String::new();
if env::var("LUA_LINK").unwrap_or(String::new()) == "static" {
link_lib = "static=".to_string();
}
let link_lib = match env::var("LUA_LINK") {
Ok(s) if s == "static" => "static=",
_ => "",
};
println!(
"cargo:rustc-link-search=native={}",

View File

@ -1,15 +1,5 @@
use std::path::PathBuf;
#[cfg(any(
feature = "lua54",
feature = "lua53",
feature = "lua52",
feature = "lua51"
))]
use lua_src;
#[cfg(feature = "luajit")]
use luajit_src;
pub fn probe_lua() -> PathBuf {
#[cfg(feature = "lua54")]
let artifacts = lua_src::Build::new().build(lua_src::Lua54);

View File

@ -379,7 +379,7 @@ where
{
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
Ok(Value::Table(
lua.create_sequence_from(self.into_iter().cloned())?,
lua.create_sequence_from(self.iter().cloned())?,
))
}
}

View File

@ -156,7 +156,7 @@ int write_str_item(FILE *f, const char *name, const char *value) {
char *buf = malloc(bufsz);
int ret;
escape(value, buf, bufsz);
ret = fprintf(f, "pub const %s: &'static str = \"%s\";\n", name, buf);
ret = fprintf(f, "pub const %s: &str = \"%s\";\n", name, buf);
free(buf);
return ret;
}

View File

@ -38,7 +38,7 @@ pub use super::glue::LUA_REGISTRYINDEX;
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub use super::glue::{LUA_ENVIRONINDEX, LUA_GLOBALSINDEX};
pub const LUA_SIGNATURE: &'static [u8] = b"\x1bLua";
pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
// option for multiple returns in 'lua_pcall' and 'lua_call'
pub const LUA_MULTRET: c_int = -1;

View File

@ -276,6 +276,7 @@ pub const SYS_MIN_ALIGN: usize = 8;
pub const SYS_MIN_ALIGN: usize = 16;
#[allow(unused_imports, dead_code, non_camel_case_types)]
#[allow(clippy::unreadable_literal)]
mod glue {
include!(concat!(env!("OUT_DIR"), "/glue.rs"));
}

View File

@ -111,8 +111,10 @@ impl Drop for Lua {
impl Lua {
/// Creates a new Lua state and loads the safe subset of the standard libraries.
///
/// # Safety
/// The created Lua state would have safety guarantees and would not allow to load unsafe
/// standard libraries or C modules.
#[allow(clippy::new_without_default)]
pub fn new() -> Lua {
mlua_expect!(
Self::new_with(StdLib::ALL_SAFE),
@ -122,6 +124,7 @@ impl Lua {
/// Creates a new Lua state and loads all the standard libraries.
///
/// # Safety
/// The created Lua state would not have safety guarantees and would allow to load C modules.
pub unsafe fn unsafe_new() -> Lua {
Self::unsafe_new_with(StdLib::ALL)
@ -131,6 +134,7 @@ impl Lua {
///
/// Use the [`StdLib`] flags to specifiy the libraries you want to load.
///
/// # Safety
/// The created Lua state would have safety guarantees and would not allow to load unsafe
/// standard libraries or C modules.
///
@ -162,6 +166,7 @@ impl Lua {
///
/// Use the [`StdLib`] flags to specifiy the libraries you want to load.
///
/// # Safety
/// The created Lua state would not have safety guarantees and would allow to load C modules.
///
/// [`StdLib`]: struct.StdLib.html
@ -244,7 +249,8 @@ impl Lua {
lua
}
/// Constructs a new Lua instance from the existing state.
/// Constructs a new Lua instance from an existing raw state.
#[allow(clippy::missing_safety_doc)]
pub unsafe fn init_from_ptr(state: *mut ffi::lua_State) -> Lua {
let main_state = get_main_state(state);
let main_state_top = ffi::lua_gettop(state);
@ -297,8 +303,8 @@ impl Lua {
Lua {
state,
main_state: main_state,
extra: extra,
main_state,
extra,
ephemeral: true,
safe: false,
_no_ref_unwind_safe: PhantomData,
@ -495,7 +501,7 @@ impl Lua {
unsafe { ffi::lua_gc(self.main_state, ffi::LUA_GCSETSTEPMUL, step_multiplier) };
}
let _ = step_size; // Ignored
return GCMode::Incremental;
GCMode::Incremental
}
#[cfg(feature = "lua54")]
@ -850,7 +856,7 @@ impl Lua {
/// Returns a handle to the active `Thread`. For calls to `Lua` this will be the main Lua thread,
/// for parameters given to a callback, this will be whatever Lua thread called the callback.
pub fn current_thread<'lua>(&'lua self) -> Thread<'lua> {
pub fn current_thread(&self) -> Thread {
unsafe {
ffi::lua_pushthread(self.state);
Thread(self.pop_ref())
@ -917,9 +923,10 @@ impl Lua {
assert_stack(self.state, 4);
self.push_value(v)?;
if protect_lua_closure(self.state, 1, 1, |state| {
let ok = protect_lua_closure(self.state, 1, 1, |state| {
!ffi::lua_tostring(state, -1).is_null()
})? {
})?;
if ok {
Some(String(self.pop_ref()))
} else {
None
@ -1294,7 +1301,7 @@ impl Lua {
// used stack. The implementation is somewhat biased towards the use case of a relatively small
// number of short term references being created, and `RegistryKey` being used for long term
// references.
pub(crate) unsafe fn pop_ref<'lua>(&'lua self) -> LuaRef<'lua> {
pub(crate) unsafe fn pop_ref(&self) -> LuaRef {
let mut extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
ffi::lua_xmove(self.state, extra.ref_thread, 1);
let index = ref_stack_pop(&mut extra);

View File

@ -31,6 +31,7 @@ use {
/// See [`Lua::scope`] for more details.
///
/// [`Lua::scope`]: struct.Lua.html#method.scope
#[allow(clippy::type_complexity)]
pub struct Scope<'lua, 'scope> {
lua: &'lua Lua,
destructors: RefCell<Vec<(LuaRef<'lua>, fn(LuaRef<'lua>) -> Vec<Box<dyn Any>>)>>,
@ -229,21 +230,17 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
match method {
NonStaticMethod::Method(method) => {
let method_data = data.clone();
let f = Box::new(move |lua, mut args: MultiValue<'callback>| {
if !check_ud_type(lua, args.pop_front()) {
return Err(Error::UserDataTypeMismatch);
}
let data = method_data
.try_borrow()
.map_err(|_| Error::UserDataBorrowError)?;
let data = data.try_borrow().map_err(|_| Error::UserDataBorrowError)?;
method(lua, &*data, args)
});
unsafe { scope.create_callback(f) }
}
NonStaticMethod::MethodMut(method) => {
let method = RefCell::new(method);
let method_data = data.clone();
let f = Box::new(move |lua, mut args: MultiValue<'callback>| {
if !check_ud_type(lua, args.pop_front()) {
return Err(Error::UserDataTypeMismatch);
@ -251,7 +248,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> {
let mut method = method
.try_borrow_mut()
.map_err(|_| Error::RecursiveMutCallback)?;
let mut data = method_data
let mut data = data
.try_borrow_mut()
.map_err(|_| Error::UserDataBorrowMutError)?;
(&mut *method)(lua, &mut *data, args)

View File

@ -7,7 +7,7 @@ pub struct StdLib(u32);
impl StdLib {
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
pub const COROUTINE: StdLib = StdLib(1 << 0);
pub const COROUTINE: StdLib = StdLib(1);
pub const TABLE: StdLib = StdLib(1 << 1);
pub const IO: StdLib = StdLib(1 << 2);
pub const OS: StdLib = StdLib(1 << 3);

View File

@ -15,6 +15,7 @@ use {futures_core::future::LocalBoxFuture, futures_util::future};
#[derive(Clone, Debug)]
pub struct Table<'lua>(pub(crate) LuaRef<'lua>);
#[allow(clippy::len_without_is_empty)]
impl<'lua> Table<'lua> {
/// Sets a key-value pair in the table.
///
@ -627,9 +628,10 @@ where
lua.push_ref(&self.table);
lua.push_value(next_key)?;
if protect_lua_closure(lua.state, 2, ffi::LUA_MULTRET, |state| {
let next = protect_lua_closure(lua.state, 2, ffi::LUA_MULTRET, |state| {
ffi::lua_next(state, -2) != 0
})? {
})?;
if next {
ffi::lua_pushvalue(lua.state, -2);
let key = lua.pop_value();
let value = lua.pop_value();

View File

@ -57,10 +57,11 @@ impl Drop for StackGuard {
fn drop(&mut self) {
unsafe {
let top = ffi::lua_gettop(self.state);
if top < self.top {
mlua_panic!("{} too many stack values popped", self.top - top)
}
if top > self.top {
ffi::lua_settop(self.state, self.top);
} else if top < self.top {
mlua_panic!("{} too many stack values popped", self.top - top);
}
}
}
@ -473,12 +474,12 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
);
get_gc_metatable_for::<WrappedError>(state);
ffi::lua_setmetatable(state, -2);
} else if let None = get_gc_userdata::<WrappedPanic>(state, -1).as_ref() {
if ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0 {
let s = ffi::luaL_tolstring(state, -1, ptr::null_mut());
ffi::luaL_traceback(state, state, s, 0);
ffi::lua_remove(state, -2);
}
} else if get_gc_userdata::<WrappedPanic>(state, -1).is_null()
&& ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0
{
let s = ffi::luaL_tolstring(state, -1, ptr::null_mut());
ffi::luaL_traceback(state, state, s, 0);
ffi::lua_remove(state, -2);
}
1
}

View File

@ -159,7 +159,7 @@ impl<'a, 'lua> IntoIterator for &'a MultiValue<'lua> {
type IntoIter = iter::Rev<slice::Iter<'a, Value<'lua>>>;
fn into_iter(self) -> Self::IntoIter {
(&self.0).into_iter().rev()
(&self.0).iter().rev()
}
}