Skip to content

get_or_insert_with followed by unwrap can be replaced by unwrap_or_else #10646

Open
@KisaragiEffective

Description

@KisaragiEffective

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());

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions