1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-03 05:27:04 +02:00

Add safety comments and use consistent format for existing ones. (#1981)

We should have safety comments on all `unsafe` blocks, to set a good
example.
This commit is contained in:
Andrew Walbran
2024-04-12 18:19:19 +01:00
committed by GitHub
parent 8433ad9a3d
commit b808887006
18 changed files with 65 additions and 53 deletions

View File

@ -12,6 +12,7 @@ extern "C" {
fn main() {
let x = -42;
// SAFETY: `abs` doesn't have any safety requirements.
let abs_x = unsafe { abs(x) };
println!("{x}, {abs_x}");
}

View File

@ -20,7 +20,10 @@ use birthday_bindgen::{card, print_card};
fn main() {
let name = std::ffi::CString::new("Peter").unwrap();
let card = card { name: name.as_ptr(), years: 42 };
// SAFETY: `print_card` is safe to call with a valid `card` pointer.
// SAFETY: The pointer we pass is valid because it came from a Rust
// reference, and the `name` it contains refers to `name` above which also
// remains valid. `print_card` doesn't store either pointer to use later
// after it returns.
unsafe {
print_card(&card as *const card);
}

View File

@ -9,6 +9,7 @@ extern "C" {
fn main() {
let x = -42;
// SAFETY: `abs` doesn't have any safety requirements.
let abs_x = unsafe { abs(x) };
println!("{x}, {abs_x}");
}