rust-lang / rust-lang/rust-clippy

Feature request: `allow-unwrap-in-tests` should allow `unwrap_err`

Open
#14,145 4 comments 0 reactions 1 assignee View on GitHub

@alex-semenyuk is already working on this.

Since Feb 5, 2025.

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

Description

Summary

For best practices, I disallow unwrap to enforce using Result<>.
But in tests, it is necessary to check errors.

given

unwrap_used = "deny"
allow-unwrap-in-tests = "allow"

results in

used `unwrap_err()` on a `Result` value
if this value is an `Ok`, it will panic
consider using `expect_err()` to provide a better panic message
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used

but expected to be allowed.

Alternative code that doesn't work:

result.err().unwrap().to_string()

# =>  consider using `expect()` to provide a better panic message

result.err().expect("").to_string()

# => called `.err().expect()` on a `Result` value
Lint Name

No response

Reproducer

I tried this code:

<code>

I saw this happen:

<output>

I expected to see this happen:

Version

Additional Labels

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.