Skip to content

New lint to minimize the unsafe block scope. #8022

Open
@xFrednet

Description

@xFrednet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsS-needs-discussionStatus: Needs further discussion before merging or work can be started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions