Skip to content

Commit

Permalink
Merge pull request #1415 from anforowicz/unsafe-warning-from-generate…
Browse files Browse the repository at this point in the history
…d-code

Suppress `unused_unsafe` warnings within the generated code.
  • Loading branch information
dtolnay authored Dec 17, 2024
2 parents 18689a1 + 86e8c0c commit 043817c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,11 +1231,14 @@ fn expand_rust_function_shim_super(
};

let mut body = quote_spanned!(span=> #call(#(#vars,)*));
let mut allow_unused_unsafe = quote!();
if unsafety.is_some() {
body = quote_spanned!(span=> unsafe { #body });
body = quote_spanned!(span=>unsafe { #body });
allow_unused_unsafe = quote_spanned!(span=> #[allow(unused_unsafe)]);
}

quote_spanned! {span=>
#allow_unused_unsafe
#unsafety fn #local_name #generics(#(#all_args,)*) #ret {
#body
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![allow(unknown_lints)]
#![warn(rust_2024_compatibility)]
#![forbid(unsafe_op_in_unsafe_fn)]
#![deny(warnings)] // Check that expansion of `cxx::bridge` doesn't trigger warnings.

pub mod cast;
pub mod module;
Expand Down Expand Up @@ -272,6 +273,7 @@ pub mod ffi {
fn r_return_ref(shared: &Shared) -> &usize;
fn r_return_mut(shared: &mut Shared) -> &mut usize;
fn r_return_str(shared: &Shared) -> &str;
unsafe fn r_return_str_via_out_param<'a>(shared: &'a Shared, out_param: &mut &'a str);
fn r_return_sliceu8(shared: &Shared) -> &[u8];
fn r_return_mutsliceu8(slice: &mut [u8]) -> &mut [u8];
fn r_return_rust_string() -> String;
Expand Down Expand Up @@ -491,6 +493,11 @@ fn r_return_str(shared: &ffi::Shared) -> &str {
"2020"
}

fn r_return_str_via_out_param<'a>(shared: &'a ffi::Shared, out_param: &mut &'a str) {
let _ = shared;
*out_param = "2020"
}

fn r_return_sliceu8(shared: &ffi::Shared) -> &[u8] {
let _ = shared;
b"2020"
Expand Down
2 changes: 2 additions & 0 deletions tests/ffi/module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(warnings)] // Check that expansion of `cxx::bridge` doesn't trigger warnings.

#[cxx::bridge(namespace = "tests")]
pub mod ffi {
struct Job {
Expand Down
6 changes: 6 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,12 @@ extern "C" const char *cxx_run_test() noexcept {
cstring.reserve(5);
ASSERT(cstring.capacity() >= 5);

{
rust::Str out_param;
r_return_str_via_out_param(Shared{2020}, out_param);
ASSERT(out_param == "2020");
}

rust::Str cstr = "test";
rust::Str other_cstr = "foo";
swap(cstr, other_cstr);
Expand Down

0 comments on commit 043817c

Please sign in to comment.