-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New lint to ensure Default::default
calls T::new
, rather than vice versa.
#12662
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
Comments
This is going to end up with way too many false-positives and be more work to write and maintain if |
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 |
I created a new lint suggestion that detects non-matching |
After some more thinking, I think this would actually be a bad pattern, at least for the cases when the // User code should follow this pattern when possible:
#[derive(Default)]
pub struct Config;
impl Config {
pub fn new() -> Self {
Self::default()
}
} |
@Skgland I see you disagreed - could you clarify why? Do you think |
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). |
ah, yes, that does make sense, thx. Hopefully rust-lang/rust#67792 will get stabilized at some point. |
What it does
Detects cases where a parameterless
new
simply callsDefault::default
, and indicates thatdefault
should call new, rather than the opposite.Advantage
new
functions can potentially beconst
, whiledefault
cannot, and calling them in the opposite order can prohibit this.Drawbacks
The lint is completely irrelevant if the
new
function is never intended to beconst
.Example
Could be written as:
The text was updated successfully, but these errors were encountered: