From 43591154b2a664e83c649fb84c40f24057d99422 Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 5 Jan 2022 19:50:25 +0100 Subject: [PATCH 1/2] clippy:ptr_as_ptr --- src/blob/mod.rs | 3 +-- src/blob/pos_io.rs | 10 +++++----- src/collation.rs | 10 +++++----- src/context.rs | 2 +- src/functions.rs | 20 ++++++++++---------- src/hooks.rs | 22 +++++++++++----------- src/inner_connection.rs | 2 +- src/lib.rs | 4 ++-- src/statement.rs | 6 +++--- src/types/value_ref.rs | 4 ++-- src/util/sqlite_string.rs | 4 ++-- src/vtab/array.rs | 2 +- src/vtab/mod.rs | 36 ++++++++++++++++++------------------ 13 files changed, 62 insertions(+), 63 deletions(-) diff --git a/src/blob/mod.rs b/src/blob/mod.rs index 2b13e1d..70c607a 100644 --- a/src/blob/mod.rs +++ b/src/blob/mod.rs @@ -318,8 +318,7 @@ impl io::Read for Blob<'_> { if n <= 0 { return Ok(0); } - let rc = - unsafe { ffi::sqlite3_blob_read(self.blob, buf.as_mut_ptr() as *mut _, n, self.pos) }; + let rc = unsafe { ffi::sqlite3_blob_read(self.blob, buf.as_mut_ptr().cast(), n, self.pos) }; self.conn .decode_result(rc) .map(|_| { diff --git a/src/blob/pos_io.rs b/src/blob/pos_io.rs index 01f9c34..ecc7d65 100644 --- a/src/blob/pos_io.rs +++ b/src/blob/pos_io.rs @@ -47,7 +47,7 @@ impl<'conn> Blob<'conn> { self.conn.decode_result(unsafe { ffi::sqlite3_blob_write( self.blob, - buf.as_ptr() as *const _, + buf.as_ptr().cast(), buf.len() as i32, write_start as i32, ) @@ -84,7 +84,7 @@ impl<'conn> Blob<'conn> { // Safety: this is safe because `raw_read_at` never stores uninitialized // data into `as_uninit`. let as_uninit: &mut [MaybeUninit] = - unsafe { from_raw_parts_mut(buf.as_mut_ptr() as *mut _, buf.len()) }; + unsafe { from_raw_parts_mut(buf.as_mut_ptr().cast(), buf.len()) }; self.raw_read_at(as_uninit, read_start).map(|s| s.len()) } @@ -119,7 +119,7 @@ impl<'conn> Blob<'conn> { // We could return `Ok(&mut [])`, but it seems confusing that the // pointers don't match, so fabricate a empty slice of u8 with the // same base pointer as `buf`. - let empty = unsafe { from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, 0) }; + let empty = unsafe { from_raw_parts_mut(buf.as_mut_ptr().cast::(), 0) }; return Ok(empty); } @@ -152,12 +152,12 @@ impl<'conn> Blob<'conn> { unsafe { self.conn.decode_result(ffi::sqlite3_blob_read( self.blob, - buf.as_mut_ptr() as *mut _, + buf.as_mut_ptr().cast(), read_len as i32, read_start as i32, ))?; - Ok(from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, read_len)) + Ok(from_raw_parts_mut(buf.as_mut_ptr().cast::(), read_len)) } } diff --git a/src/collation.rs b/src/collation.rs index 7a24ad7..c1fe3f7 100644 --- a/src/collation.rs +++ b/src/collation.rs @@ -10,7 +10,7 @@ use crate::{str_to_cstring, Connection, InnerConnection, Result}; // FIXME copy/paste from function.rs unsafe extern "C" fn free_boxed_value(p: *mut c_void) { - drop(Box::from_raw(p as *mut T)); + drop(Box::from_raw(p.cast::())); } impl Connection { @@ -57,14 +57,14 @@ impl InnerConnection { C: Fn(&str, &str) -> Ordering, { let r = catch_unwind(|| { - let boxed_f: *mut C = arg1 as *mut C; + let boxed_f: *mut C = arg1.cast::(); assert!(!boxed_f.is_null(), "Internal error - null function pointer"); let s1 = { - let c_slice = slice::from_raw_parts(arg3 as *const u8, arg2 as usize); + let c_slice = slice::from_raw_parts(arg3.cast::(), arg2 as usize); String::from_utf8_lossy(c_slice) }; let s2 = { - let c_slice = slice::from_raw_parts(arg5 as *const u8, arg4 as usize); + let c_slice = slice::from_raw_parts(arg5.cast::(), arg4 as usize); String::from_utf8_lossy(c_slice) }; (*boxed_f)(s1.as_ref(), s2.as_ref()) @@ -91,7 +91,7 @@ impl InnerConnection { self.db(), c_name.as_ptr(), flags, - boxed_f as *mut c_void, + boxed_f.cast::(), Some(call_boxed_closure::), Some(free_boxed_value::), ) diff --git a/src/context.rs b/src/context.rs index ce60ebb..845d60e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -62,7 +62,7 @@ pub(super) unsafe fn set_result(ctx: *mut sqlite3_context, result: &ToSqlOutput< } else { ffi::sqlite3_result_blob( ctx, - b.as_ptr() as *const c_void, + b.as_ptr().cast::(), length as c_int, ffi::SQLITE_TRANSIENT(), ); diff --git a/src/functions.rs b/src/functions.rs index 3006a79..952d3ee 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -101,7 +101,7 @@ unsafe fn report_error(ctx: *mut sqlite3_context, err: &Error) { } unsafe extern "C" fn free_boxed_value(p: *mut c_void) { - drop(Box::from_raw(p as *mut T)); + drop(Box::from_raw(p.cast::())); } /// Context is a wrapper for the SQLite function @@ -198,7 +198,7 @@ impl Context<'_> { ffi::sqlite3_set_auxdata( self.ctx, arg, - raw as *mut _, + raw.cast(), Some(free_boxed_value::), ) }; @@ -477,7 +477,7 @@ impl InnerConnection { T: ToSql, { let r = catch_unwind(|| { - let boxed_f: *mut F = ffi::sqlite3_user_data(ctx) as *mut F; + let boxed_f: *mut F = ffi::sqlite3_user_data(ctx).cast::(); assert!(!boxed_f.is_null(), "Internal error - null function pointer"); let ctx = Context { ctx, @@ -509,7 +509,7 @@ impl InnerConnection { c_name.as_ptr(), n_arg, flags.bits(), - boxed_f as *mut c_void, + boxed_f.cast::(), Some(call_boxed_closure::), None, None, @@ -539,7 +539,7 @@ impl InnerConnection { c_name.as_ptr(), n_arg, flags.bits(), - boxed_aggr as *mut c_void, + boxed_aggr.cast::(), None, Some(call_boxed_step::), Some(call_boxed_final::), @@ -570,7 +570,7 @@ impl InnerConnection { c_name.as_ptr(), n_arg, flags.bits(), - boxed_aggr as *mut c_void, + boxed_aggr.cast::(), Some(call_boxed_step::), Some(call_boxed_final::), Some(call_boxed_value::), @@ -626,7 +626,7 @@ unsafe extern "C" fn call_boxed_step( }; let r = catch_unwind(|| { - let boxed_aggr: *mut D = ffi::sqlite3_user_data(ctx) as *mut D; + let boxed_aggr: *mut D = ffi::sqlite3_user_data(ctx).cast::(); assert!( !boxed_aggr.is_null(), "Internal error - null aggregate pointer" @@ -674,7 +674,7 @@ unsafe extern "C" fn call_boxed_inverse( }; let r = catch_unwind(|| { - let boxed_aggr: *mut W = ffi::sqlite3_user_data(ctx) as *mut W; + let boxed_aggr: *mut W = ffi::sqlite3_user_data(ctx).cast::(); assert!( !boxed_aggr.is_null(), "Internal error - null aggregate pointer" @@ -719,7 +719,7 @@ where }; let r = catch_unwind(|| { - let boxed_aggr: *mut D = ffi::sqlite3_user_data(ctx) as *mut D; + let boxed_aggr: *mut D = ffi::sqlite3_user_data(ctx).cast::(); assert!( !boxed_aggr.is_null(), "Internal error - null aggregate pointer" @@ -764,7 +764,7 @@ where }; let r = catch_unwind(|| { - let boxed_aggr: *mut W = ffi::sqlite3_user_data(ctx) as *mut W; + let boxed_aggr: *mut W = ffi::sqlite3_user_data(ctx).cast::(); assert!( !boxed_aggr.is_null(), "Internal error - null aggregate pointer" diff --git a/src/hooks.rs b/src/hooks.rs index c15bdfc..f950711 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -425,7 +425,7 @@ impl InnerConnection { F: FnMut() -> bool, { let r = catch_unwind(|| { - let boxed_hook: *mut F = p_arg as *mut F; + let boxed_hook: *mut F = p_arg.cast::(); (*boxed_hook)() }); if let Ok(true) = r { @@ -451,7 +451,7 @@ impl InnerConnection { ffi::sqlite3_commit_hook( self.db(), Some(call_boxed_closure::), - boxed_hook as *mut _, + boxed_hook.cast(), ) } } @@ -473,8 +473,8 @@ impl InnerConnection { where F: FnMut(), { - let _ = catch_unwind(|| { - let boxed_hook: *mut F = p_arg as *mut F; + drop(catch_unwind(|| { + let boxed_hook: *mut F = p_arg.cast::(); (*boxed_hook)(); }); } @@ -492,7 +492,7 @@ impl InnerConnection { ffi::sqlite3_rollback_hook( self.db(), Some(call_boxed_closure::), - boxed_hook as *mut _, + boxed_hook.cast(), ) } } @@ -520,8 +520,8 @@ impl InnerConnection { F: FnMut(Action, &str, &str, i64), { let action = Action::from(action_code); - let _ = catch_unwind(|| { - let boxed_hook: *mut F = p_arg as *mut F; + drop(catch_unwind(|| { + let boxed_hook: *mut F = p_arg.cast::(); (*boxed_hook)( action, expect_utf8(p_db_name, "database name"), @@ -544,7 +544,7 @@ impl InnerConnection { ffi::sqlite3_update_hook( self.db(), Some(call_boxed_closure::), - boxed_hook as *mut _, + boxed_hook.cast(), ) } } @@ -567,7 +567,7 @@ impl InnerConnection { F: FnMut() -> bool, { let r = catch_unwind(|| { - let boxed_handler: *mut F = p_arg as *mut F; + let boxed_handler: *mut F = p_arg.cast::(); (*boxed_handler)() }); if let Ok(true) = r { @@ -626,7 +626,7 @@ impl InnerConnection { "accessor (inner-most trigger or view)", ), }; - let boxed_hook: *mut F = p_arg as *mut F; + let boxed_hook: *mut F = p_arg.cast::(); (*boxed_hook)(auth_ctx) }) .map(Authorization::into_raw) @@ -666,7 +666,7 @@ impl InnerConnection { } unsafe fn free_boxed_hook(p: *mut c_void) { - drop(Box::from_raw(p as *mut F)); + drop(Box::from_raw(p.cast::())); } unsafe fn expect_utf8<'a>(p_str: *const c_char, description: &'static str) -> &'a str { diff --git a/src/inner_connection.rs b/src/inner_connection.rs index b3b1bcd..f57541d 100644 --- a/src/inner_connection.rs +++ b/src/inner_connection.rs @@ -208,7 +208,7 @@ impl InnerConnection { Ok(()) } else { let message = super::errmsg_to_string(errmsg); - ffi::sqlite3_free(errmsg as *mut ::std::os::raw::c_void); + ffi::sqlite3_free(errmsg.cast::<::std::os::raw::c_void>()); Err(error_from_sqlite_code(r, Some(message))) } } diff --git a/src/lib.rs b/src/lib.rs index ec9c96f..9447170 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -258,10 +258,10 @@ fn str_to_cstring(s: &str) -> Result { fn str_for_sqlite(s: &[u8]) -> Result<(*const c_char, c_int, ffi::sqlite3_destructor_type)> { let len = len_as_c_int(s.len())?; let (ptr, dtor_info) = if len != 0 { - (s.as_ptr() as *const c_char, ffi::SQLITE_TRANSIENT()) + (s.as_ptr().cast::(), ffi::SQLITE_TRANSIENT()) } else { // Return a pointer guaranteed to live forever - ("".as_ptr() as *const c_char, ffi::SQLITE_STATIC()) + ("".as_ptr().cast::(), ffi::SQLITE_STATIC()) }; Ok((ptr, len, dtor_info)) } diff --git a/src/statement.rs b/src/statement.rs index 04c71ef..35f608f 100644 --- a/src/statement.rs +++ b/src/statement.rs @@ -717,7 +717,7 @@ impl Statement<'_> { ffi::sqlite3_bind_blob( ptr, col as c_int, - b.as_ptr() as *const c_void, + b.as_ptr().cast::(), length, ffi::SQLITE_TRANSIENT(), ) @@ -875,7 +875,7 @@ impl Statement<'_> { !text.is_null(), "unexpected SQLITE_TEXT column type with NULL data" ); - from_raw_parts(text as *const u8, len as usize) + from_raw_parts(text.cast::(), len as usize) }; ValueRef::Text(s) @@ -897,7 +897,7 @@ impl Statement<'_> { !blob.is_null(), "unexpected SQLITE_BLOB column type with NULL data" ); - ValueRef::Blob(unsafe { from_raw_parts(blob as *const u8, len as usize) }) + ValueRef::Blob(unsafe { from_raw_parts(blob.cast::(), len as usize) }) } else { // The return value from sqlite3_column_blob() for a zero-length BLOB // is a NULL pointer. diff --git a/src/types/value_ref.rs b/src/types/value_ref.rs index a7cd416..41d957b 100644 --- a/src/types/value_ref.rs +++ b/src/types/value_ref.rs @@ -228,7 +228,7 @@ impl<'a> ValueRef<'a> { !text.is_null(), "unexpected SQLITE_TEXT value type with NULL data" ); - let s = from_raw_parts(text as *const u8, len as usize); + let s = from_raw_parts(text.cast::(), len as usize); ValueRef::Text(s) } ffi::SQLITE_BLOB => { @@ -246,7 +246,7 @@ impl<'a> ValueRef<'a> { !blob.is_null(), "unexpected SQLITE_BLOB value type with NULL data" ); - ValueRef::Blob(from_raw_parts(blob as *const u8, len as usize)) + ValueRef::Blob(from_raw_parts(blob.cast::(), len as usize)) } else { // The return value from sqlite3_value_blob() for a zero-length BLOB // is a NULL pointer. diff --git a/src/util/sqlite_string.rs b/src/util/sqlite_string.rs index d131b56..94508fe 100644 --- a/src/util/sqlite_string.rs +++ b/src/util/sqlite_string.rs @@ -120,7 +120,7 @@ impl SqliteMallocString { // `>` because we added 1. debug_assert!(len_to_alloc > 0); debug_assert_eq!((len_to_alloc - 1) as usize, src_len); - NonNull::new(ffi::sqlite3_malloc(len_to_alloc) as *mut c_char) + NonNull::new(ffi::sqlite3_malloc(len_to_alloc).cast::()) }) .unwrap_or_else(|| { use std::alloc::{handle_alloc_error, Layout}; @@ -138,7 +138,7 @@ impl SqliteMallocString { // Note: This call does not return. handle_alloc_error(layout); }); - let buf: *mut c_char = res_ptr.as_ptr() as *mut c_char; + let buf: *mut c_char = res_ptr.as_ptr().cast::(); src_ptr.copy_to_nonoverlapping(buf, src_len); buf.add(src_len).write(0); debug_assert_eq!(std::ffi::CStr::from_ptr(res_ptr.as_ptr()).to_bytes(), bytes); diff --git a/src/vtab/array.rs b/src/vtab/array.rs index dadd637..1d49e1e 100644 --- a/src/vtab/array.rs +++ b/src/vtab/array.rs @@ -41,7 +41,7 @@ use crate::{Connection, Result}; // http://sqlite.org/bindptr.html -pub(crate) const ARRAY_TYPE: *const c_char = b"rarray\0" as *const u8 as *const c_char; +pub(crate) const ARRAY_TYPE: *const c_char = (b"rarray\0" as *const u8).cast::(); pub(crate) unsafe extern "C" fn free_array(p: *mut c_void) { let _: Array = Rc::from_raw(p as *const Vec); diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index 929d599..a8e455a 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -699,7 +699,7 @@ impl InnerConnection { self.db(), c_name.as_ptr(), &module.base, - boxed_aux as *mut c_void, + boxed_aux.cast::(), Some(free_boxed_value::), ) } @@ -766,7 +766,7 @@ pub fn parse_boolean(s: &str) -> Option { // FIXME copy/paste from function.rs unsafe extern "C" fn free_boxed_value(p: *mut c_void) { - let _: Box = Box::from_raw(p as *mut T); + drop(Box::from_raw(p.cast::())); } unsafe extern "C" fn rust_create<'vtab, T>( @@ -783,7 +783,7 @@ where use std::ffi::CStr; let mut conn = VTabConnection(db); - let aux = aux as *mut T::Aux; + let aux = aux.cast::(); let args = slice::from_raw_parts(argv, argc as usize); let vec = args .iter() @@ -795,7 +795,7 @@ where let rc = ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()); if rc == ffi::SQLITE_OK { let boxed_vtab: *mut T = Box::into_raw(Box::new(vtab)); - *pp_vtab = boxed_vtab as *mut ffi::sqlite3_vtab; + *pp_vtab = boxed_vtab.cast::(); ffi::SQLITE_OK } else { let err = error_from_sqlite_code(rc, None); @@ -835,7 +835,7 @@ where use std::ffi::CStr; let mut conn = VTabConnection(db); - let aux = aux as *mut T::Aux; + let aux = aux.cast::(); let args = slice::from_raw_parts(argv, argc as usize); let vec = args .iter() @@ -847,7 +847,7 @@ where let rc = ffi::sqlite3_declare_vtab(db, c_sql.as_ptr()); if rc == ffi::SQLITE_OK { let boxed_vtab: *mut T = Box::into_raw(Box::new(vtab)); - *pp_vtab = boxed_vtab as *mut ffi::sqlite3_vtab; + *pp_vtab = boxed_vtab.cast::(); ffi::SQLITE_OK } else { let err = error_from_sqlite_code(rc, None); @@ -880,7 +880,7 @@ unsafe extern "C" fn rust_best_index<'vtab, T>( where T: VTab<'vtab>, { - let vt = vtab as *mut T; + let vt = vtab.cast::(); let mut idx_info = IndexInfo(info); match (*vt).best_index(&mut idx_info) { Ok(_) => ffi::SQLITE_OK, @@ -904,8 +904,8 @@ where if vtab.is_null() { return ffi::SQLITE_OK; } - let vtab = vtab as *mut T; - let _: Box = Box::from_raw(vtab); + let vtab = vtab.cast::(); + drop(Box::from_raw(vtab)); ffi::SQLITE_OK } @@ -916,7 +916,7 @@ where if vtab.is_null() { return ffi::SQLITE_OK; } - let vt = vtab as *mut T; + let vt = vtab.cast::(); match (*vt).destroy() { Ok(_) => { let _: Box = Box::from_raw(vt); @@ -942,11 +942,11 @@ unsafe extern "C" fn rust_open<'vtab, T: 'vtab>( where T: VTab<'vtab>, { - let vt = vtab as *mut T; + let vt = vtab.cast::(); match (*vt).open() { Ok(cursor) => { let boxed_cursor: *mut T::Cursor = Box::into_raw(Box::new(cursor)); - *pp_cursor = boxed_cursor as *mut ffi::sqlite3_vtab_cursor; + *pp_cursor = boxed_cursor.cast::(); ffi::SQLITE_OK } Err(Error::SqliteFailure(err, s)) => { @@ -966,8 +966,8 @@ unsafe extern "C" fn rust_close(cursor: *mut ffi::sqlite3_vtab_cursor) -> c_i where C: VTabCursor, { - let cr = cursor as *mut C; - let _: Box = Box::from_raw(cr); + let cr = cursor.cast::(); + drop(Box::from_raw(cr)); ffi::SQLITE_OK } @@ -1007,7 +1007,7 @@ unsafe extern "C" fn rust_eof(cursor: *mut ffi::sqlite3_vtab_cursor) -> c_int where C: VTabCursor, { - let cr = cursor as *mut C; + let cr = cursor.cast::(); (*cr).eof() as c_int } @@ -1019,7 +1019,7 @@ unsafe extern "C" fn rust_column( where C: VTabCursor, { - let cr = cursor as *mut C; + let cr = cursor.cast::(); let mut ctxt = Context(ctx); result_error(ctx, (*cr).column(&mut ctxt, i)) } @@ -1031,7 +1031,7 @@ unsafe extern "C" fn rust_rowid( where C: VTabCursor, { - let cr = cursor as *mut C; + let cr = cursor.cast::(); match (*cr).rowid() { Ok(rowid) => { *p_rowid = rowid; @@ -1065,7 +1065,7 @@ unsafe fn cursor_error(cursor: *mut ffi::sqlite3_vtab_cursor, result: Result< #[cold] unsafe fn set_err_msg(vtab: *mut ffi::sqlite3_vtab, err_msg: &str) { if !(*vtab).zErrMsg.is_null() { - ffi::sqlite3_free((*vtab).zErrMsg as *mut c_void); + ffi::sqlite3_free((*vtab).zErrMsg.cast::()); } (*vtab).zErrMsg = alloc(err_msg); } From 6f8e509763c46264150f31216168f731a03edc19 Mon Sep 17 00:00:00 2001 From: gwenn Date: Wed, 5 Jan 2022 20:12:52 +0100 Subject: [PATCH 2/2] Oops --- src/hooks.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks.rs b/src/hooks.rs index f950711..78f0b2b 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -476,7 +476,7 @@ impl InnerConnection { drop(catch_unwind(|| { let boxed_hook: *mut F = p_arg.cast::(); (*boxed_hook)(); - }); + })); } let free_rollback_hook = if hook.is_some() { @@ -528,7 +528,7 @@ impl InnerConnection { expect_utf8(p_table_name, "table name"), row_id, ); - }); + })); } let free_update_hook = if hook.is_some() {