Open
Description
What it does
Checks for unsafe
blocks that contain safe code, which can be extracted to the outside safe code.
Categories (optional)
- Kind: pedantic
Rust doesn't enforce memory safety guarantees in unsafe
blocks. Having more code inside the unsafe block than necessary, might result in bugs which could be avoided by having the code in the save-code-scope. It also clearly indicates which part of an operation is unsafe.
Drawbacks
Using the smallest unsafe code block possible might be less readable.
Example
unsafe {
let mut vec: Vec<u8> = Vec::with_capacity(128);
vec.set_len(100);
println!("{:#?}", vec);
}
Could be written as:
let mut vec: Vec<u8> = Vec::with_capacity(128);
unsafe {
vec.set_len(100);
}
println!("{:#?}", vec);