You've already forked comprehensive-rust
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:
@ -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}");
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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}");
|
||||
}
|
||||
|
Reference in New Issue
Block a user