From 1e3cce774278758ccdcac137d41eccdf2e3071e2 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Wed, 27 Nov 2024 10:26:39 +0100 Subject: [PATCH] refact: Resolve some clippy warnings (cherry picked from commit dc4bfaa993eb2d712d53f70551db9ac34b0cda08) --- rust/nix-c-raw/build.rs | 1 - rust/nix-expr/src/eval_state.rs | 4 ++-- rust/nix-expr/src/primop.rs | 5 +---- rust/nix-util/src/context.rs | 9 ++++----- rust/nix-util/src/string_return.rs | 10 ++++++++-- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rust/nix-c-raw/build.rs b/rust/nix-c-raw/build.rs index 5f9f7b0..5dbdbc7 100644 --- a/rust/nix-c-raw/build.rs +++ b/rust/nix-c-raw/build.rs @@ -1,4 +1,3 @@ -use bindgen; use std::env; use std::path::PathBuf; diff --git a/rust/nix-expr/src/eval_state.rs b/rust/nix-expr/src/eval_state.rs index cfa39a3..12689e7 100644 --- a/rust/nix-expr/src/eval_state.rs +++ b/rust/nix-expr/src/eval_state.rs @@ -52,8 +52,8 @@ impl EvalStateWeak { pub fn upgrade(&self) -> Option { self.inner.upgrade().and_then(|eval_state| { self.store.upgrade().map(|store| EvalState { - eval_state: eval_state, - store: store, + eval_state, + store, context: Context::new(), }) }) diff --git a/rust/nix-expr/src/primop.rs b/rust/nix-expr/src/primop.rs index 9234954..2d2e2d2 100644 --- a/rust/nix-expr/src/primop.rs +++ b/rust/nix-expr/src/primop.rs @@ -55,10 +55,7 @@ impl PrimOp { // TODO: Use the GC with finalizer, if possible. let user_data = ManuallyDrop::new(Box::new(PrimOpContext { arity: N, - function: Box::new(move |eval_state, args| { - let r = f(eval_state, args.try_into().unwrap()); - r - }), + function: Box::new(move |eval_state, args| f(eval_state, args.try_into().unwrap())), eval_state: eval_state.weak_ref(), })); user_data.as_ref() as *const PrimOpContext as *mut c_void diff --git a/rust/nix-util/src/context.rs b/rust/nix-util/src/context.rs index d4dbe94..8e12a8b 100644 --- a/rust/nix-util/src/context.rs +++ b/rust/nix-util/src/context.rs @@ -18,10 +18,9 @@ impl Context { // We're almost certainly going to crash anyways. panic!("nix_c_context_create returned a null pointer"); } - let ctx = Context { + Context { inner: NonNull::new(ctx).unwrap(), - }; - ctx + } } /// Access the C context pointer. @@ -36,7 +35,7 @@ impl Context { /// We recommend to use `check_call!` if possible. pub fn check_err(&self) -> Result<()> { let err = unsafe { raw::err_code(self.inner.as_ptr()) }; - if err != raw::err_NIX_OK.try_into().unwrap() { + if err != raw::err_NIX_OK { // msgp is a borrowed pointer (pointing into the context), so we don't need to free it let msgp = unsafe { raw::err_msg(null_mut(), self.inner.as_ptr(), null_mut()) }; // Turn the i8 pointer into a Rust string by copying @@ -50,7 +49,7 @@ impl Context { unsafe { raw::set_err_msg( self.inner.as_ptr(), - raw::err_NIX_OK.try_into().unwrap(), + raw::err_NIX_OK, b"\0".as_ptr() as *const i8, ); } diff --git a/rust/nix-util/src/string_return.rs b/rust/nix-util/src/string_return.rs index f06fa32..2ccbc11 100644 --- a/rust/nix-util/src/string_return.rs +++ b/rust/nix-util/src/string_return.rs @@ -5,6 +5,12 @@ use anyhow::Result; /// This function is used by the other nix_* crates, and you should never need to call it yourself. /// /// Some functions in the nix library "return" strings without giving you ownership over them, by letting you pass a callback function that gets to look at that string. This callback simply turns that string pointer into an owned rust String. +/// +/// # Safety +/// +/// _Manual memory management_ +/// +/// Only for passing to the nix C API. Do not call this function directly. pub unsafe extern "C" fn callback_get_result_string( start: *const ::std::os::raw::c_char, n: std::os::raw::c_uint, @@ -12,7 +18,7 @@ pub unsafe extern "C" fn callback_get_result_string( ) { let ret = user_data as *mut Result; - if start == std::ptr::null() { + if start.is_null() { if n != 0 { panic!("callback_get_result_string: start is null but n is not zero"); } @@ -22,7 +28,7 @@ pub unsafe extern "C" fn callback_get_result_string( let slice = std::slice::from_raw_parts(start as *const u8, n as usize); - if !(*ret).is_err() { + if (*ret).is_ok() { panic!( "callback_get_result_string: Result must be initialized to Err. Did Nix call us twice?" );