Skip to content

Commit

Permalink
clippy: warn on FFI not using new traits
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorak-mmk authored and muzarski committed Nov 25, 2024
1 parent 9f7f978 commit 82dfe26
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions scylla-rust-wrapper/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
disallowed-methods = [
"std::boxed::Box::from_raw",
"std::boxed::Box::from_raw_in",
"std::boxed::Box::into_raw",
"std::boxed::Box::into_raw_with_allocator",

"std::sync::Arc::as_ptr",
"std::sync::Arc::decrement_strong_count",
"std::sync::Arc::from_raw",
"std::sync::Arc::increment_strong_count",
"std::sync::Arc::into_raw",

"std::rc::Rc::as_ptr",
"std::rc::Rc::decrement_strong_count",
"std::rc::Rc::from_raw",
"std::rc::Rc::increment_strong_count",
"std::rc::Rc::into_raw",

"const_ptr::as_ref",
"mut_ptr::as_mut"
]
14 changes: 14 additions & 0 deletions scylla-rust-wrapper/src/argconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,23 @@ pub(crate) use make_c_str;
/// the memory associated with the pointer using corresponding driver's API function.
pub trait BoxFFI {
fn into_ptr(self: Box<Self>) -> *mut Self {
#[allow(clippy::disallowed_methods)]
Box::into_raw(self)
}
unsafe fn from_ptr(ptr: *mut Self) -> Box<Self> {
#[allow(clippy::disallowed_methods)]
Box::from_raw(ptr)
}
unsafe fn maybe_as_ref<'a>(ptr: *const Self) -> Option<&'a Self> {
#[allow(clippy::disallowed_methods)]
ptr.as_ref()
}
unsafe fn as_ref<'a>(ptr: *const Self) -> &'a Self {
#[allow(clippy::disallowed_methods)]
ptr.as_ref().unwrap()
}
unsafe fn as_mut_ref<'a>(ptr: *mut Self) -> &'a mut Self {
#[allow(clippy::disallowed_methods)]
ptr.as_mut().unwrap()
}
unsafe fn free(ptr: *mut Self) {
Expand All @@ -107,22 +112,29 @@ pub trait BoxFFI {
/// This trait does not support interior mutability. For this, see [`MutArcFFI`].
pub trait ArcFFI {
fn as_ptr(self: &Arc<Self>) -> *const Self {
#[allow(clippy::disallowed_methods)]
Arc::as_ptr(self)
}
fn into_ptr(self: Arc<Self>) -> *const Self {
#[allow(clippy::disallowed_methods)]
Arc::into_raw(self)
}
unsafe fn from_ptr(ptr: *const Self) -> Arc<Self> {
#[allow(clippy::disallowed_methods)]
Arc::from_raw(ptr)
}
unsafe fn cloned_from_ptr(ptr: *const Self) -> Arc<Self> {
#[allow(clippy::disallowed_methods)]
Arc::increment_strong_count(ptr);
#[allow(clippy::disallowed_methods)]
Arc::from_raw(ptr)
}
unsafe fn maybe_as_ref<'a>(ptr: *const Self) -> Option<&'a Self> {
#[allow(clippy::disallowed_methods)]
ptr.as_ref()
}
unsafe fn as_ref<'a>(ptr: *const Self) -> &'a Self {
#[allow(clippy::disallowed_methods)]
ptr.as_ref().unwrap()
}
unsafe fn free(ptr: *const Self) {
Expand All @@ -133,6 +145,7 @@ pub trait ArcFFI {
/// An API extension for shared data that require interior mutability.
pub trait MutArcFFI: ArcFFI {
unsafe fn as_mut_ref<'a>(ptr: *mut Self) -> &'a mut Self {
#[allow(clippy::disallowed_methods)]
ptr.as_mut().unwrap()
}
}
Expand All @@ -150,6 +163,7 @@ pub trait RefFFI {
self as *const Self
}
unsafe fn as_ref<'a>(ptr: *const Self) -> &'a Self {
#[allow(clippy::disallowed_methods)]
ptr.as_ref().unwrap()
}
}
1 change: 1 addition & 0 deletions scylla-rust-wrapper/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ mod tests {
// the future, and execute its callback
#[test]
#[ntest::timeout(600)]
#[allow(clippy::disallowed_methods)]
fn test_cass_future_callback() {
unsafe {
const ERROR_MSG: &str = "NOBODY EXPECTED SPANISH INQUISITION";
Expand Down

0 comments on commit 82dfe26

Please sign in to comment.