rust-lang / rust-lang/rust-clippy

New lint: map then unwrap

Open
#7,763 6 comments 3 reactions 1 assignee View on GitHub

@king-11 is already working on this.

Since Nov 21, 2021.

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

Description

What it does

Detects usage of map(..).unwrap()

Categories (optional)

Kind: pedantic

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

The unwrap effectively only applies to the value before .map(..), so putting it after .map(..) needlessly complicates the semantics of the code.

Drawbacks

None.

Example
opt.map(|n| n * 2).unwrap()

Could be written as:

opt.unwrap() * 2

This should also work with expect as well as maybe other Option and Result transformers like filter.

I think the name map_unwrap is good, even though other method combinations are applicable.

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.