Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest use of unwrap_or_default #7250

Closed
ndmitchell opened this issue May 20, 2021 · 7 comments
Closed

Suggest use of unwrap_or_default #7250

ndmitchell opened this issue May 20, 2021 · 7 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@ndmitchell
Copy link

What it does

Suggest using Option::unwrap_or_default when Option::unwrap_or_else is called using a function that creates the default.

Categories (optional)

  • Kind: style

Makes the code shorter and more idiomatic.

Drawbacks

None.

Example

let x : Option<_> = ...
x.unwrap_or_else(Vec::new)

Could be written as:

let x : Option<_> = ...
x.unwrap_or_default()

Requires an understanding of what closures produce default values - e.g. Vec::default, Default::default, Vec::new, || Vec::new().

@ndmitchell ndmitchell added the A-lint Area: New lints label May 20, 2021
@giraffate giraffate added the good-first-issue These issues are a good way to get started with Clippy label May 25, 2021
@pmnoxx
Copy link
Contributor

pmnoxx commented Dec 23, 2021

+1, I see a lot of code in our code base that's like
tmp.get(arg).cloned().unwrap_or(0u64)
or
dict.get(arg).unwrap_or(HashMap::new())

@giraffate
Copy link
Contributor

Oh, we've already have the unwrap_or_else_default lint.

@giraffate giraffate added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints labels Dec 24, 2021
@pmnoxx
Copy link
Contributor

pmnoxx commented Dec 24, 2021

Oh, we've already have the unwrap_or_else_default lint.

Yes, I saw that. Though it only works when used with <TYPE>::default.
For example:
unwrap_or(u64::default) -> works
unwrap_or(0u64) -> doesn't.

@pmnoxx
Copy link
Contributor

pmnoxx commented Dec 24, 2021

@giraffate Maybe, there should be another int, let's call it

name: use_default.
Example:

func(0u64)

Warnings message:
0u64 could be replaced with u64::default

This way, we would convert values to default, and then unwrap_or_else_default would do the rest of the work.

@giraffate
Copy link
Contributor

Yes, I saw that. Though it only works when used with ::default.
For example:
unwrap_or(u64::default) -> works
unwrap_or(0u64) -> doesn't.

IMO or_fun_call should cover these cases.

This way, we would convert values to default, and then unwrap_or_else_default would do the rest of the work.

I don't want to make Clippy run multiple times. I want it to suggest the desired code once.

@pmnoxx
Copy link
Contributor

pmnoxx commented Dec 24, 2021

Yes, I saw that. Though it only works when used with ::default.
For example:
unwrap_or(u64::default) -> works
unwrap_or(0u64) -> doesn't.

IMO or_fun_call should cover these cases.

This way, we would convert values to default, and then unwrap_or_else_default would do the rest of the work.

I don't want to make Clippy run multiple times. I want it to suggest the desired code once.

Yes, I saw that. Though it only works when used with ::default.
For example:
unwrap_or(u64::default) -> works
unwrap_or(0u64) -> doesn't.

IMO or_fun_call should cover these cases.

This way, we would convert values to default, and then unwrap_or_else_default would do the rest of the work.

I don't want to make Clippy run multiple times. I want it to suggest the desired code once.

I'm wondering whenever something like this would work: #8163

@giraffate
Copy link
Contributor

#8163 fixes this, so I'm closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

No branches or pull requests

3 participants