rust-lang / rust-lang/rust-clippy
Using `unwrap_or_default()` in place of `unwrap_or(<default>)`
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
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.
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