You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New lint to ensure Default::default calls T::new, rather than vice versa. #12662
This is going to end up with way too many false-positives and be more work to write and maintain if Default is derived. This would have to be limited to Default impls that could be const if they weren't a trait function. Probably only for exported types as well.
I would actually suggest the opposite IF default was derived. Case to the point is a bug I just caught in the core lib - rust-lang/rust#135977 -- where fn new would instantiate struct with non-zero values, while default() was auto-derived.
After some more thinking, I think this would actually be a bad pattern, at least for the cases when the Default impl is auto derived. If Default is not autoderived, this same pattern should still be followed for consistency.
// User code should follow this pattern when possible:#[derive(Default)]pubstructConfig;implConfig{pubfnnew() -> Self{Self::default()}}
@Skgland I see you disagreed - could you clarify why? Do you think new() -> Self should have a different implementation than the auto-generated Default trait?
If Default is not autoderived, this same pattern should still be followed for consistency.
Was the part I disagree with.
In case default is derived I would agree that it's reasonable to have new call default, but in the case where default is manually implemented having default call new so the later can be made const is in my opinion preferable (at least until const Traits are available).
Activity
Jarcho commentedon Apr 12, 2024
This is going to end up with way too many false-positives and be more work to write and maintain if
Defaultis derived. This would have to be limited toDefaultimpls that could be const if they weren't a trait function. Probably only for exported types as well.nyurik commentedon Jan 24, 2025
I would actually suggest the opposite IF default was derived. Case to the point is a bug I just caught in the core lib - rust-lang/rust#135977 -- where
fn newwould instantiate struct with non-zero values, whiledefault()was auto-derived.nyurik commentedon Jan 25, 2025
I created a new lint suggestion that detects non-matching
newanddefault- #14075nyurik commentedon Jan 31, 2025
After some more thinking, I think this would actually be a bad pattern, at least for the cases when the
Defaultimpl is auto derived. If Default is not autoderived, this same pattern should still be followed for consistency.nyurik commentedon Jan 31, 2025
@Skgland I see you disagreed - could you clarify why? Do you think
new() -> Selfshould have a different implementation than the auto-generatedDefaulttrait?Skgland commentedon Jan 31, 2025
Was the part I disagree with.
In case default is derived I would agree that it's reasonable to have new call default, but in the case where default is manually implemented having default call new so the later can be made const is in my opinion preferable (at least until const Traits are available).
nyurik commentedon Jan 31, 2025
ah, yes, that does make sense, thx. Hopefully rust-lang/rust#67792 will get stabilized at some point.