rust-lang / rust-lang/rust-clippy

Lint idea: forbid all expect and unwrap use

Open
#6,636 13 comments 40 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

It would be nice to have a restriction lint (i.e., totally opt-in) that allows clippy users to completely ban all expect and unwrap use from a codebase. Functions or other items that absolutely need expect and/or unwrap or that have been manually reviewed by a human could be marked explicitly with an attribute.

It might also make sense to check for panic!.

Categories (optional)

This would probably be a restriction lint, since it's not desirable for all codebases.

What is the advantage of the recommended code over the original code

expect and unwrap are a normal part of safe Rust programming, but are often an unintentional escape hatch for poor data validation or error handling. Their use doesn't represent a security vulnerability per se, but can result in denials-of-service and are also generally less readable than the equivalent error-handling approach. Programmers in high-assurance codebases might want to ensure that none of their routines (or their dependencies' routines) perform unwraps, which this lint would allow.

Drawbacks

None. As a restriction lint, users would have to explicitly opt into it.

Example
fn whatever() -> T {
  let thing = something().unwrap();
  thing.as_t()
}

would result in an linting error indicating that the user has requested unwrap be forbidden, and encouraging the user to add an attribute (something like #[allow(clippy::reviewed_unwrap)].

I'm capable of implementing this lint, if there's interest!

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 reviewing the proposed restriction lint scope for expect, unwrap, and possibly panic!, along with the suggested opt-in and reviewed-item attributes. Done means an agreed lint design that reports forbidden uses, supports explicit exceptions, and has its behavior and scope validated for Clippy users.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.