Uh oh!
There was an error while loading. Please reload this page.
Lint against manual impl Default that could have been derived - #134175
Lint against manual impl Default that could have been derived#134175estebank wants to merge 26 commits into
impl Default that could have been derived#134175Conversation
rustbot
commented
Dec 11, 2024
This comment has been minimized.
This comment has been minimized.
compiler-errors
commented
Dec 11, 2024
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
ab57d60Compare
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.
estebank
commented
Dec 12, 2024
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
0411790Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
874b484 to
a861c8eCompare
This comment has been minimized.
This comment has been minimized.
a861c8e to
4d2f468Compare
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
55a1857Compare21ac4fd to
6fa30b6CompareLet 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. |
estebank
commented
Dec 16, 2024
@bors try |
bors
commented
Dec 16, 2024
…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).rust-log-analyzer
commented
Dec 16, 2024
The job Click to see the possible cause of the failure (guessed by this bot) |
bors
commented
Dec 16, 2024
💔 Test failed - checks-actions |
bors
commented
Dec 18, 2024
☔ 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.
Nadrieril
commented
Jan 15, 2025
Analyzing the body of a user-written function in search for known patterns feels very much like clippy territory to me. In fact clippy's |
estebank
commented
Jan 15, 2025
@Nadrieril yeah, I've filed a PR with some of the more advanced analysis to clippy, but not all of it. |
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_derivedlint 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 beDefaultalready (like an explicit call toDefault::default(), a0literal, orOption::Nonepath) or are identified to be equivalent to the field's type'sDefaultvalue (by opportunistically looking at theDefault::default()body for that field's type).