Summary
Using &raw mut <mutable global static> to create a raw pointer reference but not read from or write to it, the operation is entirely safe:
static mut GLOBAL_INT: u32 = 5;
fn main() {
let _ = &raw mut GLOBAL_INT;
}
however, a raw pointer reference created to pass to an unsafe function, multiple_unsafe_ops_per_block complains
Lint Name
multiple_unsafe_ops_per_block
Reproducer
I tried this code:
#![warn(clippy::multiple_unsafe_ops_per_block)]
static mut GLOBAL_INT: u32 = 5;
fn set_global_int(value: u32) {
unsafe {
std::ptr::copy_nonoverlapping(
&raw const value,
&raw mut GLOBAL_INT,
1
)
};
}
fn main() {
set_global_int(6);
}
I saw this happen:
Checking playground v0.0.1 (/playground)
warning: this `unsafe` block contains 2 unsafe operations, expected only one
--> src/main.rs:6:5
|
6 | / unsafe {
7 | | std::ptr::copy_nonoverlapping(
8 | | &raw const value,
9 | | &raw mut GLOBAL_INT,
... |
12 | | };
| |_____^
|
note: access of a mutable static occurs here
--> src/main.rs:9:22
|
9 | &raw mut GLOBAL_INT,
| ^^^^^^^^^^
note: unsafe function call occurs here
--> src/main.rs:7:9
|
7 | / std::ptr::copy_nonoverlapping(
8 | | &raw const value,
9 | | &raw mut GLOBAL_INT,
10 | | 1
11 | | )
| |_________^
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.97.0/index.html#multiple_unsafe_ops_per_block
note: the lint level is defined here
--> src/main.rs:1:9
|
1 | #![warn(clippy::multiple_unsafe_ops_per_block)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: `playground` (bin "playground") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.68s
I expected to see this happen:
Lint not triggered, the note: access of a mutable static occurs here note is for something that is entirely safe
Version
Clippy 0.1.97 (2026-07-14 8bab26f4f6)
Playground 1.97.1
Additional Labels
No response
Summary
Using
&raw mut <mutable global static>to create a raw pointer reference but not read from or write to it, the operation is entirely safe:however, a raw pointer reference created to pass to an unsafe function,
multiple_unsafe_ops_per_blockcomplainsLint Name
multiple_unsafe_ops_per_block
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
Lint not triggered, the
note: access of a mutable static occurs herenote is for something that is entirely safeVersion
Additional Labels
No response