rust-lang / rust-lang/rust-clippy

lint suggestion: `manual_unwrap_unchecked`

Open
#16,601 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

Essentially: search for cases where hint::unreachable_unchecked is used when Option::unwrap_unchecked or Result::unwrap_unchecked could be preferred.

Advantage

No response

Drawbacks

No response

Example

A bunch from a hashbrown PR: https://github.com/rust-lang/hashbrown/pull/693/changes

But to inline a here, the main cases are:


if some_result.is_err() {
    unsafe { unreachable_unchecked() }
}
if some_option.is_none() {
    unsafe { unreachable_unchecked() }
}

becomes

unsafe { some_result.unwrap_unchecked() };
unsafe { some_option.unwrap_unchecked() };

let value1 = match some_result {
    Ok(ok) => ok,
    Err(_) => unsafe { unwrap_unchecked() }
};
let value2 = match some_option {
    Some(some) => some,
    None => unsafe { unwrap_unchecked() }
};

becomes

let value1 = unsafe { some_result.unwrap_unchecked() };
let value2 = unsafe { some_option.unwrap_unchecked() };

let Ok(value1) = some_result else {
    unsafe { unwrap_unchecked() }
};
let Some(value2) = some_option else {
    unsafe { unwrap_unchecked() }
};

becomes:

let value1 = unsafe { some_result.unwrap_unchecked() };
let value2 = unsafe { some_option.unwrap_unchecked() };
Comparison with existing lints

Clippy has lots of manual_(method) lints, so, this fits right in with those.

Additional Context

No response

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

Start by comparing the three Rust examples in the issue with Clippy’s existing manual_* lints. Trace how those lints are implemented and tested, then determine how the proposed lint should recognize the Result and Option cases. Done means the lint consistently suggests unwrap_unchecked in the described patterns without suggesting unsafe transformations outside their valid cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.