ci: add cargo clippy

This commit is contained in:
LongYinan 2020-12-01 14:55:19 +08:00
parent 96f3a45709
commit c184ab3926
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
17 changed files with 50 additions and 32 deletions

View file

@ -25,6 +25,7 @@ jobs:
toolchain: stable
profile: default
override: true
components: rustfmt, clippy
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
@ -55,9 +56,12 @@ jobs:
- name: 'Lint JS/TS'
run: yarn lint
- name: 'Lint rust'
- name: Cargo fmt
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy
- name: Clear the cargo caches
run: |
cargo install cargo-cache --no-default-features --features ci-autoclean

View file

@ -18,8 +18,8 @@ impl Parse for ArgLength {
Ok(ArgLength {
length: vars
.first()
.map(|i| i.clone())
.unwrap_or(Literal::usize_unsuffixed(0)),
.cloned()
.unwrap_or_else(|| Literal::usize_unsuffixed(0)),
})
}
}

View file

@ -32,7 +32,7 @@ impl<'env> AsyncWorkPromise<'env> {
}
#[inline]
pub fn run<'env, T: Task>(env: &'env Env, task: T) -> Result<AsyncWorkPromise<'env>> {
pub fn run<T: Task>(env: &Env, task: T) -> Result<AsyncWorkPromise<'_>> {
let mut raw_resource = ptr::null_mut();
check_status!(unsafe { sys::napi_create_object(env.0, &mut raw_resource) })?;
let mut raw_promise = ptr::null_mut();
@ -86,7 +86,7 @@ unsafe extern "C" fn execute<T: Task>(_env: sys::napi_env, data: *mut c_void) {
let mut work = Box::from_raw(data as *mut AsyncWork<T>);
let _ = mem::replace(
&mut work.value,
work.inner_task.compute().map(|v| mem::MaybeUninit::new(v)),
work.inner_task.compute().map(mem::MaybeUninit::new),
);
Box::leak(work);
}

View file

@ -53,12 +53,10 @@ impl<'env> CallContext<'env> {
status: Status::GenericFailure,
reason: "Arguments index out of range".to_owned(),
})
} else if index < self.length {
unsafe { ArgType::from_raw(self.env.0, self.args[index]) }.map(Either::A)
} else {
if index < self.length {
unsafe { ArgType::from_raw(self.env.0, self.args[index]) }.map(Either::A)
} else {
self.env.get_undefined().map(Either::B)
}
self.env.get_undefined().map(Either::B)
}
}

View file

@ -1,6 +1,6 @@
pub(crate) struct CleanupEnvHookData<T: 'static> {
pub(crate) data: T,
pub(crate) hook: Box<dyn FnOnce(T) -> ()>,
pub(crate) hook: Box<dyn FnOnce(T)>,
}
#[derive(Clone, Copy)]

View file

@ -1,7 +1,6 @@
use std::any::TypeId;
use std::convert::TryInto;
use std::ffi::CString;
use std::mem;
use std::os::raw::{c_char, c_void};
use std::ptr;
@ -37,6 +36,7 @@ pub struct Env(pub(crate) sys::napi_env);
impl Env {
#[inline]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn from_raw(env: sys::napi_env) -> Self {
Env(env)
}
@ -461,9 +461,9 @@ impl Env {
&mut unknown_tagged_object,
))?;
let type_id: *const TypeId = mem::transmute(unknown_tagged_object);
let type_id = unknown_tagged_object as *const TypeId;
if *type_id == TypeId::of::<T>() {
let tagged_object: *mut TaggedObject<T> = mem::transmute(unknown_tagged_object);
let tagged_object = unknown_tagged_object as *mut TaggedObject<T>;
(*tagged_object).object.as_mut().ok_or(Error {
status: Status::InvalidArg,
reason: "Invalid argument, nothing attach to js_object".to_owned(),
@ -487,9 +487,9 @@ impl Env {
&mut unknown_tagged_object,
))?;
let type_id: *const TypeId = mem::transmute(unknown_tagged_object);
let type_id = unknown_tagged_object as *const TypeId;
if *type_id == TypeId::of::<T>() {
let tagged_object: *mut TaggedObject<T> = mem::transmute(unknown_tagged_object);
let tagged_object = unknown_tagged_object as *mut TaggedObject<T>;
(*tagged_object).object = None;
Ok(())
} else {
@ -527,9 +527,9 @@ impl Env {
&mut unknown_tagged_object,
))?;
let type_id: *const TypeId = mem::transmute(unknown_tagged_object);
let type_id = unknown_tagged_object as *const TypeId;
if *type_id == TypeId::of::<T>() {
let tagged_object: *mut TaggedObject<T> = mem::transmute(unknown_tagged_object);
let tagged_object = unknown_tagged_object as *mut TaggedObject<T>;
(*tagged_object).object.as_mut().ok_or(Error {
status: Status::InvalidArg,
reason: "nothing attach to js_external".to_owned(),
@ -610,7 +610,7 @@ impl Env {
) -> Result<CleanupEnvHook<T>>
where
T: 'static,
F: 'static + FnOnce(T) -> (),
F: 'static + FnOnce(T),
{
let hook = CleanupEnvHookData {
data: cleanup_data,
@ -793,7 +793,7 @@ unsafe extern "C" fn raw_finalize<T>(
finalize_data: *mut c_void,
_finalize_hint: *mut c_void,
) {
let tagged_object: *mut TaggedObject<T> = mem::transmute(finalize_data);
let tagged_object = finalize_data as *mut TaggedObject<T>;
Box::from_raw(tagged_object);
}

View file

@ -109,7 +109,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> {
ValueType::String => visitor.visit_enum(JsEnumAccess::new(
unsafe { JsString::from_raw_unchecked(self.0.env, self.0.value) }
.into_utf8()?
.to_owned()?,
.into_owned()?,
None,
)),
ValueType::Object => {
@ -128,7 +128,7 @@ impl<'x, 'de, 'env> serde::de::Deserializer<'x> for &'de mut De<'env> {
let key = properties.get_element::<JsString>(0)?;
let value: JsUnknown = js_object.get_property(&key)?;
visitor.visit_enum(JsEnumAccess::new(
key.into_utf8()?.to_owned()?,
key.into_utf8()?.into_owned()?,
Some(&value.0),
))
}

View file

@ -24,7 +24,6 @@ impl<T: NapiValue> EscapableHandleScope<T> {
})
}
#[must_use]
pub fn close(self, env: Env) -> Result<()> {
check_status!(unsafe { sys::napi_close_escapable_handle_scope(env.0, self.handle_scope) })
}

View file

@ -32,10 +32,7 @@ impl JsFunction {
.ok()
.map(|u| unsafe { u.raw() })
})
.ok_or(Error::new(
Status::GenericFailure,
"Get raw this failed".to_owned(),
))?;
.ok_or_else(|| Error::new(Status::GenericFailure, "Get raw this failed".to_owned()))?;
let raw_args = args
.iter()
.map(|arg| arg.0.value)
@ -57,6 +54,7 @@ impl JsFunction {
/// https://nodejs.org/api/n-api.html#n_api_napi_new_instance
/// This method is used to instantiate a new `JavaScript` value using a given `JsFunction` that represents the constructor for the object.
#[allow(clippy::new_ret_no_self)]
#[inline]
pub fn new<V>(&self, args: &[V]) -> Result<JsObject>
where

View file

@ -516,10 +516,13 @@ macro_rules! impl_object_methods {
}
pub trait NapiValue: Sized {
#[allow(clippy::missing_safety_doc)]
unsafe fn from_raw(env: sys::napi_env, value: sys::napi_value) -> Result<Self>;
#[allow(clippy::missing_safety_doc)]
unsafe fn from_raw_unchecked(env: sys::napi_env, value: sys::napi_value) -> Self;
#[allow(clippy::missing_safety_doc)]
unsafe fn raw(&self) -> sys::napi_value;
}

View file

@ -24,6 +24,11 @@ impl JsStringLatin1 {
self.buf.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
#[inline]
pub fn take(self) -> Vec<u8> {
self.as_slice().to_vec()

View file

@ -26,6 +26,11 @@ impl JsStringUtf16 {
self.buf.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
#[inline]
pub fn into_value(self) -> JsString {
self.inner

View file

@ -27,7 +27,12 @@ impl JsStringUtf8 {
}
#[inline]
pub fn to_owned(self) -> Result<String> {
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
#[inline]
pub fn into_owned(self) -> Result<String> {
Ok(self.as_str()?.to_owned())
}

View file

@ -39,7 +39,6 @@ impl<T> Ref<T> {
Ok(self.count)
}
#[must_use]
#[inline]
pub fn unref(mut self, env: Env) -> Result<u32> {
check_status!(unsafe { sys::napi_reference_unref(env.0, self.raw_ref, &mut self.count) })?;

View file

@ -1,3 +1,5 @@
#![deny(clippy::all)]
//! High level NodeJS [N-API](https://nodejs.org/api/n-api.html) binding
//!
//! **napi-rs** provides minimal overhead to write N-API modules in `Rust`.

View file

@ -127,7 +127,7 @@ async fn read_file_content(filepath: &Path) -> Result<Vec<u8>> {
pub fn test_tokio_readfile(ctx: CallContext) -> Result<JsUndefined> {
let js_filepath = ctx.get::<JsString>(0)?;
let js_func = ctx.get::<JsFunction>(1)?;
let path_str = js_filepath.into_utf8()?.to_owned()?;
let path_str = js_filepath.into_utf8()?.into_owned()?;
let tsfn =
ctx

View file

@ -5,7 +5,7 @@ use tokio;
#[js_function(1)]
pub fn test_execute_tokio_readfile(ctx: CallContext) -> Result<JsObject> {
let js_filepath = ctx.get::<JsString>(0)?;
let path_str = js_filepath.into_utf8()?.to_owned()?;
let path_str = js_filepath.into_utf8()?.into_owned()?;
ctx.env.execute_tokio_future(
tokio::fs::read(path_str).map(|v| {
v.map_err(|e| {
@ -22,7 +22,7 @@ pub fn test_execute_tokio_readfile(ctx: CallContext) -> Result<JsObject> {
#[js_function(1)]
pub fn error_from_tokio_future(ctx: CallContext) -> Result<JsObject> {
let js_filepath = ctx.get::<JsString>(0)?;
let path_str = js_filepath.into_utf8()?.to_owned()?;
let path_str = js_filepath.into_utf8()?.into_owned()?;
ctx.env.execute_tokio_future(
tokio::fs::read(path_str)
.map_err(Error::from)