Commit e690992
committed
Implement
Detect when a manual `Default` implementation isn't using the existing default field values and suggest using `..` instead:
```
error: `Default` impl doesn't use the declared default field values
--> $DIR/manual-default-impl-could-be-derived.rs:13:1
|
LL | / impl Default for A {
LL | | fn default() -> Self {
LL | | A {
LL | | x: S,
LL | | y: 0,
| | - this field has a default value
... |
LL | | }
| |_^
|
note: the lint level is defined here
--> $DIR/manual-default-impl-could-be-derived.rs:4:35
|
LL | #![deny(default_could_be_derived, default_overrides_default_fields)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use the default values in the `impl` to avoid them diverging over time
|
LL - x: S,
LL - y: 0,
LL + x: S, ..
|
```
Detect when a manual `Default` implementation for a type containing at least one default field value has *all* fields that could be derived and suggest `#[derive(Default)]`:
```
error: `Default` impl that could be derived
--> $DIR/manual-default-impl-could-be-derived.rs:27:1
|
LL | struct B {
| -------- all the fields in this struct have default values
LL | x: S = S,
| - default value
LL | y: i32 = 1,
| - default value
...
LL | / impl Default for B {
LL | | fn default() -> Self {
LL | | B {
LL | | x: S,
... |
LL | | }
| |_^
|
note: the lint level is defined here
--> $DIR/manual-default-impl-could-be-derived.rs:4:9
|
LL | #![deny(default_could_be_derived, default_overrides_default_fields)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: to avoid divergence in behavior between `Struct { .. }` and `<Struct as Default>::default()`, derive the `Default`
|
LL ~ #[derive(Default)] struct B {
|
```
Store a mapping between 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.default_could_be_derived and default_overrides_default_fields lints1 parent f2b91cc commit e690992
File tree
26 files changed
+945
-30
lines changed- compiler
- rustc_ast_lowering/src
- rustc_ast_pretty/src/pprust/state
- rustc_ast/src
- rustc_expand/src
- rustc_hir_pretty/src
- rustc_hir_typeck/src
- rustc_hir/src
- rustc_lint/src
- rustc_metadata/src/rmeta
- decoder
- rustc_middle/src
- query
- ty
- rustc_mir_build/src/thir/cx
- rustc_parse/src/parser
- rustc_passes/src
- rustc_privacy/src
- rustc_resolve/src
- tests/ui/structs
26 files changed
+945
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1449 | 1449 | | |
1450 | 1450 | | |
1451 | 1451 | | |
1452 | | - | |
1453 | | - | |
| 1452 | + | |
| 1453 | + | |
1454 | 1454 | | |
1455 | 1455 | | |
1456 | 1456 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1772 | 1772 | | |
1773 | 1773 | | |
1774 | 1774 | | |
1775 | | - | |
| 1775 | + | |
1776 | 1776 | | |
1777 | 1777 | | |
1778 | 1778 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1095 | 1095 | | |
1096 | 1096 | | |
1097 | 1097 | | |
1098 | | - | |
| 1098 | + | |
1099 | 1099 | | |
1100 | 1100 | | |
1101 | 1101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
359 | 359 | | |
360 | 360 | | |
361 | 361 | | |
362 | | - | |
| 362 | + | |
363 | 363 | | |
364 | 364 | | |
365 | 365 | | |
| |||
1420 | 1420 | | |
1421 | 1421 | | |
1422 | 1422 | | |
1423 | | - | |
| 1423 | + | |
1424 | 1424 | | |
1425 | 1425 | | |
1426 | 1426 | | |
| |||
1531 | 1531 | | |
1532 | 1532 | | |
1533 | 1533 | | |
1534 | | - | |
| 1534 | + | |
1535 | 1535 | | |
1536 | 1536 | | |
1537 | 1537 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
368 | 368 | | |
369 | 369 | | |
370 | 370 | | |
371 | | - | |
| 371 | + | |
372 | 372 | | |
373 | 373 | | |
374 | 374 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2112 | 2112 | | |
2113 | 2113 | | |
2114 | 2114 | | |
2115 | | - | |
| 2115 | + | |
2116 | 2116 | | |
2117 | 2117 | | |
2118 | 2118 | | |
| |||
2187 | 2187 | | |
2188 | 2188 | | |
2189 | 2189 | | |
2190 | | - | |
| 2190 | + | |
2191 | 2191 | | |
2192 | 2192 | | |
2193 | 2193 | | |
2194 | 2194 | | |
2195 | | - | |
| 2195 | + | |
2196 | 2196 | | |
2197 | 2197 | | |
2198 | 2198 | | |
2199 | 2199 | | |
2200 | 2200 | | |
2201 | 2201 | | |
2202 | | - | |
| 2202 | + | |
2203 | 2203 | | |
2204 | 2204 | | |
2205 | 2205 | | |
2206 | 2206 | | |
2207 | | - | |
| 2207 | + | |
2208 | 2208 | | |
2209 | 2209 | | |
2210 | 2210 | | |
2211 | 2211 | | |
2212 | 2212 | | |
2213 | 2213 | | |
2214 | | - | |
| 2214 | + | |
2215 | 2215 | | |
2216 | 2216 | | |
2217 | 2217 | | |
2218 | 2218 | | |
2219 | | - | |
| 2219 | + | |
2220 | 2220 | | |
2221 | 2221 | | |
2222 | 2222 | | |
2223 | 2223 | | |
2224 | 2224 | | |
2225 | 2225 | | |
2226 | | - | |
| 2226 | + | |
2227 | 2227 | | |
2228 | 2228 | | |
2229 | 2229 | | |
2230 | 2230 | | |
2231 | | - | |
| 2231 | + | |
2232 | 2232 | | |
2233 | 2233 | | |
2234 | 2234 | | |
| |||
2424 | 2424 | | |
2425 | 2425 | | |
2426 | 2426 | | |
2427 | | - | |
| 2427 | + | |
2428 | 2428 | | |
2429 | 2429 | | |
2430 | 2430 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
750 | 750 | | |
751 | 751 | | |
752 | 752 | | |
753 | | - | |
| 753 | + | |
754 | 754 | | |
755 | 755 | | |
756 | 756 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1225 | 1225 | | |
1226 | 1226 | | |
1227 | 1227 | | |
1228 | | - | |
| 1228 | + | |
1229 | 1229 | | |
1230 | 1230 | | |
1231 | 1231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
707 | 707 | | |
708 | 708 | | |
709 | 709 | | |
710 | | - | |
| 710 | + | |
711 | 711 | | |
712 | 712 | | |
713 | 713 | | |
| |||
0 commit comments