-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Lint against manual impl Default
that could have been derive
d
#134175
base: master
Are you sure you want to change the base?
Lint against manual impl Default
that could have been derive
d
#134175
Conversation
This comment has been minimized.
This comment has been minimized.
This adds a new warn-by-default lint, so make sure it gets I-lang-nominated when it's ready |
This comment has been minimized.
This comment has been minimized.
5d36626
to
ab57d60
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The new |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
5b4a086
to
0411790
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
874b484
to
a861c8e
Compare
This comment has been minimized.
This comment has been minimized.
a861c8e
to
4d2f468
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
ffdc153
to
55a1857
Compare
Store a mapping betweein the `DefId` for an `impl Default for Ty` and the `DefId` of either a Unit variant/struct or an fn with no arguments that is called within `<Ty as Default>::default()`. When linting `impl`s, if it is for `Default`, we evaluate the contents of their `fn default()`. If it is *only* an ADT literal for `Self` and every field is either a "known to be defaulted" value (`0` or `false`), an explicit `Default::default()` call or a call or path to the same "equivalent" `DefId` from that field's type's `Default::default()` implementation.
21ac4fd
to
6fa30b6
Compare
Let me know if this is ready for review / you want me to look at it, I'm going to mark it as waiting-on-author for the time being. |
@bors try |
…ive, r=<try> Lint against manual `impl Default` that could have been `derive`d ``` error: `impl Default` that could be derived --> $DIR/manual-default-impl-could-be-derived.rs:74:1 | LL | / impl Default for G { LL | | fn default() -> Self { LL | | G { LL | | f: F::Unit, LL | | } LL | | } LL | | } | |_^ | help: you don't need to manually `impl Default`, you can derive it | LL ~ #[derive(Default)] struct G { | ``` As part of rust-lang#132162/rust-lang/rfcs#3681 we want to lint when default fields values could preclude the need of a manual `impl Default`, but there are already cases where these manual impls could be derived. This PR introduces a new `default_could_be_derived` lint that makes a best effort check of the body of the `Default::default()` implementation to see if all the fields of a single expression in that body are either known to be `Default` already (like an explicit call to `Default::default()`, a `0` literal, or `Option::None` path) or are identified to be equivalent to the field's type's `Default` value (by opportunistically looking at the `Default::default()` body for that field's type).
The job Click to see the possible cause of the failure (guessed by this bot)
|
💔 Test failed - checks-actions |
☔ The latest upstream changes (presumably #134243) made this pull request unmergeable. Please resolve the merge conflicts. |
Use `#[derive(Default)]` instead of manual `impl` when possible While working on rust-lang#134175 I noticed a few manual `Default` `impl`s that could be `derive`d instead. These likely predate the existence of the `#[default]` attribute for `enum`s.
Rollup merge of rust-lang#134363 - estebank:derive-default, r=SparrowLii Use `#[derive(Default)]` instead of manual `impl` when possible While working on rust-lang#134175 I noticed a few manual `Default` `impl`s that could be `derive`d instead. These likely predate the existence of the `#[default]` attribute for `enum`s.
As part of #132162/rust-lang/rfcs#3681 we want to lint when default fields values could preclude the need of a manual
impl Default
, but there are already cases where these manual impls could be derived. This PR introduces a newdefault_could_be_derived
lint that makes a best effort check of the body of theDefault::default()
implementation to see if all the fields of a single expression in that body are either known to beDefault
already (like an explicit call toDefault::default()
, a0
literal, orOption::None
path) or are identified to be equivalent to the field's type'sDefault
value (by opportunistically looking at theDefault::default()
body for that field's type).