Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsafe_op_in_unsafe_fn warning in Rust edition 2024 #3147

Open
jmjoy opened this issue Feb 22, 2025 · 3 comments
Open

unsafe_op_in_unsafe_fn warning in Rust edition 2024 #3147

jmjoy opened this issue Feb 22, 2025 · 3 comments

Comments

@jmjoy
Copy link

jmjoy commented Feb 22, 2025

The unsafe_op_in_unsafe_fn lint now warns by default. This warning detects calls to unsafe operations in unsafe functions without an explicit unsafe block.

warning[E0133]: dereference of raw pointer is unsafe and requires unsafe block
  --> /origin/home/jmjoy/workspace/rust/phper/target/debug/build/phper-sys-ef5a7a32f49c54c5/out/php_bindings.rs:39:20
   |
39 |         let byte = *(core::ptr::addr_of!((*this).storage) as *const u8).offset(byte_index as isize);
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html>
   = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
  --> /origin/home/jmjoy/workspace/rust/phper/target/debug/build/phper-sys-ef5a7a32f49c54c5/out/php_bindings.rs:36:5
   |
36 |     pub unsafe fn raw_get_bit(this: *const Self, index: usize) -> bool {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: `#[warn(unsafe_op_in_unsafe_fn)]` on by default
@jinghao-jia
Copy link

jinghao-jia commented Feb 23, 2025

Unsafe bitfield raw getters and setters (line 39 in your case) are addressed in #3124. For other cases, bindgen's --wrap-unsafe-ops option should do the work.

@OmriSteiner
Copy link

Maybe it makes sense to enable wrap_unsafe_ops by default?
Looks like it was first implemented by #2266 but then it broke building for Rust <1.65, so it was re-implemented as an optional flag.

@jinghao-jia
Copy link

Maybe it makes sense to enable wrap_unsafe_ops by default? Looks like it was first implemented by #2266 but then it broke building for Rust <1.65, so it was re-implemented as an optional flag.

Yeah it makes sense to at least enable wrap_unsafe_ops when the target edition is set to 2024.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants