Skip to content

Commit

Permalink
chore(neon): fix Clippy lints exposed by #1065
Browse files Browse the repository at this point in the history
  • Loading branch information
dherman committed Nov 22, 2024
1 parent 7780a2f commit 2388227
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
6 changes: 4 additions & 2 deletions crates/neon/src/sys/async_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub unsafe fn schedule<I, O, D>(
Ok(()) => {}
status => {
// If queueing failed, delete the work to prevent a leak
napi::delete_async_work(env, *work);
napi::delete_async_work(env, *work).unwrap();
status.unwrap()
}
}
Expand Down Expand Up @@ -150,7 +150,9 @@ unsafe extern "C" fn call_complete<I, O, D>(env: Env, status: napi::Status, data
..
} = *Box::<Data<I, O, D>>::from_raw(data.cast());

napi::delete_async_work(env, work);
if napi::delete_async_work(env, work).is_err() {
panic!("failed to delete async work");
}

BOUNDARY.catch_failure(env, None, move |env| {
// `unwrap` is okay because `call_complete` should be called exactly once
Expand Down
26 changes: 13 additions & 13 deletions crates/neon/src/sys/no_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,8 @@ unsafe fn error_from_panic(env: Env, panic: Panic) -> Local {
unsafe fn set_property(env: Env, object: Local, key: &str, value: Local) {
let key = create_string(env, key);

match napi::set_property(env, object, key, value) {
Err(_) => fatal_error("Failed to set an object property"),
Ok(()) => (),
if napi::set_property(env, object, key, value).is_err() {
fatal_error("Failed to set an object property");
}
}

Expand Down Expand Up @@ -271,9 +270,8 @@ unsafe fn external_from_panic(env: Env, panic: Panic) -> Local {
let external = result.assume_init();

#[cfg(feature = "napi-8")]
match napi::type_tag_object(env, external, &*crate::MODULE_TAG) {
Err(_) => fail(),
Ok(()) => (),
if napi::type_tag_object(env, external, &*crate::MODULE_TAG).is_err() {
fail();
}

external
Expand All @@ -288,11 +286,14 @@ extern "C" fn finalize_panic(_env: Env, data: *mut c_void, _hint: *mut c_void) {
#[track_caller]
unsafe fn create_string(env: Env, msg: &str) -> Local {
let mut string = MaybeUninit::uninit();
let status = napi::create_string_utf8(env, msg.as_ptr().cast(), msg.len(), string.as_mut_ptr());

match status {
Err(_) => fatal_error("Failed to create a String"),
Ok(()) => (),
if napi::create_string_utf8(
env,
msg.as_ptr().cast(),
msg.len(),
string.as_mut_ptr(),
).is_err() {
fatal_error("Failed to create a String");
}

string.assume_init()
Expand All @@ -301,9 +302,8 @@ unsafe fn create_string(env: Env, msg: &str) -> Local {
unsafe fn is_exception_pending(env: Env) -> bool {
let mut throwing = false;

match napi::is_exception_pending(env, &mut throwing) {
Err(_) => fatal_error("Failed to check if an exception is pending"),
Ok(()) => (),
if napi::is_exception_pending(env, &mut throwing).is_err() {
fatal_error("Failed to check if an exception is pending");
}

throwing
Expand Down
47 changes: 24 additions & 23 deletions crates/neon/src/sys/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{

/// Mutates the `out` argument to refer to a `napi_value` containing a newly created JavaScript Object.
pub unsafe fn new(out: &mut Local, env: Env) {
napi::create_object(env, out as *mut _);
assert!(napi::create_object(env, out as *mut _).is_ok());
}

#[cfg(feature = "napi-8")]
Expand All @@ -31,16 +31,15 @@ pub unsafe fn seal(env: Env, obj: Local) -> Result<(), napi::Status> {
pub unsafe fn get_own_property_names(out: &mut Local, env: Env, object: Local) -> bool {
let mut property_names = MaybeUninit::uninit();

match napi::get_all_property_names(
if napi::get_all_property_names(
env,
object,
napi::KeyCollectionMode::OwnOnly,
napi::KeyFilter::ALL_PROPERTIES | napi::KeyFilter::SKIP_SYMBOLS,
napi::KeyConversion::NumbersToStrings,
property_names.as_mut_ptr(),
) {
Err(_) => return false,
Ok(()) => (),
).is_err() {
return false;
}

*out = property_names.assume_init();
Expand Down Expand Up @@ -81,15 +80,18 @@ pub unsafe fn get_string(

// Not using `crate::string::new()` because it requires a _reference_ to a Local,
// while we only have uninitialized memory.
match napi::create_string_utf8(env, key as *const _, len as usize, key_val.as_mut_ptr()) {
Err(_) => return false,
Ok(()) => (),
if napi::create_string_utf8(
env,
key as *const _,
len as usize,
key_val.as_mut_ptr(),
).is_err() {
return false;
}

// Not using napi_get_named_property() because the `key` may not be null terminated.
match napi::get_property(env, object, key_val.assume_init(), out as *mut _) {
Err(_) => return false,
Ok(()) => (),
if napi::get_property(env, object, key_val.assume_init(), out as *mut _).is_err() {
return false;
}

true
Expand All @@ -113,20 +115,19 @@ pub unsafe fn set_string(

*out = true;

match napi::create_string_utf8(env, key as *const _, len as usize, key_val.as_mut_ptr()) {
Err(_) => {
*out = false;
return false;
}
Ok(()) => (),
if napi::create_string_utf8(
env,
key as *const _,
len as usize,
key_val.as_mut_ptr(),
).is_err() {
*out = false;
return false;
}

match napi::set_property(env, object, key_val.assume_init(), val) {
Err(_) => {
*out = false;
return false;
}
Ok(()) => (),
if napi::set_property(env, object, key_val.assume_init(), val).is_err() {
*out = false;
return false;
}

true
Expand Down
4 changes: 2 additions & 2 deletions crates/neon/src/sys/tsfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl<T> Drop for ThreadsafeFunction<T> {
}

unsafe {
napi::release_threadsafe_function(
assert!(napi::release_threadsafe_function(
self.tsfn.0,
napi::ThreadsafeFunctionReleaseMode::Release,
);
).is_ok());
};
}
}

0 comments on commit 2388227

Please sign in to comment.