Adopt unsafe_op_in_unsafe_fn style

This commit is contained in:
David Tolnay 2021-08-27 12:12:53 -07:00
parent 7285554739
commit 0b4b73f2cb
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
16 changed files with 101 additions and 71 deletions

View File

@ -28,6 +28,12 @@ fn main() {
rustc.version,
);
}
if rustc.minor < 52 {
// #![deny(unsafe_op_in_unsafe_fn)].
// https://github.com/rust-lang/rust/issues/71668
println!("cargo:rustc-cfg=no_unsafe_op_in_unsafe_fn_lint");
}
}
}

View File

@ -240,9 +240,11 @@ impl StackString {
pub unsafe fn init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString> {
let value = value.as_ref();
let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
string_init(this, value.as_ptr(), value.len());
Pin::new_unchecked(&mut *this.as_mut_ptr())
unsafe {
let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
string_init(this, value.as_ptr(), value.len());
Pin::new_unchecked(&mut *this.as_mut_ptr())
}
}
}

View File

@ -86,8 +86,10 @@ where
/// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
pub unsafe fn get_unchecked(&self, pos: usize) -> &T {
let this = self as *const CxxVector<T> as *mut CxxVector<T>;
let ptr = T::__get_unchecked(this, pos) as *const T;
&*ptr
unsafe {
let ptr = T::__get_unchecked(this, pos) as *const T;
&*ptr
}
}
/// Returns a pinned mutable reference to an element without doing bounds
@ -102,8 +104,10 @@ where
///
/// [operator_at]: https://en.cppreference.com/w/cpp/container/vector/operator_at
pub unsafe fn index_unchecked_mut(self: Pin<&mut Self>, pos: usize) -> Pin<&mut T> {
let ptr = T::__get_unchecked(self.get_unchecked_mut(), pos);
Pin::new_unchecked(&mut *ptr)
unsafe {
let ptr = T::__get_unchecked(self.get_unchecked_mut(), pos);
Pin::new_unchecked(&mut *ptr)
}
}
/// Returns a slice to the underlying contiguous array of elements.
@ -372,7 +376,7 @@ macro_rules! vector_element_by_value_methods {
fn __push_back(_: Pin<&mut CxxVector<$ty>>, _: &mut ManuallyDrop<$ty>);
}
}
__push_back(v, value);
unsafe { __push_back(v, value) }
}
#[doc(hidden)]
unsafe fn __pop_back(v: Pin<&mut CxxVector<$ty>>, out: &mut MaybeUninit<$ty>) {
@ -382,7 +386,7 @@ macro_rules! vector_element_by_value_methods {
fn __pop_back(_: Pin<&mut CxxVector<$ty>>, _: &mut MaybeUninit<$ty>);
}
}
__pop_back(v, out);
unsafe { __pop_back(v, out) }
}
};
}
@ -415,7 +419,7 @@ macro_rules! impl_vector_element {
fn __get_unchecked(_: *mut CxxVector<$ty>, _: usize) -> *mut $ty;
}
}
__get_unchecked(v, pos)
unsafe { __get_unchecked(v, pos) }
}
vector_element_by_value_methods!($kind, $segment, $ty);
#[doc(hidden)]
@ -439,7 +443,7 @@ macro_rules! impl_vector_element {
}
}
let mut repr = MaybeUninit::uninit();
__unique_ptr_raw(&mut repr, raw);
unsafe { __unique_ptr_raw(&mut repr, raw) }
repr
}
#[doc(hidden)]
@ -450,7 +454,7 @@ macro_rules! impl_vector_element {
fn __unique_ptr_get(this: *const MaybeUninit<*mut c_void>) -> *const CxxVector<$ty>;
}
}
__unique_ptr_get(&repr)
unsafe { __unique_ptr_get(&repr) }
}
#[doc(hidden)]
unsafe fn __unique_ptr_release(mut repr: MaybeUninit<*mut c_void>) -> *mut CxxVector<Self> {
@ -460,7 +464,7 @@ macro_rules! impl_vector_element {
fn __unique_ptr_release(this: *mut MaybeUninit<*mut c_void>) -> *mut CxxVector<$ty>;
}
}
__unique_ptr_release(&mut repr)
unsafe { __unique_ptr_release(&mut repr) }
}
#[doc(hidden)]
unsafe fn __unique_ptr_drop(mut repr: MaybeUninit<*mut c_void>) {
@ -470,7 +474,7 @@ macro_rules! impl_vector_element {
fn __unique_ptr_drop(this: *mut MaybeUninit<*mut c_void>);
}
}
__unique_ptr_drop(&mut repr);
unsafe { __unique_ptr_drop(&mut repr) }
}
}
};

View File

@ -366,6 +366,8 @@
#![no_std]
#![doc(html_root_url = "https://docs.rs/cxx/1.0.53")]
#![deny(improper_ctypes, improper_ctypes_definitions, missing_docs)]
#![cfg_attr(not(no_unsafe_op_in_unsafe_fn_lint), deny(unsafe_op_in_unsafe_fn))]
#![cfg_attr(no_unsafe_op_in_unsafe_fn_lint, allow(unused_unsafe))]
#![allow(non_camel_case_types)]
#![allow(
clippy::cognitive_complexity,

View File

@ -28,16 +28,16 @@ where
{
match result {
Ok(ok) => {
ptr::write(ret, ok);
unsafe { ptr::write(ret, ok) }
Result { ok: ptr::null() }
}
Err(err) => to_c_error(err.to_string()),
Err(err) => unsafe { to_c_error(err.to_string()) },
}
}
unsafe fn to_c_error(msg: String) -> Result {
let mut msg = msg;
msg.as_mut_vec().push(b'\0');
unsafe { msg.as_mut_vec() }.push(b'\0');
let ptr = msg.as_ptr();
let len = msg.len();
@ -46,22 +46,24 @@ unsafe fn to_c_error(msg: String) -> Result {
fn error(ptr: *const u8, len: usize) -> NonNull<u8>;
}
let copy = error(ptr, len);
let copy = unsafe { error(ptr, len) };
let err = PtrLen { ptr: copy, len };
Result { err }
}
impl Result {
pub unsafe fn exception(self) -> StdResult<(), Exception> {
if self.ok.is_null() {
Ok(())
} else {
let err = self.err;
let slice = slice::from_raw_parts_mut(err.ptr.as_ptr(), err.len);
let s = str::from_utf8_unchecked_mut(slice);
Err(Exception {
what: Box::from_raw(s),
})
unsafe {
if self.ok.is_null() {
Ok(())
} else {
let err = self.err;
let slice = slice::from_raw_parts_mut(err.ptr.as_ptr(), err.len);
let s = str::from_utf8_unchecked_mut(slice);
Err(Exception {
what: Box::from_raw(s),
})
}
}
}
}

View File

@ -26,13 +26,13 @@ impl RustSlice {
pub unsafe fn as_slice<'a, T>(self) -> &'a [T] {
let ptr = self.as_non_null_ptr().as_ptr();
let len = self.len();
slice::from_raw_parts(ptr, len)
unsafe { slice::from_raw_parts(ptr, len) }
}
pub unsafe fn as_mut_slice<'a, T>(self) -> &'a mut [T] {
let ptr = self.as_non_null_ptr().as_ptr();
let len = self.len();
slice::from_raw_parts_mut(ptr, len)
unsafe { slice::from_raw_parts_mut(ptr, len) }
}
pub(crate) fn from_raw_parts<T>(ptr: NonNull<T>, len: usize) -> Self {

View File

@ -17,8 +17,10 @@ impl RustStr {
}
pub unsafe fn as_str<'a>(self) -> &'a str {
let repr = mem::transmute::<RustStr, NonNull<str>>(self);
&*repr.as_ptr()
unsafe {
let repr = mem::transmute::<RustStr, NonNull<str>>(self);
&*repr.as_ptr()
}
}
}

View File

@ -65,7 +65,7 @@ impl<T> RustVec<T> {
}
pub unsafe fn set_len(&mut self, len: usize) {
self.as_mut_vec().set_len(len);
unsafe { self.as_mut_vec().set_len(len) }
}
}

View File

@ -216,7 +216,7 @@ macro_rules! impl_shared_ptr_target {
fn __null(new: *mut c_void);
}
}
__null(new);
unsafe { __null(new) }
}
#[doc(hidden)]
unsafe fn __new(value: Self, new: *mut c_void) {
@ -226,7 +226,7 @@ macro_rules! impl_shared_ptr_target {
fn __uninit(new: *mut c_void) -> *mut c_void;
}
}
__uninit(new).cast::<$ty>().write(value);
unsafe { __uninit(new).cast::<$ty>().write(value) }
}
#[doc(hidden)]
unsafe fn __clone(this: *const c_void, new: *mut c_void) {
@ -236,7 +236,7 @@ macro_rules! impl_shared_ptr_target {
fn __clone(this: *const c_void, new: *mut c_void);
}
}
__clone(this, new);
unsafe { __clone(this, new) }
}
#[doc(hidden)]
unsafe fn __get(this: *const c_void) -> *const Self {
@ -246,7 +246,7 @@ macro_rules! impl_shared_ptr_target {
fn __get(this: *const c_void) -> *const c_void;
}
}
__get(this).cast()
unsafe { __get(this) }.cast()
}
#[doc(hidden)]
unsafe fn __drop(this: *mut c_void) {
@ -256,7 +256,7 @@ macro_rules! impl_shared_ptr_target {
fn __drop(this: *mut c_void);
}
}
__drop(this);
unsafe { __drop(this) }
}
}
};

View File

@ -4,7 +4,7 @@ use core::slice;
#[export_name = "cxxbridge1$exception"]
unsafe extern "C" fn exception(ptr: *const u8, len: usize) -> *const u8 {
let slice = slice::from_raw_parts(ptr, len);
let slice = unsafe { slice::from_raw_parts(ptr, len) };
let boxed = String::from_utf8_lossy(slice).into_owned().into_boxed_str();
Box::leak(boxed).as_ptr()
}

View File

@ -4,8 +4,9 @@ use core::ptr::{self, NonNull};
#[export_name = "cxxbridge1$slice$new"]
unsafe extern "C" fn slice_new(this: &mut MaybeUninit<RustSlice>, ptr: NonNull<()>, len: usize) {
let this = this.as_mut_ptr();
let rust_slice = RustSlice::from_raw_parts(ptr, len);
ptr::write(this.as_mut_ptr(), rust_slice);
unsafe { ptr::write(this, rust_slice) }
}
#[export_name = "cxxbridge1$slice$ptr"]

View File

@ -6,20 +6,24 @@ use core::str;
#[export_name = "cxxbridge1$str$new"]
unsafe extern "C" fn str_new(this: &mut MaybeUninit<&str>) {
ptr::write(this.as_mut_ptr(), "");
let this = this.as_mut_ptr();
unsafe { ptr::write(this, "") }
}
#[export_name = "cxxbridge1$str$ref"]
unsafe extern "C" fn str_ref<'a>(this: &mut MaybeUninit<&'a str>, string: &'a String) {
ptr::write(this.as_mut_ptr(), string.as_str());
let this = this.as_mut_ptr();
let s = string.as_str();
unsafe { ptr::write(this, s) }
}
#[export_name = "cxxbridge1$str$from"]
unsafe extern "C" fn str_from(this: &mut MaybeUninit<&str>, ptr: *const u8, len: usize) -> bool {
let slice = slice::from_raw_parts(ptr, len);
let slice = unsafe { slice::from_raw_parts(ptr, len) };
match str::from_utf8(slice) {
Ok(s) => {
ptr::write(this.as_mut_ptr(), s);
let this = this.as_mut_ptr();
unsafe { ptr::write(this, s) }
true
}
Err(_) => false,

View File

@ -7,12 +7,16 @@ use core::str;
#[export_name = "cxxbridge1$string$new"]
unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) {
ptr::write(this.as_mut_ptr(), String::new());
let this = this.as_mut_ptr();
let new = String::new();
unsafe { ptr::write(this, new) }
}
#[export_name = "cxxbridge1$string$clone"]
unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) {
ptr::write(this.as_mut_ptr(), other.clone());
let this = this.as_mut_ptr();
let clone = other.clone();
unsafe { ptr::write(this, clone) }
}
#[export_name = "cxxbridge1$string$from_utf8"]
@ -21,10 +25,12 @@ unsafe extern "C" fn string_from_utf8(
ptr: *const u8,
len: usize,
) -> bool {
let slice = slice::from_raw_parts(ptr, len);
let slice = unsafe { slice::from_raw_parts(ptr, len) };
match str::from_utf8(slice) {
Ok(s) => {
ptr::write(this.as_mut_ptr(), s.to_owned());
let this = this.as_mut_ptr();
let owned = s.to_owned();
unsafe { ptr::write(this, owned) }
true
}
Err(_) => false,
@ -37,10 +43,11 @@ unsafe extern "C" fn string_from_utf16(
ptr: *const u16,
len: usize,
) -> bool {
let slice = slice::from_raw_parts(ptr, len);
let slice = unsafe { slice::from_raw_parts(ptr, len) };
match String::from_utf16(slice) {
Ok(s) => {
ptr::write(this.as_mut_ptr(), s);
let this = this.as_mut_ptr();
unsafe { ptr::write(this, s) }
true
}
Err(_) => false,
@ -49,7 +56,7 @@ unsafe extern "C" fn string_from_utf16(
#[export_name = "cxxbridge1$string$drop"]
unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) {
ManuallyDrop::drop(this);
unsafe { ManuallyDrop::drop(this) }
}
#[export_name = "cxxbridge1$string$ptr"]

View File

@ -15,43 +15,43 @@ macro_rules! rust_vec_shims {
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$new")]
unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
ptr::write(this, RustVec::new());
unsafe { ptr::write(this, RustVec::new()) }
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$drop")]
unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
ptr::drop_in_place(this);
unsafe { ptr::drop_in_place(this) }
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$len")]
unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize {
(*this).len()
unsafe { &*this }.len()
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$capacity")]
unsafe extern "C" fn __capacity(this: *const RustVec<$ty>) -> usize {
(*this).capacity()
unsafe { &*this }.capacity()
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$data")]
unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty {
(*this).as_ptr()
unsafe { &*this }.as_ptr()
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$reserve_total")]
unsafe extern "C" fn __reserve_total(this: *mut RustVec<$ty>, new_cap: usize) {
(*this).reserve_total(new_cap);
unsafe { &mut *this }.reserve_total(new_cap);
}
}
attr! {
#[export_name = concat!("cxxbridge1$rust_vec$", $segment, "$set_len")]
unsafe extern "C" fn __set_len(this: *mut RustVec<$ty>, len: usize) {
(*this).set_len(len);
unsafe { (*this).set_len(len) }
}
}
};

View File

@ -103,7 +103,7 @@ where
/// twice on the same raw pointer.
pub unsafe fn from_raw(raw: *mut T) -> Self {
UniquePtr {
repr: T::__raw(raw),
repr: unsafe { T::__raw(raw) },
ty: PhantomData,
}
}
@ -256,20 +256,20 @@ unsafe impl UniquePtrTarget for CxxString {
#[doc(hidden)]
unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
let mut repr = MaybeUninit::uninit();
unique_ptr_std_string_raw(&mut repr, raw);
unsafe { unique_ptr_std_string_raw(&mut repr, raw) }
repr
}
#[doc(hidden)]
unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
unique_ptr_std_string_get(&repr)
unsafe { unique_ptr_std_string_get(&repr) }
}
#[doc(hidden)]
unsafe fn __release(mut repr: MaybeUninit<*mut c_void>) -> *mut Self {
unique_ptr_std_string_release(&mut repr)
unsafe { unique_ptr_std_string_release(&mut repr) }
}
#[doc(hidden)]
unsafe fn __drop(mut repr: MaybeUninit<*mut c_void>) {
unique_ptr_std_string_drop(&mut repr);
unsafe { unique_ptr_std_string_drop(&mut repr) }
}
}
@ -287,18 +287,18 @@ where
}
#[doc(hidden)]
unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
T::__unique_ptr_raw(raw)
unsafe { T::__unique_ptr_raw(raw) }
}
#[doc(hidden)]
unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
T::__unique_ptr_get(repr)
unsafe { T::__unique_ptr_get(repr) }
}
#[doc(hidden)]
unsafe fn __release(repr: MaybeUninit<*mut c_void>) -> *mut Self {
T::__unique_ptr_release(repr)
unsafe { T::__unique_ptr_release(repr) }
}
#[doc(hidden)]
unsafe fn __drop(repr: MaybeUninit<*mut c_void>) {
T::__unique_ptr_drop(repr);
unsafe { T::__unique_ptr_drop(repr) }
}
}

View File

@ -126,7 +126,7 @@ macro_rules! impl_weak_ptr_target {
fn __null(new: *mut c_void);
}
}
__null(new);
unsafe { __null(new) }
}
#[doc(hidden)]
unsafe fn __clone(this: *const c_void, new: *mut c_void) {
@ -136,7 +136,7 @@ macro_rules! impl_weak_ptr_target {
fn __clone(this: *const c_void, new: *mut c_void);
}
}
__clone(this, new);
unsafe { __clone(this, new) }
}
#[doc(hidden)]
unsafe fn __downgrade(shared: *const c_void, weak: *mut c_void) {
@ -146,7 +146,7 @@ macro_rules! impl_weak_ptr_target {
fn __downgrade(shared: *const c_void, weak: *mut c_void);
}
}
__downgrade(shared, weak);
unsafe { __downgrade(shared, weak) }
}
#[doc(hidden)]
unsafe fn __upgrade(weak: *const c_void, shared: *mut c_void) {
@ -156,7 +156,7 @@ macro_rules! impl_weak_ptr_target {
fn __upgrade(weak: *const c_void, shared: *mut c_void);
}
}
__upgrade(weak, shared);
unsafe { __upgrade(weak, shared) }
}
#[doc(hidden)]
unsafe fn __drop(this: *mut c_void) {
@ -166,7 +166,7 @@ macro_rules! impl_weak_ptr_target {
fn __drop(this: *mut c_void);
}
}
__drop(this);
unsafe { __drop(this) }
}
}
};