Macros wrapping expressions in unsafe blocks when using unsafe_op_in_unsafe_fn · Issue #7323 · rust-lang/rust-clippy (original) (raw)

What it does

For projects using unsafe_op_in_unsafe_fn (and specially those requiring // SAFETY comments), it would be nice to detect cases where a macro is wrapping an expression passed as a parameter in a "hidden" unsafe block, and thus bypassing the intention of unsafe_op_in_unsafe_fn.

The logic could be something like:

Callers of the macro would need to wrap the entire macro call in an unsafe block to avoid hitting the lint (plus perhaps playing with #[allow(unused_unsafe)] inside the macro to avoid the unneeded nested unsafe warning from rustc).

Categories (optional)

Drawbacks

None.

Example

Assume #![deny(unsafe_op_in_unsafe_fn)], and that container_of! wraps the first parameter in an unsafe block. Then:

let reg = container_of!((*file).private_data, Self, mdev);

Could be written as:

let reg = unsafe { container_of!((*file).private_data, Self, mdev) };