Skip to content

Lint idea: forbid all expect and unwrap use #6636

@woodruffw

Description

@woodruffw

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-restrictionLint: Belongs in the restriction lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions