Skip to content

Allow Default::default() pattern #1991

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

Merged
merged 1 commit into from
Oct 31, 2024
Merged

Allow Default::default() pattern #1991

merged 1 commit into from
Oct 31, 2024

Conversation

sezna
Copy link
Contributor

@sezna sezna commented Oct 31, 2024

One pattern I use a lot in Rust is Default::default() to construct a default instance of a type. However, pedantic clippy settings state that this is unclear. I personally dislike this setting, and actually find Default::default() more usable and consistent.

Consistency wise, there's no reason (IMO) for clippy to single out the Default trait. We access other trait methods in this way, and clippy has no such rule against other traits. For example, Into::into(), or Display::to_string().

Style wise -- the point of using a language with type inference is that you only have to specify the type once, and then subsequent lines of code do not need to redeclare (or "stutter") the type. I will frequently update an underlying data structure, e.g. switching from HashMap to BTreeMap, or Vec to HashSet. If I've used Default::default() everywhere, I do not need to go update those invocations. However, if I've followed clippy's advice, I have a lot more refactoring to do -- I have to go call HashMap::default() or whatever everywhere.

These are just some examples to prove a point -- in my opinion, this pattern is clear, idiomatic, and consistent.

@sezna sezna enabled auto-merge October 31, 2024 18:12
@sezna sezna added this pull request to the merge queue Oct 31, 2024
@DmitryVasilevsky
Copy link
Contributor

I agree with Clippy here, but since most people disagree, I'll just have to live with it.

Merged via the queue into main with commit c209384 Oct 31, 2024
18 checks passed
@sezna sezna deleted the alex/clippy-crusade branch October 31, 2024 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants