rust-lang / rust-lang/rust-clippy

`get_or_insert_with` followed by unwrap can be replaced by `unwrap_or_else`

Open
#10,646 1 comment 0 reactions 1 assignee View on GitHub

@sjwang05 is already working on this.

Since Sep 28, 2023.

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

Description

What it does

get_or_insert_with followed by unwrap function can be replaced by unwrap_or_else.

unwrap function:

  • unwrap
  • except
  • unwrap_unchecked
Lint Name

manual_option_folding

Category

style

Advantage
  • Remove unsafe block for unwrap_unchecked.
  • More idiomatic.
Drawbacks

No response

Example
// foo: impl Fn() -> X
// def: impl Fn() -> X
let mut opt: Option<X> = foo();
opt.get_or_insert_with(|| def());
let res: X = unsafe { opt.unwrap_unchecked() };

Could be written as:

let res: X = foo().unwrap_or_else(|| def());

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.