rust-lang / rust-lang/rust-clippy

New lint to minimize the unsafe block scope.

Open
#8,022 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint S-needs-discussion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files, tests, or entry points are named. Start from the unsafe-block examples and define which safe statements may move outside the block, including the readability drawback. Done means the proposed lint consistently identifies unnecessary safe code inside unsafe blocks without flagging the intentionally grouped case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.