rust-lang / rust-lang/rust-clippy

Using `unwrap_or_default()` in place of `unwrap_or(<default>)`

Open
#9,435 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

Currently this is used:

num.unwrap_or(0);

This is ok for non-default values, but I propose we suggest unwrap_or_default instead for default values of primitive types, which all happen to be 0, and false for bools.

So instead we'd write:

num.unwrap_or_default();

Our syntax is longer, but it'd hard to come up with a case where default value has more characters than keyword "default" itself, which is 7 chars long. If we have an unwrap_or_default method, it makes sense to use it.

Comments please.

Version

No response

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.

Research direction

Review the proposed unwrap_or_default() suggestion and its examples in the issue first. Define which primitive default values and code patterns the Clippy lint should cover, then verify that the completed lint produces the proposed replacement without changing non-default cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.