Conversation

nik-rev

This PR introduces a complexity lint concealed_obvious_default which checks usages of Option::<T>::unwrap_or_default() on a type with an obvious default and suggests using Option::<T>::unwrap_or(<default>) instead. It checks similar methods on Result and Entry

for example, it suggests to replace x.unwrap_or_default() with x.unwrap_or(0) where x: Option<u8>.

Closes #14779

changelog: add [concealed_obvious_default] lint

@rustbot

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbotrustbot added the S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested partieslabel Jun 12, 2025
@nik-revnik-rev changed the title feat: Implement concealed_obvious_default lint New lint: concealed_obvious_default Jun 12, 2025
@nik-revnik-rev marked this pull request as draft June 12, 2025 15:45
@nik-revnik-rev marked this pull request as ready for review June 12, 2025 16:03

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is interesting. I've added some comments.

Also, could you split this into two commits: one with the lint applied (auto-fixed) to Clippy sources, then one with the lint itself. The order between both commits is not mandatory, but auto-fixing Clippy sources first allows the repository to pass the dogfood at every step, in case we need to bisect it.

Also, should the lint move to a allow-by-default category once we have discussed about it, removing the application commit would be easy.

@rustbotrustbot added S-waiting-on-authorStatus: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)and removed S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested partieslabels Jun 12, 2025
@nik-revnik-rev force-pushed the or-default branch 2 times, most recently from 14d8112 to 4f7953e Compare June 13, 2025 15:06
@nik-revnik-rev marked this pull request as draft June 13, 2025 15:10
@nik-revnik-rev force-pushed the or-default branch 2 times, most recently from f2f906f to bb6c0d4 Compare June 13, 2025 15:19
@nik-revnik-rev marked this pull request as ready for review June 13, 2025 15:26
@nik-rev

Thanks, this is interesting. I've added some comments.

Also, could you split this into two commits: one with the lint applied (auto-fixed) to Clippy sources, then one with the lint itself. The order between both commits is not mandatory, but auto-fixing Clippy sources first allows the repository to pass the dogfood at every step, in case we need to bisect it.

Also, should the lint move to a allow-by-default category once we have discussed about it, removing the application commit would be easy.

Thanks for the review. I've addressed your comments and added tests to check for macro expansion both on the receiver and the method

@nik-revnik-rev requested a review from samueltardieu June 13, 2025 15:32
@rustbotrustbot added S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested partiesand removed S-waiting-on-authorStatus: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)labels Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've started a FCP thread on Zulip to discuss about the lint inclusion in Clippy.

Sign up for free to join this conversation on . Already have an account? Sign in to comment
S-waiting-on-reviewStatus: Awaiting review from the assignee but also interested parties
None yet

Successfully merging this pull request may close these issues.

New lint: Prefer .unwrap_or(0) over .unwrap_or_default()