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
As this bug in rust compiler shows, it is easy to implement a non-trivial new() method, while using #[derive(Default)] -- thus having two different ways to instantiate a struct, most likely creating bugs. I suspect there is a almost no good use case for the fn new() -> Self (without parameters) to contain anything other than { Self::default() }.
Other considerations
We may want to warn if there is a fn new() that returns anything other than Self, like Result or Option or some other type.
What it does
As this bug in rust compiler shows, it is easy to implement a non-trivial
new()
method, while using#[derive(Default)]
-- thus having two different ways to instantiate a struct, most likely creating bugs. I suspect there is a almost no good use case for thefn new() -> Self
(without parameters) to contain anything other than{ Self::default() }
.Other considerations
fn new()
that returns anything other thanSelf
, likeResult
orOption
or some other type.Default::default
callsT::new
, rather than vice versa. #12662 ideaAdvantage
Default
andnew
consistentDrawbacks
There might be some strange usecase for
new
to be different from default. I couldn't come up with one, but I am sure something could exist...Example
Could be written as:
The text was updated successfully, but these errors were encountered: