rust-lang / rust-lang/rust-clippy
`get_or_insert_with` followed by unwrap can be replaced by `unwrap_or_else`
Open
@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:
unwrapexceptunwrap_unchecked
Lint Name
manual_option_folding
Category
style
Advantage
- Remove
unsafeblock forunwrap_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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.