diff --git a/.github/workflows/bindgen.yml b/.github/workflows/bindgen.yml index f292d001aa..bc8477d503 100644 --- a/.github/workflows/bindgen.yml +++ b/.github/workflows/bindgen.yml @@ -89,8 +89,6 @@ jobs: - name: Test expectations run: cd bindgen-tests/tests/expectations && cargo test - env: - RUSTFLAGS: "-D unsafe_op_in_unsafe_fn -D unused_unsafe" test: runs-on: ${{matrix.os}} diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c9889f263..0664a6d8c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,8 @@ ## Added ## Changed + * Only wrap unsafe operations in unsafe blocks if the `--wrap_unsafe_ops` + option is enabled. * Replace the `name: &str` argument for `ParseCallbacks::add_derives` by `info: DeriveInfo`. diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index 426e5cf795..f7fccc4ff4 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -562,6 +562,9 @@ where .value_name("override") .multiple_occurrences(true) .number_of_values(1), + Arg::new("wrap-unsafe-ops") + .long("wrap-unsafe-ops") + .help("Wrap unsafe operations in unsafe blocks."), Arg::new("V") .long("version") .help("Prints the version, and exits"), @@ -1085,5 +1088,9 @@ where } } + if matches.is_present("wrap-unsafe-ops") { + builder = builder.wrap_unsafe_ops(true); + } + Ok((builder, output, verbose)) } diff --git a/bindgen-tests/tests/expectations/tests/16-byte-alignment_1_0.rs b/bindgen-tests/tests/expectations/tests/16-byte-alignment_1_0.rs index ac6beee12d..369e2c75ea 100644 --- a/bindgen-tests/tests/expectations/tests/16-byte-alignment_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/16-byte-alignment_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/allowlist-file.rs b/bindgen-tests/tests/expectations/tests/allowlist-file.rs index db35c22654..3b72fb75eb 100644 --- a/bindgen-tests/tests/expectations/tests/allowlist-file.rs +++ b/bindgen-tests/tests/expectations/tests/allowlist-file.rs @@ -41,7 +41,7 @@ extern "C" { impl someClass { #[inline] pub unsafe fn somePublicMethod(&mut self, foo: ::std::os::raw::c_int) { - unsafe { someClass_somePublicMethod(self, foo) } + someClass_somePublicMethod(self, foo) } } extern "C" { diff --git a/bindgen-tests/tests/expectations/tests/anon_struct_in_union_1_0.rs b/bindgen-tests/tests/expectations/tests/anon_struct_in_union_1_0.rs index 782ebdb4bb..cbc5ac9b3f 100644 --- a/bindgen-tests/tests/expectations/tests/anon_struct_in_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/anon_struct_in_union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs b/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs index ea6456bd75..63273e608f 100644 --- a/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result.rs b/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result.rs index f60d564a9a..12d5eab04b 100644 --- a/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result.rs +++ b/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result.rs @@ -38,7 +38,7 @@ impl Foo { &mut self, arg1: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { Foo_foo(self, arg1) } + Foo_foo(self, arg1) } } extern "C" { diff --git a/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs b/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs index 317d52297c..d860acd1b4 100644 --- a/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs +++ b/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_no_attribute_detection.rs @@ -36,7 +36,7 @@ impl Foo { &mut self, arg1: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { Foo_foo(self, arg1) } + Foo_foo(self, arg1) } } extern "C" { diff --git a/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_pre_1_27.rs b/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_pre_1_27.rs index 317d52297c..d860acd1b4 100644 --- a/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_pre_1_27.rs +++ b/bindgen-tests/tests/expectations/tests/attribute_warn_unused_result_pre_1_27.rs @@ -36,7 +36,7 @@ impl Foo { &mut self, arg1: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { Foo_foo(self, arg1) } + Foo_foo(self, arg1) } } extern "C" { diff --git a/bindgen-tests/tests/expectations/tests/bindgen-union-inside-namespace.rs b/bindgen-tests/tests/expectations/tests/bindgen-union-inside-namespace.rs index e143e415eb..8a091cb5e2 100644 --- a/bindgen-tests/tests/expectations/tests/bindgen-union-inside-namespace.rs +++ b/bindgen-tests/tests/expectations/tests/bindgen-union-inside-namespace.rs @@ -16,11 +16,11 @@ pub mod root { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs b/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs index 91efa9b9da..e9c1a76d1c 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield-method-same-name.rs @@ -151,14 +151,14 @@ impl Foo { } #[inline] pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char { - unsafe { Foo_type(self) } + Foo_type(self) } #[inline] pub unsafe fn set_type_(&mut self, c: ::std::os::raw::c_char) { - unsafe { Foo_set_type_(self, c) } + Foo_set_type_(self, c) } #[inline] pub unsafe fn set_type(&mut self, c: ::std::os::raw::c_char) { - unsafe { Foo_set_type(self, c) } + Foo_set_type(self, c) } } diff --git a/bindgen-tests/tests/expectations/tests/blocklist-methods.rs b/bindgen-tests/tests/expectations/tests/blocklist-methods.rs index 5b625f0a4e..eadc0694ed 100644 --- a/bindgen-tests/tests/expectations/tests/blocklist-methods.rs +++ b/bindgen-tests/tests/expectations/tests/blocklist-methods.rs @@ -30,6 +30,6 @@ extern "C" { impl Foo { #[inline] pub unsafe fn foo(&mut self) -> ::std::os::raw::c_int { - unsafe { Foo_foo(self) } + Foo_foo(self) } } diff --git a/bindgen-tests/tests/expectations/tests/class.rs b/bindgen-tests/tests/expectations/tests/class.rs index 9b2158fedb..8542135b07 100644 --- a/bindgen-tests/tests/expectations/tests/class.rs +++ b/bindgen-tests/tests/expectations/tests/class.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { @@ -490,18 +490,18 @@ extern "C" { impl RealAbstractionWithTonsOfMethods { #[inline] pub unsafe fn bar(&self) { - unsafe { RealAbstractionWithTonsOfMethods_bar(self) } + RealAbstractionWithTonsOfMethods_bar(self) } #[inline] pub unsafe fn bar1(&mut self) { - unsafe { RealAbstractionWithTonsOfMethods_bar1(self) } + RealAbstractionWithTonsOfMethods_bar1(self) } #[inline] pub unsafe fn bar2(&mut self, foo: ::std::os::raw::c_int) { - unsafe { RealAbstractionWithTonsOfMethods_bar2(self, foo) } + RealAbstractionWithTonsOfMethods_bar2(self, foo) } #[inline] pub unsafe fn sta() { - unsafe { RealAbstractionWithTonsOfMethods_sta() } + RealAbstractionWithTonsOfMethods_sta() } } diff --git a/bindgen-tests/tests/expectations/tests/class_1_0.rs b/bindgen-tests/tests/expectations/tests/class_1_0.rs index 5163bae9be..f6c3021eb3 100644 --- a/bindgen-tests/tests/expectations/tests/class_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/class_1_0.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { @@ -44,11 +44,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { @@ -541,18 +541,18 @@ impl Clone for RealAbstractionWithTonsOfMethods { impl RealAbstractionWithTonsOfMethods { #[inline] pub unsafe fn bar(&self) { - unsafe { RealAbstractionWithTonsOfMethods_bar(self) } + RealAbstractionWithTonsOfMethods_bar(self) } #[inline] pub unsafe fn bar1(&mut self) { - unsafe { RealAbstractionWithTonsOfMethods_bar1(self) } + RealAbstractionWithTonsOfMethods_bar1(self) } #[inline] pub unsafe fn bar2(&mut self, foo: ::std::os::raw::c_int) { - unsafe { RealAbstractionWithTonsOfMethods_bar2(self, foo) } + RealAbstractionWithTonsOfMethods_bar2(self, foo) } #[inline] pub unsafe fn sta() { - unsafe { RealAbstractionWithTonsOfMethods_sta() } + RealAbstractionWithTonsOfMethods_sta() } } diff --git a/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs b/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs index cf2122c625..42656079df 100644 --- a/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/class_with_typedef.rs b/bindgen-tests/tests/expectations/tests/class_with_typedef.rs index 033b0c3c0b..65e86b2709 100644 --- a/bindgen-tests/tests/expectations/tests/class_with_typedef.rs +++ b/bindgen-tests/tests/expectations/tests/class_with_typedef.rs @@ -93,19 +93,19 @@ impl Default for C { impl C { #[inline] pub unsafe fn method(&mut self, c: C_MyInt) { - unsafe { C_method(self, c) } + C_method(self, c) } #[inline] pub unsafe fn methodRef(&mut self, c: *mut C_MyInt) { - unsafe { C_methodRef(self, c) } + C_methodRef(self, c) } #[inline] pub unsafe fn complexMethodRef(&mut self, c: *mut C_Lookup) { - unsafe { C_complexMethodRef(self, c) } + C_complexMethodRef(self, c) } #[inline] pub unsafe fn anotherMethod(&mut self, c: AnotherInt) { - unsafe { C_anotherMethod(self, c) } + C_anotherMethod(self, c) } } #[repr(C)] diff --git a/bindgen-tests/tests/expectations/tests/constructor-tp.rs b/bindgen-tests/tests/expectations/tests/constructor-tp.rs index 35c096d704..4b339dc3c4 100644 --- a/bindgen-tests/tests/expectations/tests/constructor-tp.rs +++ b/bindgen-tests/tests/expectations/tests/constructor-tp.rs @@ -35,10 +35,8 @@ extern "C" { impl Bar { #[inline] pub unsafe fn new() -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - Bar_Bar(__bindgen_tmp.as_mut_ptr()); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + Bar_Bar(__bindgen_tmp.as_mut_ptr()); + __bindgen_tmp.assume_init() } } diff --git a/bindgen-tests/tests/expectations/tests/constructors.rs b/bindgen-tests/tests/expectations/tests/constructors.rs index 615af667d0..2f13effbef 100644 --- a/bindgen-tests/tests/expectations/tests/constructors.rs +++ b/bindgen-tests/tests/expectations/tests/constructors.rs @@ -37,19 +37,15 @@ extern "C" { impl TestOverload { #[inline] pub unsafe fn new(arg1: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - TestOverload_TestOverload(__bindgen_tmp.as_mut_ptr(), arg1); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + TestOverload_TestOverload(__bindgen_tmp.as_mut_ptr(), arg1); + __bindgen_tmp.assume_init() } #[inline] pub unsafe fn new1(arg1: f64) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - TestOverload_TestOverload1(__bindgen_tmp.as_mut_ptr(), arg1); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + TestOverload_TestOverload1(__bindgen_tmp.as_mut_ptr(), arg1); + __bindgen_tmp.assume_init() } } #[repr(C)] @@ -77,10 +73,8 @@ extern "C" { impl TestPublicNoArgs { #[inline] pub unsafe fn new() -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - TestPublicNoArgs_TestPublicNoArgs(__bindgen_tmp.as_mut_ptr()); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + TestPublicNoArgs_TestPublicNoArgs(__bindgen_tmp.as_mut_ptr()); + __bindgen_tmp.assume_init() } } diff --git a/bindgen-tests/tests/expectations/tests/constructors_1_33.rs b/bindgen-tests/tests/expectations/tests/constructors_1_33.rs index c9818ccaab..b5d333325b 100644 --- a/bindgen-tests/tests/expectations/tests/constructors_1_33.rs +++ b/bindgen-tests/tests/expectations/tests/constructors_1_33.rs @@ -39,19 +39,15 @@ extern "C" { impl TestOverload { #[inline] pub unsafe fn new(arg1: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::uninitialized(); - TestOverload_TestOverload(&mut __bindgen_tmp, arg1); - __bindgen_tmp - } + let mut __bindgen_tmp = ::std::mem::uninitialized(); + TestOverload_TestOverload(&mut __bindgen_tmp, arg1); + __bindgen_tmp } #[inline] pub unsafe fn new1(arg1: f64) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::uninitialized(); - TestOverload_TestOverload1(&mut __bindgen_tmp, arg1); - __bindgen_tmp - } + let mut __bindgen_tmp = ::std::mem::uninitialized(); + TestOverload_TestOverload1(&mut __bindgen_tmp, arg1); + __bindgen_tmp } } #[repr(C)] @@ -79,10 +75,8 @@ extern "C" { impl TestPublicNoArgs { #[inline] pub unsafe fn new() -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::uninitialized(); - TestPublicNoArgs_TestPublicNoArgs(&mut __bindgen_tmp); - __bindgen_tmp - } + let mut __bindgen_tmp = ::std::mem::uninitialized(); + TestPublicNoArgs_TestPublicNoArgs(&mut __bindgen_tmp); + __bindgen_tmp } } diff --git a/bindgen-tests/tests/expectations/tests/deleted-function.rs b/bindgen-tests/tests/expectations/tests/deleted-function.rs index 69a6961803..96967bb461 100644 --- a/bindgen-tests/tests/expectations/tests/deleted-function.rs +++ b/bindgen-tests/tests/expectations/tests/deleted-function.rs @@ -34,11 +34,11 @@ extern "C" { impl A { #[inline] pub unsafe fn inline_definition(&mut self) { - unsafe { A_inline_definition(self) } + A_inline_definition(self) } #[inline] pub unsafe fn out_of_line_definition(&mut self) { - unsafe { A_out_of_line_definition(self) } + A_out_of_line_definition(self) } } #[repr(C)] @@ -84,10 +84,8 @@ extern "C" { impl C { #[inline] pub unsafe fn new(arg1: *mut C) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - C_C(__bindgen_tmp.as_mut_ptr(), arg1); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + C_C(__bindgen_tmp.as_mut_ptr(), arg1); + __bindgen_tmp.assume_init() } } diff --git a/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs b/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs index 97a539d91d..ea6621dd1c 100644 --- a/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs +++ b/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs @@ -202,14 +202,14 @@ impl Foo { } #[inline] pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char { - unsafe { Foo_type(self) } + Foo_type(self) } #[inline] pub unsafe fn set_type_(&mut self, c: ::std::os::raw::c_char) { - unsafe { Foo_set_type_(self, c) } + Foo_set_type_(self, c) } #[inline] pub unsafe fn set_type(&mut self, c: ::std::os::raw::c_char) { - unsafe { Foo_set_type(self, c) } + Foo_set_type(self, c) } } diff --git a/bindgen-tests/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs b/bindgen-tests/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs index 067aab36f8..06c8da1bad 100644 --- a/bindgen-tests/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs +++ b/bindgen-tests/tests/expectations/tests/derive-hash-struct-with-incomplete-array.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/derive-partialeq-union_1_0.rs b/bindgen-tests/tests/expectations/tests/derive-partialeq-union_1_0.rs index 604aa28d44..a53b9ba8e6 100644 --- a/bindgen-tests/tests/expectations/tests/derive-partialeq-union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/derive-partialeq-union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/disable-untagged-union.rs b/bindgen-tests/tests/expectations/tests/disable-untagged-union.rs index 78cbe47fa4..515f496b87 100644 --- a/bindgen-tests/tests/expectations/tests/disable-untagged-union.rs +++ b/bindgen-tests/tests/expectations/tests/disable-untagged-union.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs b/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs index 8fdffe56e4..829022a279 100644 --- a/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs +++ b/bindgen-tests/tests/expectations/tests/duplicated-definition-count.rs @@ -50,7 +50,7 @@ impl BitStream { inputByteArray: *const ::std::os::raw::c_char, numberOfBytes: ::std::os::raw::c_uint, ) { - unsafe { BitStream_Write(self, inputByteArray, numberOfBytes) } + BitStream_Write(self, inputByteArray, numberOfBytes) } #[inline] pub unsafe fn Write1( @@ -58,10 +58,10 @@ impl BitStream { bitStream: *mut BitStream, numberOfBits: ::std::os::raw::c_uint, ) { - unsafe { BitStream_Write1(self, bitStream, numberOfBits) } + BitStream_Write1(self, bitStream, numberOfBits) } #[inline] pub unsafe fn Write11(&mut self) { - unsafe { BitStream_Write11(self) } + BitStream_Write11(self) } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs index 5ad4b23f7b..c834752e3c 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs @@ -20,7 +20,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -29,8 +29,8 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym)?; - let baz = unsafe { __library.get(b"baz\0") }.map(|sym| *sym)?; + let foo = __library.get(b"foo\0").map(|sym| *sym)?; + let baz = __library.get(b"baz\0").map(|sym| *sym)?; Ok(TestLib { __library, foo, @@ -48,9 +48,9 @@ impl TestLib { x: ::std::os::raw::c_int, y: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { (self.foo)(x, y) } + (self.foo)(x, y) } pub unsafe fn baz(&self) -> ::std::os::raw::c_int { - unsafe { (self.baz)() } + (self.baz)() } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs index 723f44ad09..e46ffd49e0 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs @@ -23,7 +23,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -32,9 +32,9 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym)?; - let bar = unsafe { __library.get(b"bar\0") }.map(|sym| *sym)?; - let baz = unsafe { __library.get(b"baz\0") }.map(|sym| *sym)?; + let foo = __library.get(b"foo\0").map(|sym| *sym)?; + let bar = __library.get(b"bar\0").map(|sym| *sym)?; + let baz = __library.get(b"baz\0").map(|sym| *sym)?; Ok(TestLib { __library, foo, @@ -47,15 +47,15 @@ impl TestLib { x: ::std::os::raw::c_int, y: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { (self.foo)(x, y) } + (self.foo)(x, y) } pub unsafe fn bar( &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { (self.bar)(x) } + (self.bar)(x) } pub unsafe fn baz(&self) -> ::std::os::raw::c_int { - unsafe { (self.baz)() } + (self.baz)() } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs index 5864ba0a8d..cae5bd6937 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs @@ -32,7 +32,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -41,9 +41,9 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym); - let bar = unsafe { __library.get(b"bar\0") }.map(|sym| *sym); - let baz = unsafe { __library.get(b"baz\0") }.map(|sym| *sym); + let foo = __library.get(b"foo\0").map(|sym| *sym); + let bar = __library.get(b"bar\0").map(|sym| *sym); + let baz = __library.get(b"baz\0").map(|sym| *sym); Ok(TestLib { __library, foo, @@ -56,19 +56,15 @@ impl TestLib { x: ::std::os::raw::c_int, y: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { - (self.foo.as_ref().expect("Expected function, got error."))(x, y) - } + (self.foo.as_ref().expect("Expected function, got error."))(x, y) } pub unsafe fn bar( &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { - (self.bar.as_ref().expect("Expected function, got error."))(x) - } + (self.bar.as_ref().expect("Expected function, got error."))(x) } pub unsafe fn baz(&self) -> ::std::os::raw::c_int { - unsafe { (self.baz.as_ref().expect("Expected function, got error."))() } + (self.baz.as_ref().expect("Expected function, got error."))() } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs index 6c9ea27496..06e67ed791 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs @@ -20,7 +20,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -29,8 +29,8 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym); - let foo1 = unsafe { __library.get(b"foo1\0") }.map(|sym| *sym); + let foo = __library.get(b"foo\0").map(|sym| *sym); + let foo1 = __library.get(b"foo1\0").map(|sym| *sym); Ok(TestLib { __library, foo, @@ -41,13 +41,9 @@ impl TestLib { &self, x: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { - (self.foo.as_ref().expect("Expected function, got error."))(x) - } + (self.foo.as_ref().expect("Expected function, got error."))(x) } pub unsafe fn foo1(&self, x: f32) -> f32 { - unsafe { - (self.foo1.as_ref().expect("Expected function, got error."))(x) - } + (self.foo1.as_ref().expect("Expected function, got error."))(x) } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs index c9b2471419..97bb67abdc 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs @@ -34,7 +34,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -43,9 +43,9 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym); - let baz = unsafe { __library.get(b"baz\0") }.map(|sym| *sym); - let bazz = unsafe { __library.get(b"bazz\0") }.map(|sym| *sym); + let foo = __library.get(b"foo\0").map(|sym| *sym); + let baz = __library.get(b"baz\0").map(|sym| *sym); + let bazz = __library.get(b"bazz\0").map(|sym| *sym); Ok(TestLib { __library, foo, @@ -57,16 +57,12 @@ impl TestLib { &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { - (self.foo.as_ref().expect("Expected function, got error."))(x) - } + (self.foo.as_ref().expect("Expected function, got error."))(x) } pub unsafe fn baz( &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { - (self.baz.as_ref().expect("Expected function, got error."))(x) - } + (self.baz.as_ref().expect("Expected function, got error."))(x) } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs index 30f2be1d1e..af482ad938 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs @@ -46,19 +46,17 @@ extern "C" { impl X { #[inline] pub unsafe fn some_function(&mut self) { - unsafe { X_some_function(self) } + X_some_function(self) } #[inline] pub unsafe fn some_other_function(&mut self) { - unsafe { X_some_other_function(self) } + X_some_other_function(self) } #[inline] pub unsafe fn new(x: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - X_X(__bindgen_tmp.as_mut_ptr(), x); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + X_X(__bindgen_tmp.as_mut_ptr(), x); + __bindgen_tmp.assume_init() } } extern crate libloading; @@ -83,7 +81,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -92,8 +90,8 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym); - let bar = unsafe { __library.get(b"bar\0") }.map(|sym| *sym); + let foo = __library.get(b"foo\0").map(|sym| *sym); + let bar = __library.get(b"bar\0").map(|sym| *sym); Ok(TestLib { __library, foo, @@ -104,16 +102,12 @@ impl TestLib { &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { - (self.foo.as_ref().expect("Expected function, got error."))(x) - } + (self.foo.as_ref().expect("Expected function, got error."))(x) } pub unsafe fn bar( &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { - (self.bar.as_ref().expect("Expected function, got error."))(x) - } + (self.bar.as_ref().expect("Expected function, got error."))(x) } } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs index d928751892..514cff7317 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs @@ -46,19 +46,17 @@ extern "C" { impl A { #[inline] pub unsafe fn some_function(&mut self) { - unsafe { A_some_function(self) } + A_some_function(self) } #[inline] pub unsafe fn some_other_function(&mut self) { - unsafe { A_some_other_function(self) } + A_some_other_function(self) } #[inline] pub unsafe fn new(x: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - A_A(__bindgen_tmp.as_mut_ptr(), x); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + A_A(__bindgen_tmp.as_mut_ptr(), x); + __bindgen_tmp.assume_init() } } extern crate libloading; @@ -78,7 +76,7 @@ impl TestLib { P: AsRef<::std::ffi::OsStr>, { let library = ::libloading::Library::new(path)?; - unsafe { Self::from_library(library) } + Self::from_library(library) } pub unsafe fn from_library( library: L, @@ -87,8 +85,8 @@ impl TestLib { L: Into<::libloading::Library>, { let __library = library.into(); - let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym); - let bar = unsafe { __library.get(b"bar\0") }.map(|sym| *sym); + let foo = __library.get(b"foo\0").map(|sym| *sym); + let bar = __library.get(b"bar\0").map(|sym| *sym); Ok(TestLib { __library, foo, @@ -99,11 +97,9 @@ impl TestLib { &self, x: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int { - unsafe { - (self.foo.as_ref().expect("Expected function, got error."))(x) - } + (self.foo.as_ref().expect("Expected function, got error."))(x) } pub unsafe fn bar(&self) -> () { - unsafe { (self.bar.as_ref().expect("Expected function, got error."))() } + (self.bar.as_ref().expect("Expected function, got error."))() } } diff --git a/bindgen-tests/tests/expectations/tests/gen-constructors.rs b/bindgen-tests/tests/expectations/tests/gen-constructors.rs index 59af8ef30c..89b8637954 100644 --- a/bindgen-tests/tests/expectations/tests/gen-constructors.rs +++ b/bindgen-tests/tests/expectations/tests/gen-constructors.rs @@ -30,10 +30,8 @@ extern "C" { impl Foo { #[inline] pub unsafe fn new(a: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - Foo_Foo(__bindgen_tmp.as_mut_ptr(), a); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + Foo_Foo(__bindgen_tmp.as_mut_ptr(), a); + __bindgen_tmp.assume_init() } } diff --git a/bindgen-tests/tests/expectations/tests/gen-destructors.rs b/bindgen-tests/tests/expectations/tests/gen-destructors.rs index 4109520987..1711f76d0c 100644 --- a/bindgen-tests/tests/expectations/tests/gen-destructors.rs +++ b/bindgen-tests/tests/expectations/tests/gen-destructors.rs @@ -38,6 +38,6 @@ extern "C" { impl Foo { #[inline] pub unsafe fn destruct(&mut self) { - unsafe { Foo_Foo_destructor(self) } + Foo_Foo_destructor(self) } } diff --git a/bindgen-tests/tests/expectations/tests/generate-inline.rs b/bindgen-tests/tests/expectations/tests/generate-inline.rs index 9ac66b0f7b..3cb865a4d7 100644 --- a/bindgen-tests/tests/expectations/tests/generate-inline.rs +++ b/bindgen-tests/tests/expectations/tests/generate-inline.rs @@ -30,7 +30,7 @@ extern "C" { impl Foo { #[inline] pub unsafe fn bar() -> ::std::os::raw::c_int { - unsafe { Foo_bar() } + Foo_bar() } } extern "C" { diff --git a/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs b/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs index d1ae7ebd2b..18061ea12a 100644 --- a/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs +++ b/bindgen-tests/tests/expectations/tests/incomplete-array-padding.rs @@ -109,11 +109,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/issue-2019.rs b/bindgen-tests/tests/expectations/tests/issue-2019.rs index 89f1ac6268..294e9f0e83 100644 --- a/bindgen-tests/tests/expectations/tests/issue-2019.rs +++ b/bindgen-tests/tests/expectations/tests/issue-2019.rs @@ -38,7 +38,7 @@ extern "C" { impl A { #[inline] pub unsafe fn make() -> A { - unsafe { make() } + make() } } #[repr(C)] @@ -74,6 +74,6 @@ extern "C" { impl B { #[inline] pub unsafe fn make() -> B { - unsafe { make1() } + make1() } } diff --git a/bindgen-tests/tests/expectations/tests/issue-410.rs b/bindgen-tests/tests/expectations/tests/issue-410.rs index 739ddf29ce..ad7463c266 100644 --- a/bindgen-tests/tests/expectations/tests/issue-410.rs +++ b/bindgen-tests/tests/expectations/tests/issue-410.rs @@ -37,7 +37,7 @@ pub mod root { impl Value { #[inline] pub unsafe fn a(&mut self, arg1: root::JSWhyMagic) { - unsafe { Value_a(self, arg1) } + Value_a(self, arg1) } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-447.rs b/bindgen-tests/tests/expectations/tests/issue-447.rs index 0a6dd5e6b8..949db73b79 100644 --- a/bindgen-tests/tests/expectations/tests/issue-447.rs +++ b/bindgen-tests/tests/expectations/tests/issue-447.rs @@ -65,14 +65,12 @@ pub mod root { pub unsafe fn new( arg1: root::mozilla::detail::GuardObjectNotifier, ) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - JSAutoCompartment_JSAutoCompartment( - __bindgen_tmp.as_mut_ptr(), - arg1, - ); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + JSAutoCompartment_JSAutoCompartment( + __bindgen_tmp.as_mut_ptr(), + arg1, + ); + __bindgen_tmp.assume_init() } } } diff --git a/bindgen-tests/tests/expectations/tests/issue-493.rs b/bindgen-tests/tests/expectations/tests/issue-493.rs index 019982dbb9..61c7f7929e 100644 --- a/bindgen-tests/tests/expectations/tests/issue-493.rs +++ b/bindgen-tests/tests/expectations/tests/issue-493.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs b/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs index 12a1e7a267..1a0131c327 100644 --- a/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/issue-643-inner-struct.rs b/bindgen-tests/tests/expectations/tests/issue-643-inner-struct.rs index 03162c4c9f..b69aa16ef3 100644 --- a/bindgen-tests/tests/expectations/tests/issue-643-inner-struct.rs +++ b/bindgen-tests/tests/expectations/tests/issue-643-inner-struct.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/issue-807-opaque-types-methods-being-generated.rs b/bindgen-tests/tests/expectations/tests/issue-807-opaque-types-methods-being-generated.rs index dc0b7f08d9..f886952822 100644 --- a/bindgen-tests/tests/expectations/tests/issue-807-opaque-types-methods-being-generated.rs +++ b/bindgen-tests/tests/expectations/tests/issue-807-opaque-types-methods-being-generated.rs @@ -89,15 +89,13 @@ extern "C" { impl Opaque { #[inline] pub unsafe fn eleven_out_of_ten(&mut self) -> SuchWow { - unsafe { Opaque_eleven_out_of_ten(self) } + Opaque_eleven_out_of_ten(self) } #[inline] pub unsafe fn new(pup: Pupper) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - Opaque_Opaque(__bindgen_tmp.as_mut_ptr(), pup); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + Opaque_Opaque(__bindgen_tmp.as_mut_ptr(), pup); + __bindgen_tmp.assume_init() } } extern "C" { diff --git a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs index 32af443f29..b439499355 100644 --- a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs @@ -100,11 +100,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/layout_align.rs b/bindgen-tests/tests/expectations/tests/layout_align.rs index 718a8024a6..466e76862c 100644 --- a/bindgen-tests/tests/expectations/tests/layout_align.rs +++ b/bindgen-tests/tests/expectations/tests/layout_align.rs @@ -109,11 +109,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs b/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs index de814146e1..d7fa0a634a 100644 --- a/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs @@ -100,11 +100,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs b/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs index f0b312a037..075ef46ae4 100644 --- a/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs +++ b/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/layout_mbuf_1_0.rs b/bindgen-tests/tests/expectations/tests/layout_mbuf_1_0.rs index 3788d3d0a4..90f7ed09b2 100644 --- a/bindgen-tests/tests/expectations/tests/layout_mbuf_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/layout_mbuf_1_0.rs @@ -100,11 +100,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/libclang-5/objc_template.rs b/bindgen-tests/tests/expectations/tests/libclang-5/objc_template.rs index 85ca160107..53caa661c6 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-5/objc_template.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-5/objc_template.rs @@ -30,7 +30,7 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, get) } + msg_send!(*self, get) } } #[repr(transparent)] @@ -59,6 +59,6 @@ pub trait IFooMultiGeneric: where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, objectForKey: key) } + msg_send!(*self, objectForKey: key) } } diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/class.rs b/bindgen-tests/tests/expectations/tests/libclang-9/class.rs index 7d732bb09a..0d64a9ffc8 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/class.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/class.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { @@ -674,18 +674,18 @@ extern "C" { impl RealAbstractionWithTonsOfMethods { #[inline] pub unsafe fn bar(&self) { - unsafe { RealAbstractionWithTonsOfMethods_bar(self) } + RealAbstractionWithTonsOfMethods_bar(self) } #[inline] pub unsafe fn bar1(&mut self) { - unsafe { RealAbstractionWithTonsOfMethods_bar1(self) } + RealAbstractionWithTonsOfMethods_bar1(self) } #[inline] pub unsafe fn bar2(&mut self, foo: ::std::os::raw::c_int) { - unsafe { RealAbstractionWithTonsOfMethods_bar2(self, foo) } + RealAbstractionWithTonsOfMethods_bar2(self, foo) } #[inline] pub unsafe fn sta() { - unsafe { RealAbstractionWithTonsOfMethods_sta() } + RealAbstractionWithTonsOfMethods_sta() } } diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/class_1_0.rs b/bindgen-tests/tests/expectations/tests/libclang-9/class_1_0.rs index 4364c69a74..e8c2e07798 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/class_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/class_1_0.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { @@ -44,11 +44,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { @@ -725,18 +725,18 @@ impl Clone for RealAbstractionWithTonsOfMethods { impl RealAbstractionWithTonsOfMethods { #[inline] pub unsafe fn bar(&self) { - unsafe { RealAbstractionWithTonsOfMethods_bar(self) } + RealAbstractionWithTonsOfMethods_bar(self) } #[inline] pub unsafe fn bar1(&mut self) { - unsafe { RealAbstractionWithTonsOfMethods_bar1(self) } + RealAbstractionWithTonsOfMethods_bar1(self) } #[inline] pub unsafe fn bar2(&mut self, foo: ::std::os::raw::c_int) { - unsafe { RealAbstractionWithTonsOfMethods_bar2(self, foo) } + RealAbstractionWithTonsOfMethods_bar2(self, foo) } #[inline] pub unsafe fn sta() { - unsafe { RealAbstractionWithTonsOfMethods_sta() } + RealAbstractionWithTonsOfMethods_sta() } } diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/derive-hash-struct-with-incomplete-array.rs b/bindgen-tests/tests/expectations/tests/libclang-9/derive-hash-struct-with-incomplete-array.rs index 17154e4d1a..2af22cc4c9 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/derive-hash-struct-with-incomplete-array.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/derive-hash-struct-with-incomplete-array.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/incomplete-array-padding.rs b/bindgen-tests/tests/expectations/tests/libclang-9/incomplete-array-padding.rs index 441c3f69b2..1de963bf0f 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/incomplete-array-padding.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/incomplete-array-padding.rs @@ -109,11 +109,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/issue-643-inner-struct.rs b/bindgen-tests/tests/expectations/tests/libclang-9/issue-643-inner-struct.rs index 4161c6dbfc..ab402cfb7c 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/issue-643-inner-struct.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/issue-643-inner-struct.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/layout_align.rs b/bindgen-tests/tests/expectations/tests/libclang-9/layout_align.rs index df940912b3..f6a32704f1 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/layout_align.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/layout_align.rs @@ -109,11 +109,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { @@ -151,56 +151,56 @@ fn bindgen_test_layout_rte_kni_fifo() { concat!("Alignment of ", stringify!(rte_kni_fifo)) ); assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).write) as usize - ptr as usize }, - 0usize, - concat!( - "Offset of field: ", - stringify!(rte_kni_fifo), - "::", - stringify!(write) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).read) as usize - ptr as usize }, - 4usize, - concat!( - "Offset of field: ", - stringify!(rte_kni_fifo), - "::", - stringify!(read) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).len) as usize - ptr as usize }, - 8usize, - concat!( - "Offset of field: ", - stringify!(rte_kni_fifo), - "::", - stringify!(len) - ) - ); - assert_eq!( - unsafe { - ::std::ptr::addr_of!((*ptr).elem_size) as usize - ptr as usize - }, - 12usize, - concat!( - "Offset of field: ", - stringify!(rte_kni_fifo), - "::", - stringify!(elem_size) - ) - ); - assert_eq!( - unsafe { ::std::ptr::addr_of!((*ptr).buffer) as usize - ptr as usize }, - 16usize, - concat!( - "Offset of field: ", - stringify!(rte_kni_fifo), - "::", - stringify!(buffer) - ) + unsafe { ::std::ptr::addr_of!((*ptr).write) as usize - ptr as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(rte_kni_fifo), + "::", + stringify!(write) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).read) as usize - ptr as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(rte_kni_fifo), + "::", + stringify!(read) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).len) as usize - ptr as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(rte_kni_fifo), + "::", + stringify!(len) + ) + ); + assert_eq!( + unsafe { + ::std::ptr::addr_of!((*ptr).elem_size) as usize - ptr as usize + }, + 12usize, + concat!( + "Offset of field: ", + stringify!(rte_kni_fifo), + "::", + stringify!(elem_size) + ) + ); + assert_eq!( + unsafe { ::std::ptr::addr_of!((*ptr).buffer) as usize - ptr as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(rte_kni_fifo), + "::", + stringify!(buffer) + ) ); } impl Default for rte_kni_fifo { diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/objc_template.rs b/bindgen-tests/tests/expectations/tests/libclang-9/objc_template.rs index 90d9d9931b..3c615035ce 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/objc_template.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/objc_template.rs @@ -30,7 +30,7 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, get) } + msg_send!(*self, get) } } #[repr(transparent)] @@ -59,6 +59,6 @@ pub trait IFooMultiGeneric: where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, objectForKey: key) } + msg_send!(*self, objectForKey: key) } } diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/zero-sized-array.rs b/bindgen-tests/tests/expectations/tests/libclang-9/zero-sized-array.rs index 71a2d6d6aa..3066fac40a 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/zero-sized-array.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/zero-sized-array.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/method-mangling.rs b/bindgen-tests/tests/expectations/tests/method-mangling.rs index 5f8a847452..e4627ac8a7 100644 --- a/bindgen-tests/tests/expectations/tests/method-mangling.rs +++ b/bindgen-tests/tests/expectations/tests/method-mangling.rs @@ -30,6 +30,6 @@ extern "C" { impl Foo { #[inline] pub unsafe fn type_(&mut self) -> ::std::os::raw::c_int { - unsafe { Foo_type(self) } + Foo_type(self) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_allowlist.rs b/bindgen-tests/tests/expectations/tests/objc_allowlist.rs index 76971a5f38..370cab9332 100644 --- a/bindgen-tests/tests/expectations/tests/objc_allowlist.rs +++ b/bindgen-tests/tests/expectations/tests/objc_allowlist.rs @@ -14,13 +14,13 @@ pub trait PSomeProtocol: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, protocolMethod) } + msg_send!(*self, protocolMethod) } unsafe fn protocolClassMethod() where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(SomeProtocol), protocolClassMethod) } + msg_send!(class!(SomeProtocol), protocolClassMethod) } } #[repr(transparent)] @@ -45,13 +45,13 @@ pub trait IAllowlistMe: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, method) } + msg_send!(*self, method) } unsafe fn classMethod() where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(AllowlistMe), classMethod) } + msg_send!(class!(AllowlistMe), classMethod) } } impl AllowlistMe_InterestingCategory for AllowlistMe {} diff --git a/bindgen-tests/tests/expectations/tests/objc_blocklist.rs b/bindgen-tests/tests/expectations/tests/objc_blocklist.rs index f16eb0f929..7d5d19b083 100644 --- a/bindgen-tests/tests/expectations/tests/objc_blocklist.rs +++ b/bindgen-tests/tests/expectations/tests/objc_blocklist.rs @@ -31,12 +31,12 @@ pub trait ISomeClass: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, ambiguouslyBlockedMethod) } + msg_send!(*self, ambiguouslyBlockedMethod) } unsafe fn instanceMethod(&self) where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, instanceMethod) } + msg_send!(*self, instanceMethod) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_category.rs b/bindgen-tests/tests/expectations/tests/objc_category.rs index f05c2bbd9b..9d60233bed 100644 --- a/bindgen-tests/tests/expectations/tests/objc_category.rs +++ b/bindgen-tests/tests/expectations/tests/objc_category.rs @@ -30,7 +30,7 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, method) } + msg_send!(*self, method) } } impl Foo_BarCategory for Foo {} @@ -39,6 +39,6 @@ pub trait Foo_BarCategory: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, categoryMethod) } + msg_send!(*self, categoryMethod) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_class.rs b/bindgen-tests/tests/expectations/tests/objc_class.rs index 0fe8e254c3..b322bddcb6 100644 --- a/bindgen-tests/tests/expectations/tests/objc_class.rs +++ b/bindgen-tests/tests/expectations/tests/objc_class.rs @@ -33,6 +33,6 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, method) } + msg_send!(*self, method) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_class_method.rs b/bindgen-tests/tests/expectations/tests/objc_class_method.rs index 796698d271..29e70256ff 100644 --- a/bindgen-tests/tests/expectations/tests/objc_class_method.rs +++ b/bindgen-tests/tests/expectations/tests/objc_class_method.rs @@ -30,31 +30,31 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), method) } + msg_send!(class!(Foo), method) } unsafe fn methodWithInt_(foo: ::std::os::raw::c_int) where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), methodWithInt: foo) } + msg_send!(class!(Foo), methodWithInt: foo) } unsafe fn methodWithFoo_(foo: Foo) where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), methodWithFoo: foo) } + msg_send!(class!(Foo), methodWithFoo: foo) } unsafe fn methodReturningInt() -> ::std::os::raw::c_int where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), methodReturningInt) } + msg_send!(class!(Foo), methodReturningInt) } unsafe fn methodReturningFoo() -> Foo where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), methodReturningFoo) } + msg_send!(class!(Foo), methodReturningFoo) } unsafe fn methodWithArg1_andArg2_andArg3_( intvalue: ::std::os::raw::c_int, @@ -63,8 +63,6 @@ pub trait IFoo: Sized + std::ops::Deref { ) where ::Target: objc::Message + Sized, { - unsafe { - msg_send ! (class ! (Foo) , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue) - } + msg_send ! (class ! (Foo) , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_method.rs b/bindgen-tests/tests/expectations/tests/objc_method.rs index 8962413e24..593fd27738 100644 --- a/bindgen-tests/tests/expectations/tests/objc_method.rs +++ b/bindgen-tests/tests/expectations/tests/objc_method.rs @@ -30,31 +30,31 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, method) } + msg_send!(*self, method) } unsafe fn methodWithInt_(&self, foo: ::std::os::raw::c_int) where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, methodWithInt: foo) } + msg_send!(*self, methodWithInt: foo) } unsafe fn methodWithFoo_(&self, foo: Foo) where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, methodWithFoo: foo) } + msg_send!(*self, methodWithFoo: foo) } unsafe fn methodReturningInt(&self) -> ::std::os::raw::c_int where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, methodReturningInt) } + msg_send!(*self, methodReturningInt) } unsafe fn methodReturningFoo(&self) -> Foo where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, methodReturningFoo) } + msg_send!(*self, methodReturningFoo) } unsafe fn methodWithArg1_andArg2_andArg3_( &self, @@ -64,9 +64,7 @@ pub trait IFoo: Sized + std::ops::Deref { ) where ::Target: objc::Message + Sized, { - unsafe { - msg_send ! (* self , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue) - } + msg_send ! (* self , methodWithArg1 : intvalue andArg2 : ptr andArg3 : floatvalue) } unsafe fn methodWithAndWithoutKeywords_arg2Name__arg4Name_( &self, @@ -78,9 +76,7 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { - msg_send ! (* self , methodWithAndWithoutKeywords : arg1 arg2Name : arg2 arg3 : arg3 arg4Name : arg4) - } + msg_send ! (* self , methodWithAndWithoutKeywords : arg1 arg2Name : arg2 arg3 : arg3 arg4Name : arg4) } } pub type instancetype = id; diff --git a/bindgen-tests/tests/expectations/tests/objc_method_clash.rs b/bindgen-tests/tests/expectations/tests/objc_method_clash.rs index bd2df7d0d6..ac77cc19e3 100644 --- a/bindgen-tests/tests/expectations/tests/objc_method_clash.rs +++ b/bindgen-tests/tests/expectations/tests/objc_method_clash.rs @@ -30,12 +30,12 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, foo) } + msg_send!(*self, foo) } unsafe fn class_foo() where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), foo) } + msg_send!(class!(Foo), foo) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_pointer_return_types.rs b/bindgen-tests/tests/expectations/tests/objc_pointer_return_types.rs index d49f17c1a6..1ec8494d25 100644 --- a/bindgen-tests/tests/expectations/tests/objc_pointer_return_types.rs +++ b/bindgen-tests/tests/expectations/tests/objc_pointer_return_types.rs @@ -47,12 +47,12 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, methodUsingBar: my_bar) } + msg_send!(*self, methodUsingBar: my_bar) } unsafe fn methodReturningBar() -> Bar where ::Target: objc::Message + Sized, { - unsafe { msg_send!(class!(Foo), methodReturningBar) } + msg_send!(class!(Foo), methodReturningBar) } } diff --git a/bindgen-tests/tests/expectations/tests/objc_property_fnptr.rs b/bindgen-tests/tests/expectations/tests/objc_property_fnptr.rs index 90a3dc8dea..9f3fabd272 100644 --- a/bindgen-tests/tests/expectations/tests/objc_property_fnptr.rs +++ b/bindgen-tests/tests/expectations/tests/objc_property_fnptr.rs @@ -38,7 +38,7 @@ pub trait IFoo: Sized + std::ops::Deref { where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, func) } + msg_send!(*self, func) } unsafe fn setFunc_( &self, @@ -52,6 +52,6 @@ pub trait IFoo: Sized + std::ops::Deref { ) where ::Target: objc::Message + Sized, { - unsafe { msg_send!(*self, setFunc: func) } + msg_send!(*self, setFunc: func) } } diff --git a/bindgen-tests/tests/expectations/tests/parm-union.rs b/bindgen-tests/tests/expectations/tests/parm-union.rs index 4738b301e7..9f7dd20ad9 100644 --- a/bindgen-tests/tests/expectations/tests/parm-union.rs +++ b/bindgen-tests/tests/expectations/tests/parm-union.rs @@ -30,7 +30,7 @@ extern "C" { impl Struct { #[inline] pub unsafe fn Function(&mut self, arg1: *mut Union) { - unsafe { Struct_Function(self, arg1) } + Struct_Function(self, arg1) } } #[repr(C)] diff --git a/bindgen-tests/tests/expectations/tests/public-dtor.rs b/bindgen-tests/tests/expectations/tests/public-dtor.rs index 1d095b8d0b..979dd4b564 100644 --- a/bindgen-tests/tests/expectations/tests/public-dtor.rs +++ b/bindgen-tests/tests/expectations/tests/public-dtor.rs @@ -1,53 +1,53 @@ -#![allow( - dead_code, - non_snake_case, - non_camel_case_types, - non_upper_case_globals -)] - -#[repr(C)] -#[derive(Debug, Default)] -pub struct cv_Foo { - pub _address: u8, -} -#[test] -fn bindgen_test_layout_cv_Foo() { - assert_eq!( - ::std::mem::size_of::(), - 1usize, - concat!("Size of: ", stringify!(cv_Foo)) - ); - assert_eq!( - ::std::mem::align_of::(), - 1usize, - concat!("Alignment of ", stringify!(cv_Foo)) - ); -} -extern "C" { - #[link_name = "\u{1}_ZN2cv3FooD1Ev"] - pub fn cv_Foo_Foo_destructor(this: *mut cv_Foo); -} -impl cv_Foo { - #[inline] - pub unsafe fn destruct(&mut self) { - unsafe { cv_Foo_Foo_destructor(self) } - } -} -#[repr(C)] -#[derive(Debug, Default)] -pub struct cv_Bar { - pub _address: u8, -} -#[test] -fn bindgen_test_layout_cv_Bar() { - assert_eq!( - ::std::mem::size_of::(), - 1usize, - concat!("Size of: ", stringify!(cv_Bar)) - ); - assert_eq!( - ::std::mem::align_of::(), - 1usize, - concat!("Alignment of ", stringify!(cv_Bar)) - ); -} +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Debug, Default)] +pub struct cv_Foo { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_cv_Foo() { + assert_eq!( + ::std::mem::size_of::(), + 1usize, + concat!("Size of: ", stringify!(cv_Foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!("Alignment of ", stringify!(cv_Foo)) + ); +} +extern "C" { + #[link_name = "\u{1}_ZN2cv3FooD1Ev"] + pub fn cv_Foo_Foo_destructor(this: *mut cv_Foo); +} +impl cv_Foo { + #[inline] + pub unsafe fn destruct(&mut self) { + cv_Foo_Foo_destructor(self) + } +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct cv_Bar { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_cv_Bar() { + assert_eq!( + ::std::mem::size_of::(), + 1usize, + concat!("Size of: ", stringify!(cv_Bar)) + ); + assert_eq!( + ::std::mem::align_of::(), + 1usize, + concat!("Alignment of ", stringify!(cv_Bar)) + ); +} diff --git a/bindgen-tests/tests/expectations/tests/struct_with_anon_union_1_0.rs b/bindgen-tests/tests/expectations/tests/struct_with_anon_union_1_0.rs index fd4d561b79..8db33d113e 100644 --- a/bindgen-tests/tests/expectations/tests/struct_with_anon_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/struct_with_anon_union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/struct_with_anon_unnamed_union_1_0.rs b/bindgen-tests/tests/expectations/tests/struct_with_anon_unnamed_union_1_0.rs index 52c6904c26..55432d47fc 100644 --- a/bindgen-tests/tests/expectations/tests/struct_with_anon_unnamed_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/struct_with_anon_unnamed_union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/struct_with_nesting_1_0.rs b/bindgen-tests/tests/expectations/tests/struct_with_nesting_1_0.rs index a777adf360..af795b6423 100644 --- a/bindgen-tests/tests/expectations/tests/struct_with_nesting_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/struct_with_nesting_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/transform-op.rs b/bindgen-tests/tests/expectations/tests/transform-op.rs index eec37a3dae..84a9460362 100644 --- a/bindgen-tests/tests/expectations/tests/transform-op.rs +++ b/bindgen-tests/tests/expectations/tests/transform-op.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/typeref_1_0.rs b/bindgen-tests/tests/expectations/tests/typeref_1_0.rs index 3376bf0438..8e4143c082 100644 --- a/bindgen-tests/tests/expectations/tests/typeref_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/typeref_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union-in-ns_1_0.rs b/bindgen-tests/tests/expectations/tests/union-in-ns_1_0.rs index 63e3978199..a057cc348a 100644 --- a/bindgen-tests/tests/expectations/tests/union-in-ns_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union-in-ns_1_0.rs @@ -16,11 +16,11 @@ pub mod root { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_bitfield_1_0.rs b/bindgen-tests/tests/expectations/tests/union_bitfield_1_0.rs index f15ce0f020..2227746849 100644 --- a/bindgen-tests/tests/expectations/tests/union_bitfield_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_bitfield_1_0.rs @@ -100,11 +100,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_dtor.rs b/bindgen-tests/tests/expectations/tests/union_dtor.rs index cb363de6b5..085e5ca30a 100644 --- a/bindgen-tests/tests/expectations/tests/union_dtor.rs +++ b/bindgen-tests/tests/expectations/tests/union_dtor.rs @@ -62,6 +62,6 @@ impl Default for UnionWithDtor { impl UnionWithDtor { #[inline] pub unsafe fn destruct(&mut self) { - unsafe { UnionWithDtor_UnionWithDtor_destructor(self) } + UnionWithDtor_UnionWithDtor_destructor(self) } } diff --git a/bindgen-tests/tests/expectations/tests/union_dtor_1_0.rs b/bindgen-tests/tests/expectations/tests/union_dtor_1_0.rs index 519bd8692f..67455aa8b8 100644 --- a/bindgen-tests/tests/expectations/tests/union_dtor_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_dtor_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { @@ -98,6 +98,6 @@ extern "C" { impl UnionWithDtor { #[inline] pub unsafe fn destruct(&mut self) { - unsafe { UnionWithDtor_UnionWithDtor_destructor(self) } + UnionWithDtor_UnionWithDtor_destructor(self) } } diff --git a/bindgen-tests/tests/expectations/tests/union_fields_1_0.rs b/bindgen-tests/tests/expectations/tests/union_fields_1_0.rs index 621a961f4f..b5f94bcd05 100644 --- a/bindgen-tests/tests/expectations/tests/union_fields_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_fields_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_template_1_0.rs b/bindgen-tests/tests/expectations/tests/union_template_1_0.rs index 543f154d05..487579f7e5 100644 --- a/bindgen-tests/tests/expectations/tests/union_template_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_template_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_anon_struct_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_anon_struct_1_0.rs index 316f8453e5..8b11e75796 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_anon_struct_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_anon_struct_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield_1_0.rs index c06f6489b9..6ce6ed2282 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_anon_struct_bitfield_1_0.rs @@ -100,11 +100,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_anon_union_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_anon_union_1_0.rs index 6c856634da..f3e4b84003 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_anon_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_anon_union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_struct_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_struct_1_0.rs index d4c6fec977..1562a8c971 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_struct_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_struct_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_union_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_union_1_0.rs index 96f03680aa..197a6bbfbc 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_anon_unnamed_union_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_big_member_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_big_member_1_0.rs index 869720d442..63b43baa82 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_big_member_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_big_member_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_nesting_1_0.rs b/bindgen-tests/tests/expectations/tests/union_with_nesting_1_0.rs index 4b1ab55336..a1e452fe03 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_nesting_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_nesting_1_0.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/union_with_non_copy_member.rs b/bindgen-tests/tests/expectations/tests/union_with_non_copy_member.rs index 227afaffcc..3c500b2106 100644 --- a/bindgen-tests/tests/expectations/tests/union_with_non_copy_member.rs +++ b/bindgen-tests/tests/expectations/tests/union_with_non_copy_member.rs @@ -14,11 +14,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::std::mem::transmute(self) } + ::std::mem::transmute(self) } } impl ::std::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/use-core_1_0.rs b/bindgen-tests/tests/expectations/tests/use-core_1_0.rs index 0310211d88..8c6d0fd272 100644 --- a/bindgen-tests/tests/expectations/tests/use-core_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/use-core_1_0.rs @@ -16,11 +16,11 @@ impl __BindgenUnionField { } #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { ::core::mem::transmute(self) } + ::core::mem::transmute(self) } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { ::core::mem::transmute(self) } + ::core::mem::transmute(self) } } impl ::core::default::Default for __BindgenUnionField { diff --git a/bindgen-tests/tests/expectations/tests/var-tracing.rs b/bindgen-tests/tests/expectations/tests/var-tracing.rs index 22e7e6ed69..37fdd95105 100644 --- a/bindgen-tests/tests/expectations/tests/var-tracing.rs +++ b/bindgen-tests/tests/expectations/tests/var-tracing.rs @@ -43,11 +43,9 @@ extern "C" { impl Bar { #[inline] pub unsafe fn new(baz: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - Bar_Bar(__bindgen_tmp.as_mut_ptr(), baz); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + Bar_Bar(__bindgen_tmp.as_mut_ptr(), baz); + __bindgen_tmp.assume_init() } } #[repr(C)] diff --git a/bindgen-tests/tests/expectations/tests/wasm-constructor-returns.rs b/bindgen-tests/tests/expectations/tests/wasm-constructor-returns.rs index 909d04ded4..6d15c51f9d 100644 --- a/bindgen-tests/tests/expectations/tests/wasm-constructor-returns.rs +++ b/bindgen-tests/tests/expectations/tests/wasm-constructor-returns.rs @@ -33,10 +33,8 @@ extern "C" { impl Foo { #[inline] pub unsafe fn new(var: ::std::os::raw::c_int) -> Self { - unsafe { - let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); - Foo_Foo(__bindgen_tmp.as_mut_ptr(), var); - __bindgen_tmp.assume_init() - } + let mut __bindgen_tmp = ::std::mem::MaybeUninit::uninit(); + Foo_Foo(__bindgen_tmp.as_mut_ptr(), var); + __bindgen_tmp.assume_init() } } diff --git a/bindgen-tests/tests/expectations/tests/win32-thiscall_nightly.rs b/bindgen-tests/tests/expectations/tests/win32-thiscall_nightly.rs index f144edd4d0..92bb13cc61 100644 --- a/bindgen-tests/tests/expectations/tests/win32-thiscall_nightly.rs +++ b/bindgen-tests/tests/expectations/tests/win32-thiscall_nightly.rs @@ -39,13 +39,13 @@ extern "thiscall" { impl Foo { #[inline] pub unsafe fn test(&mut self) { - unsafe { Foo_test(self) } + Foo_test(self) } #[inline] pub unsafe fn test2( &mut self, var: ::std::os::raw::c_int, ) -> ::std::os::raw::c_int { - unsafe { Foo_test2(self, var) } + Foo_test2(self, var) } } diff --git a/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_anon_union_1_0.rs b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_anon_union_1_0.rs new file mode 100644 index 0000000000..0eef6d2e78 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_anon_union_1_0.rs @@ -0,0 +1,107 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +pub struct __BindgenUnionField(::std::marker::PhantomData); +impl __BindgenUnionField { + #[inline] + pub fn new() -> Self { + __BindgenUnionField(::std::marker::PhantomData) + } + #[inline] + pub unsafe fn as_ref(&self) -> &T { + unsafe { ::std::mem::transmute(self) } + } + #[inline] + pub unsafe fn as_mut(&mut self) -> &mut T { + unsafe { ::std::mem::transmute(self) } + } +} +impl ::std::default::Default for __BindgenUnionField { + #[inline] + fn default() -> Self { + Self::new() + } +} +impl ::std::clone::Clone for __BindgenUnionField { + #[inline] + fn clone(&self) -> Self { + Self::new() + } +} +impl ::std::marker::Copy for __BindgenUnionField {} +impl ::std::fmt::Debug for __BindgenUnionField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__BindgenUnionField") + } +} +impl ::std::hash::Hash for __BindgenUnionField { + fn hash(&self, _state: &mut H) {} +} +impl ::std::cmp::PartialEq for __BindgenUnionField { + fn eq(&self, _other: &__BindgenUnionField) -> bool { + true + } +} +impl ::std::cmp::Eq for __BindgenUnionField {} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct TErrorResult { + pub mResult: ::std::os::raw::c_int, + pub __bindgen_anon_1: TErrorResult__bindgen_ty_1, + pub mMightHaveUnreported: bool, + pub mUnionState: TErrorResult_UnionState, +} +pub const TErrorResult_UnionState_HasMessage: TErrorResult_UnionState = 0; +pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState = 0; +pub type TErrorResult_UnionState = i32; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct TErrorResult_Message { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct TErrorResult_DOMExceptionInfo { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct TErrorResult__bindgen_ty_1 { + pub mMessage: __BindgenUnionField<*mut TErrorResult_Message>, + pub mDOMExceptionInfo: + __BindgenUnionField<*mut TErrorResult_DOMExceptionInfo>, + pub bindgen_union_field: u64, +} +impl Default for TErrorResult { + fn default() -> Self { + unsafe { + let mut s: Self = ::std::mem::uninitialized(); + ::std::ptr::write_bytes(&mut s, 0, 1); + s + } + } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct ErrorResult { + pub _base: TErrorResult, +} +impl Clone for ErrorResult { + fn clone(&self) -> Self { + *self + } +} +impl Default for ErrorResult { + fn default() -> Self { + unsafe { + let mut s: Self = ::std::mem::uninitialized(); + ::std::ptr::write_bytes(&mut s, 0, 1); + s + } + } +} diff --git a/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_class.rs b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_class.rs new file mode 100644 index 0000000000..d621b450a2 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_class.rs @@ -0,0 +1,215 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField(::std::marker::PhantomData, [T; 0]); +impl __IncompleteArrayField { + #[inline] + pub const fn new() -> Self { + __IncompleteArrayField(::std::marker::PhantomData, []) + } + #[inline] + pub fn as_ptr(&self) -> *const T { + self as *const _ as *const T + } + #[inline] + pub fn as_mut_ptr(&mut self) -> *mut T { + self as *mut _ as *mut T + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + } + #[inline] + pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { + unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + } +} +impl ::std::fmt::Debug for __IncompleteArrayField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct C { + pub a: ::std::os::raw::c_int, + pub big_array: [::std::os::raw::c_char; 33usize], +} +impl Default for C { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug)] +pub struct C_with_zero_length_array { + pub a: ::std::os::raw::c_int, + pub big_array: [::std::os::raw::c_char; 33usize], + pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>, +} +impl Default for C_with_zero_length_array { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct C_with_zero_length_array_2 { + pub a: ::std::os::raw::c_int, + pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>, +} +#[repr(C)] +#[derive(Debug)] +pub struct C_with_incomplete_array { + pub a: ::std::os::raw::c_int, + pub big_array: [::std::os::raw::c_char; 33usize], + pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>, +} +impl Default for C_with_incomplete_array { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct C_with_incomplete_array_2 { + pub a: ::std::os::raw::c_int, + pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>, +} +#[repr(C)] +#[derive(Debug)] +pub struct C_with_zero_length_array_and_incomplete_array { + pub a: ::std::os::raw::c_int, + pub big_array: [::std::os::raw::c_char; 33usize], + pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>, + pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>, +} +impl Default for C_with_zero_length_array_and_incomplete_array { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct C_with_zero_length_array_and_incomplete_array_2 { + pub a: ::std::os::raw::c_int, + pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>, + pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>, +} +#[repr(C)] +#[derive(Debug, Default)] +pub struct WithDtor { + pub b: ::std::os::raw::c_int, +} +#[repr(C)] +#[derive(Debug)] +pub struct IncompleteArrayNonCopiable { + pub whatever: *mut ::std::os::raw::c_void, + pub incomplete_array: __IncompleteArrayField, +} +impl Default for IncompleteArrayNonCopiable { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub union Union { + pub d: f32, + pub i: ::std::os::raw::c_int, +} +impl Default for Union { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct WithUnion { + pub data: Union, +} +impl Default for WithUnion { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} +#[repr(C)] +#[derive(Debug, Default, Copy, Clone)] +pub struct RealAbstractionWithTonsOfMethods { + pub _address: u8, +} +extern "C" { + #[link_name = "\u{1}_ZNK32RealAbstractionWithTonsOfMethods3barEv"] + pub fn RealAbstractionWithTonsOfMethods_bar( + this: *const RealAbstractionWithTonsOfMethods, + ); +} +extern "C" { + #[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEv"] + pub fn RealAbstractionWithTonsOfMethods_bar1( + this: *mut RealAbstractionWithTonsOfMethods, + ); +} +extern "C" { + #[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEi"] + pub fn RealAbstractionWithTonsOfMethods_bar2( + this: *mut RealAbstractionWithTonsOfMethods, + foo: ::std::os::raw::c_int, + ); +} +extern "C" { + #[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3staEv"] + pub fn RealAbstractionWithTonsOfMethods_sta(); +} +impl RealAbstractionWithTonsOfMethods { + #[inline] + pub unsafe fn bar(&self) { + unsafe { RealAbstractionWithTonsOfMethods_bar(self) } + } + #[inline] + pub unsafe fn bar1(&mut self) { + unsafe { RealAbstractionWithTonsOfMethods_bar1(self) } + } + #[inline] + pub unsafe fn bar2(&mut self, foo: ::std::os::raw::c_int) { + unsafe { RealAbstractionWithTonsOfMethods_bar2(self, foo) } + } + #[inline] + pub unsafe fn sta() { + unsafe { RealAbstractionWithTonsOfMethods_sta() } + } +} diff --git a/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs new file mode 100644 index 0000000000..5864ba0a8d --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs @@ -0,0 +1,74 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +extern crate libloading; +pub struct TestLib { + __library: ::libloading::Library, + pub foo: Result< + unsafe extern "C" fn( + x: ::std::os::raw::c_int, + y: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int, + ::libloading::Error, + >, + pub bar: Result< + unsafe extern "C" fn( + x: *mut ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int, + ::libloading::Error, + >, + pub baz: Result< + unsafe extern "C" fn() -> ::std::os::raw::c_int, + ::libloading::Error, + >, +} +impl TestLib { + pub unsafe fn new

(path: P) -> Result + where + P: AsRef<::std::ffi::OsStr>, + { + let library = ::libloading::Library::new(path)?; + unsafe { Self::from_library(library) } + } + pub unsafe fn from_library( + library: L, + ) -> Result + where + L: Into<::libloading::Library>, + { + let __library = library.into(); + let foo = unsafe { __library.get(b"foo\0") }.map(|sym| *sym); + let bar = unsafe { __library.get(b"bar\0") }.map(|sym| *sym); + let baz = unsafe { __library.get(b"baz\0") }.map(|sym| *sym); + Ok(TestLib { + __library, + foo, + bar, + baz, + }) + } + pub unsafe fn foo( + &self, + x: ::std::os::raw::c_int, + y: ::std::os::raw::c_int, + ) -> ::std::os::raw::c_int { + unsafe { + (self.foo.as_ref().expect("Expected function, got error."))(x, y) + } + } + pub unsafe fn bar( + &self, + x: *mut ::std::os::raw::c_void, + ) -> ::std::os::raw::c_int { + unsafe { + (self.bar.as_ref().expect("Expected function, got error."))(x) + } + } + pub unsafe fn baz(&self) -> ::std::os::raw::c_int { + unsafe { (self.baz.as_ref().expect("Expected function, got error."))() } + } +} diff --git a/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_objc_class.rs b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_objc_class.rs new file mode 100644 index 0000000000..0fe8e254c3 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_objc_class.rs @@ -0,0 +1,38 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] +#![cfg(target_os = "macos")] + +use objc::{self, class, msg_send, sel, sel_impl}; +#[allow(non_camel_case_types)] +pub type id = *mut objc::runtime::Object; +extern "C" { + pub static mut fooVar: Foo; +} +#[repr(transparent)] +#[derive(Debug, Copy, Clone)] +pub struct Foo(pub id); +impl std::ops::Deref for Foo { + type Target = objc::runtime::Object; + fn deref(&self) -> &Self::Target { + unsafe { &*self.0 } + } +} +unsafe impl objc::Message for Foo {} +impl Foo { + pub fn alloc() -> Self { + Self(unsafe { msg_send!(class!(Foo), alloc) }) + } +} +impl IFoo for Foo {} +pub trait IFoo: Sized + std::ops::Deref { + unsafe fn method(&self) + where + ::Target: objc::Message + Sized, + { + unsafe { msg_send!(*self, method) } + } +} diff --git a/bindgen-tests/tests/expectations/tests/zero-size-array-align.rs b/bindgen-tests/tests/expectations/tests/zero-size-array-align.rs index e99abb8721..d7d0f6613d 100644 --- a/bindgen-tests/tests/expectations/tests/zero-size-array-align.rs +++ b/bindgen-tests/tests/expectations/tests/zero-size-array-align.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/expectations/tests/zero-sized-array.rs b/bindgen-tests/tests/expectations/tests/zero-sized-array.rs index 27c48f0e25..4ba52498c4 100644 --- a/bindgen-tests/tests/expectations/tests/zero-sized-array.rs +++ b/bindgen-tests/tests/expectations/tests/zero-sized-array.rs @@ -23,11 +23,11 @@ impl __IncompleteArrayField { } #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { ::std::slice::from_raw_parts(self.as_ptr(), len) } + ::std::slice::from_raw_parts(self.as_ptr(), len) } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) } } impl ::std::fmt::Debug for __IncompleteArrayField { diff --git a/bindgen-tests/tests/headers/wrap_unsafe_ops_anon_union_1_0.hpp b/bindgen-tests/tests/headers/wrap_unsafe_ops_anon_union_1_0.hpp new file mode 100644 index 0000000000..3b595f2ea7 --- /dev/null +++ b/bindgen-tests/tests/headers/wrap_unsafe_ops_anon_union_1_0.hpp @@ -0,0 +1,22 @@ +// bindgen-flags: --rust-target 1.0 --wrap-unsafe-ops --no-layout-tests + +template +struct TErrorResult { + enum UnionState { + HasMessage, + HasException, + }; + int mResult; + struct Message; + struct DOMExceptionInfo; + union { + Message* mMessage; + DOMExceptionInfo* mDOMExceptionInfo; + }; + + bool mMightHaveUnreported; + UnionState mUnionState; +}; + +struct ErrorResult : public TErrorResult { +}; diff --git a/bindgen-tests/tests/headers/wrap_unsafe_ops_class.hpp b/bindgen-tests/tests/headers/wrap_unsafe_ops_class.hpp new file mode 100644 index 0000000000..e3d11c571b --- /dev/null +++ b/bindgen-tests/tests/headers/wrap_unsafe_ops_class.hpp @@ -0,0 +1,74 @@ +// bindgen-flags: --wrap-unsafe-ops --no-layout-tests + +class C { + int a; + // More than rust limits (32) + char big_array[33]; +}; + +class C_with_zero_length_array { + int a; + // More than rust limits (32) + char big_array[33]; + char zero_length_array[0]; +}; + +class C_with_zero_length_array_2 { + int a; + char zero_length_array[0]; +}; + +class C_with_incomplete_array { + int a; + // More than rust limits (32) + char big_array[33]; + char incomplete_array[]; +}; + +class C_with_incomplete_array_2 { + int a; + char incomplete_array[]; +}; + +class C_with_zero_length_array_and_incomplete_array { + int a; + // More than rust limits (32) + char big_array[33]; + char zero_length_array[0]; + char incomplete_array[]; +}; + +class C_with_zero_length_array_and_incomplete_array_2 { + int a; + char zero_length_array[0]; + char incomplete_array[]; +}; + +class WithDtor { + int b; + + ~WithDtor() {} +}; + +class IncompleteArrayNonCopiable { + void* whatever; + C incomplete_array[]; +}; + +union Union { + float d; + int i; +}; + +class WithUnion { + Union data; +}; + +class RealAbstractionWithTonsOfMethods { + void foo(); +public: + void bar() const; + void bar(); + void bar(int foo); + static void sta(); +}; diff --git a/bindgen-tests/tests/headers/wrap_unsafe_ops_dynamic_loading_simple.h b/bindgen-tests/tests/headers/wrap_unsafe_ops_dynamic_loading_simple.h new file mode 100644 index 0000000000..2b8c107185 --- /dev/null +++ b/bindgen-tests/tests/headers/wrap_unsafe_ops_dynamic_loading_simple.h @@ -0,0 +1,5 @@ +// bindgen-flags: --dynamic-loading TestLib --wrap-unsafe-ops + +int foo(int x, int y); +int bar(void *x); +int baz(); diff --git a/bindgen-tests/tests/headers/wrap_unsafe_ops_objc_class.h b/bindgen-tests/tests/headers/wrap_unsafe_ops_objc_class.h new file mode 100644 index 0000000000..017795c85d --- /dev/null +++ b/bindgen-tests/tests/headers/wrap_unsafe_ops_objc_class.h @@ -0,0 +1,10 @@ +// bindgen-flags: --wrap-unsafe-ops -- -x objective-c +// bindgen-osx-only + +@class Foo; + +Foo* fooVar; + +@interface Foo +-(void)method; +@end diff --git a/bindgen/codegen/dyngen.rs b/bindgen/codegen/dyngen.rs index f310f14e08..5e734ccd15 100644 --- a/bindgen/codegen/dyngen.rs +++ b/bindgen/codegen/dyngen.rs @@ -1,4 +1,5 @@ use crate::codegen; +use crate::ir::context::BindgenContext; use crate::ir::function::ClangAbi; use proc_macro2::Ident; @@ -72,12 +73,22 @@ impl DynamicItems { Self::default() } - pub fn get_tokens(&self, lib_ident: Ident) -> proc_macro2::TokenStream { + pub fn get_tokens( + &self, + lib_ident: Ident, + ctx: &BindgenContext, + ) -> proc_macro2::TokenStream { let struct_members = &self.struct_members; let constructor_inits = &self.constructor_inits; let init_fields = &self.init_fields; let struct_implementation = &self.struct_implementation; + let from_library = if ctx.options().wrap_unsafe_ops { + quote!(unsafe { Self::from_library(library) }) + } else { + quote!(Self::from_library(library)) + }; + quote! { extern crate libloading; @@ -92,9 +103,7 @@ impl DynamicItems { ) -> Result where P: AsRef<::std::ffi::OsStr> { let library = ::libloading::Library::new(path)?; - unsafe { - Self::from_library(library) - } + #from_library } pub unsafe fn from_library( @@ -126,6 +135,7 @@ impl DynamicItems { ret: proc_macro2::TokenStream, ret_ty: proc_macro2::TokenStream, attributes: Vec, + ctx: &BindgenContext, ) { if !is_variadic { assert_eq!(args.len(), args_identifiers.len()); @@ -149,8 +159,10 @@ impl DynamicItems { } else { quote! { self.#ident.as_ref().expect("Expected function, got error.") } }; - let call_body = quote! { - (#fn_)(#( #args_identifiers ),*) + let call_body = if ctx.options().wrap_unsafe_ops { + quote!(unsafe { (#fn_)(#( #args_identifiers ),*) }) + } else { + quote!((#fn_)(#( #args_identifiers ),*) ) }; // We can't implement variadic functions from C easily, so we allow to @@ -159,22 +171,26 @@ impl DynamicItems { self.struct_implementation.push(quote! { #(#attributes)* pub unsafe fn #ident ( &self, #( #args ),* ) -> #ret_ty { - unsafe { - #call_body - } + #call_body } }); } // N.B: Unwrap the signature upon construction if it is required to be resolved. let ident_str = codegen::helpers::ast_ty::cstr_expr(ident.to_string()); + let library_get = if ctx.options().wrap_unsafe_ops { + quote!(unsafe { __library.get(#ident_str) }) + } else { + quote!(__library.get(#ident_str)) + }; + self.constructor_inits.push(if is_required { quote! { - let #ident = unsafe { __library.get(#ident_str) }.map(|sym| *sym)?; + let #ident = #library_get.map(|sym| *sym)?; } } else { quote! { - let #ident = unsafe { __library.get(#ident_str) }.map(|sym| *sym); + let #ident = #library_get.map(|sym| *sym); } }); diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index c1e92b77d5..e537242710 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -2585,9 +2585,7 @@ impl MethodCodegen for Method { }) } - let block = quote! { - #( #stmts );* - }; + let block = ctx.wrap_unsafe_ops(quote! ( #( #stmts );*)); let mut attrs = vec![attributes::inline()]; @@ -2601,9 +2599,7 @@ impl MethodCodegen for Method { methods.push(quote! { #(#attrs)* pub unsafe fn #name ( #( #args ),* ) #ret { - unsafe { - #block - } + #block } }); } @@ -4169,6 +4165,7 @@ impl CodeGenerator for Function { ret, ret_ty, attributes, + ctx, ); } else { result.push(tokens); @@ -4212,17 +4209,18 @@ fn objc_method_codegen( let methods_and_args = method.format_method_call(&fn_args); - let body = if method.is_class_method() { - let class_name = ctx.rust_ident( - class_name.expect("Generating a class method without class name?"), - ); - quote! { - msg_send!(class!(#class_name), #methods_and_args) - } - } else { - quote! { - msg_send!(*self, #methods_and_args) - } + let body = { + let body = if method.is_class_method() { + let class_name = ctx.rust_ident( + class_name + .expect("Generating a class method without class name?"), + ); + quote!(msg_send!(class!(#class_name), #methods_and_args)) + } else { + quote!(msg_send!(*self, #methods_and_args)) + }; + + ctx.wrap_unsafe_ops(body) }; let method_name = @@ -4230,9 +4228,7 @@ fn objc_method_codegen( methods.push(quote! { unsafe fn #method_name #sig where ::Target: objc::Message + Sized { - unsafe { - #body - } + #body } }); } @@ -4495,7 +4491,7 @@ pub(crate) fn codegen( if let Some(ref lib_name) = context.options().dynamic_library_name { let lib_ident = context.rust_ident(lib_name); let dynamic_items_tokens = - result.dynamic_items().get_tokens(lib_ident); + result.dynamic_items().get_tokens(lib_ident, context); result.push(dynamic_items_tokens); } @@ -4598,6 +4594,9 @@ pub mod utils { pub struct __BindgenUnionField(::#prefix::marker::PhantomData); }; + let transmute = + ctx.wrap_unsafe_ops(quote!(::#prefix::mem::transmute(self))); + let union_field_impl = quote! { impl __BindgenUnionField { #[inline] @@ -4607,16 +4606,12 @@ pub mod utils { #[inline] pub unsafe fn as_ref(&self) -> &T { - unsafe { - ::#prefix::mem::transmute(self) - } + #transmute } #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { - unsafe { - ::#prefix::mem::transmute(self) - } + #transmute } } }; @@ -4711,6 +4706,13 @@ pub mod utils { ::#prefix::marker::PhantomData, [T; 0]); }; + let from_raw_parts = ctx.wrap_unsafe_ops(quote! ( + ::#prefix::slice::from_raw_parts(self.as_ptr(), len) + )); + let from_raw_parts_mut = ctx.wrap_unsafe_ops(quote! ( + ::#prefix::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + )); + let incomplete_array_impl = quote! { impl __IncompleteArrayField { #[inline] @@ -4730,16 +4732,12 @@ pub mod utils { #[inline] pub unsafe fn as_slice(&self, len: usize) -> &[T] { - unsafe { - ::#prefix::slice::from_raw_parts(self.as_ptr(), len) - } + #from_raw_parts } #[inline] pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { - unsafe { - ::#prefix::slice::from_raw_parts_mut(self.as_mut_ptr(), len) - } + #from_raw_parts_mut } } }; diff --git a/bindgen/ir/context.rs b/bindgen/ir/context.rs index 928f24067b..4623b25344 100644 --- a/bindgen/ir/context.rs +++ b/bindgen/ir/context.rs @@ -25,7 +25,8 @@ use crate::BindgenOptions; use crate::{Entry, HashMap, HashSet}; use cexpr; use clang_sys; -use proc_macro2::{Ident, Span}; +use proc_macro2::{Ident, Span, TokenStream}; +use quote::ToTokens; use std::borrow::Cow; use std::cell::{Cell, RefCell}; use std::collections::{BTreeSet, HashMap as StdHashMap}; @@ -2702,6 +2703,14 @@ If you encounter an error missing from this list, please file an issue or a PR!" let name = item.path_for_allowlisting(self)[1..].join("::"); self.options().must_use_types.matches(name) } + + pub(crate) fn wrap_unsafe_ops(&self, tokens: impl ToTokens) -> TokenStream { + if self.options.wrap_unsafe_ops { + quote!(unsafe { #tokens }) + } else { + tokens.into_token_stream() + } + } } /// A builder struct for configuring item resolution options. diff --git a/bindgen/lib.rs b/bindgen/lib.rs index d281e94967..53bbd8874c 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -645,6 +645,10 @@ impl Builder { output_vector.push("--merge-extern-blocks".into()); } + if self.options.wrap_unsafe_ops { + output_vector.push("--wrap-unsafe-ops".into()); + } + // Add clang arguments output_vector.push("--".into()); @@ -1770,6 +1774,12 @@ impl Builder { .insert(arg.into()); self } + + /// If true, wraps unsafe operations in unsafe blocks. + pub fn wrap_unsafe_ops(mut self, doit: bool) -> Self { + self.options.wrap_unsafe_ops = doit; + self + } } /// Configuration options for generated bindings. @@ -2107,6 +2117,9 @@ struct BindgenOptions { merge_extern_blocks: bool, abi_overrides: HashMap, + + /// Whether to wrap unsafe operations in unsafe blocks or not. + wrap_unsafe_ops: bool, } impl BindgenOptions { @@ -2298,6 +2311,7 @@ impl Default for BindgenOptions { sort_semantically, merge_extern_blocks, abi_overrides, + wrap_unsafe_ops, } } }