Allow Default::default()
pattern
#1991
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 findDefault::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()
, orDisplay::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
toBTreeMap
, orVec
toHashSet
. If I've usedDefault::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 callHashMap::default()
or whatever everywhere.These are just some examples to prove a point -- in my opinion, this pattern is clear, idiomatic, and consistent.