New Lint: Unsafe block in safe functions without SAFETY
comment · Issue #7238 · rust-lang/rust-clippy (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Appearance settings
Description
What it does
Detects unsafe blocks used in safe functions missing a comment explaining/justifying the use of unsafe
.
Categories (optional)
- Kind:
clippy::pedantic
What is the advantage of the recommended code over the original code
- Explains why the use of
unsafe
in the function is okay - Makes it easier to maintain
Drawbacks
None.
Example
fn get(self, slice: &[T]) -> Option { if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None } }
Could be written as:
fn get(self, slice: &[T]) -> Option {
// SAFETY: self
is checked to be in bounds.
if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
}