style: clippy fix

This commit is contained in:
LongYinan 2022-12-16 20:19:39 +08:00
parent 968d9e10b1
commit 6e4b16fe5d
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
9 changed files with 15 additions and 26 deletions

View file

@ -52,7 +52,7 @@ impl FromNapiValue for BigInt {
ptr::null_mut(),
)
})?;
let mut words: Vec<u64> = Vec::with_capacity(word_count as usize);
let mut words: Vec<u64> = Vec::with_capacity(word_count);
let mut sign_bit = 0;
unsafe {
@ -64,7 +64,7 @@ impl FromNapiValue for BigInt {
words.as_mut_ptr(),
))?;
words.set_len(word_count as usize);
words.set_len(word_count);
}
if word_count == 0 {
words = vec![0];
@ -155,7 +155,7 @@ impl ToNapiValue for BigInt {
impl ToNapiValue for i128 {
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> crate::Result<sys::napi_value> {
let mut raw_value = ptr::null_mut();
let sign_bit = if val > 0 { 0 } else { 1 };
let sign_bit = i32::from(val <= 0);
let words = &val as *const i128 as *const u64;
check_status!(unsafe {
sys::napi_create_bigint_words(env, sign_bit, 2, words, &mut raw_value)

View file

@ -66,6 +66,7 @@ impl<T> Drop for Reference<T> {
impl<T: 'static> Reference<T> {
#[doc(hidden)]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn add_ref(env: crate::sys::napi_env, t: *mut c_void, value: RefInformation) {
REFERENCE_MAP.borrow_mut(|map| {
if let Some((_, previous_ref, previous_rc)) = map.insert(t, value) {

View file

@ -115,7 +115,7 @@ impl Env {
#[cfg(feature = "napi6")]
pub fn create_bigint_from_i128(&self, value: i128) -> Result<JsBigInt> {
let mut raw_value = ptr::null_mut();
let sign_bit = if value > 0 { 0 } else { 1 };
let sign_bit = i32::from(value <= 0);
let words = &value as *const i128 as *const u64;
check_status!(unsafe {
sys::napi_create_bigint_words(self.0, sign_bit, 2, words, &mut raw_value)

View file

@ -204,7 +204,7 @@ impl TryFrom<JsBigInt> for u64 {
impl JsBigInt {
/// <https://nodejs.org/api/n-api.html#n_api_napi_get_value_bigint_words>
pub fn get_words(&mut self) -> Result<(bool, Vec<u64>)> {
let mut words: Vec<u64> = Vec::with_capacity(self.word_count as usize);
let mut words: Vec<u64> = Vec::with_capacity(self.word_count);
let word_count = &mut self.word_count;
let mut sign_bit = 0;
check_status!(unsafe {
@ -218,7 +218,7 @@ impl JsBigInt {
})?;
unsafe {
words.set_len(self.word_count as usize);
words.set_len(self.word_count);
};
Ok((sign_bit == 1, words))

View file

@ -35,7 +35,7 @@ impl JsString {
check_status!(unsafe {
sys::napi_get_value_string_utf8(self.0.env, self.0.value, ptr::null_mut(), 0, &mut length)
})?;
Ok(length as usize)
Ok(length)
}
pub fn utf16_len(&self) -> Result<usize> {
@ -43,7 +43,7 @@ impl JsString {
check_status!(unsafe {
sys::napi_get_value_string_utf16(self.0.env, self.0.value, ptr::null_mut(), 0, &mut length)
})?;
Ok(length as usize)
Ok(length)
}
pub fn latin1_len(&self) -> Result<usize> {
@ -51,7 +51,7 @@ impl JsString {
check_status!(unsafe {
sys::napi_get_value_string_latin1(self.0.env, self.0.value, ptr::null_mut(), 0, &mut length)
})?;
Ok(length as usize)
Ok(length)
}
pub fn into_utf8(self) -> Result<JsStringUtf8> {

View file

@ -87,7 +87,7 @@ fn test_delete_named_property(ctx: CallContext) -> Result<JsBoolean> {
fn test_get_property(ctx: CallContext) -> Result<JsUnknown> {
let obj = ctx.get::<JsObject>(0)?;
let key = ctx.get::<JsUnknown>(1)?;
obj.get_property(&key)
obj.get_property(key)
}
#[js_function(1)]

View file

@ -22,13 +22,7 @@ fn either3(input: Either3<String, u32, bool>) -> u32 {
match input {
Either3::A(s) => s.len() as u32,
Either3::B(n) => n,
Either3::C(b) => {
if b {
1
} else {
0
}
}
Either3::C(b) => u32::from(b),
}
}
@ -42,13 +36,7 @@ fn either4(input: Either4<String, u32, bool, Obj>) -> u32 {
match input {
Either4::A(s) => s.len() as u32,
Either4::B(n) => n,
Either4::C(b) => {
if b {
1
} else {
0
}
}
Either4::C(b) => u32::from(b),
Either4::D(f) => match f.v {
Either::A(s) => s.len() as u32,
Either::B(n) => n,

View file

@ -69,5 +69,5 @@ impl Task for AsyncBuffer {
#[napi]
fn async_reduce_buffer(buf: Buffer) -> Result<AsyncTask<AsyncBuffer>> {
Ok(AsyncTask::new(AsyncBuffer { buf: buf.clone() }))
Ok(AsyncTask::new(AsyncBuffer { buf }))
}

View file

@ -75,7 +75,7 @@ pub fn test_async(env: Env) -> napi::Result<napi::JsObject> {
#[napi]
pub fn from_js(env: Env, input_object: Object) -> napi::Result<String> {
let a: Welcome = env.from_js_value(&input_object)?;
let a: Welcome = env.from_js_value(input_object)?;
Ok(serde_json::to_string(&a)?)
}