Skip to content

New Lint: Unsafe block in safe functions without SAFETY comment  #7238

Closed
@fee1-dead

Description

@fee1-dead

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<T> {
  if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
}

Could be written as:

fn get(self, slice: &[T]) -> Option<T> {
  // SAFETY: `self` is checked to be in bounds.
  if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions