Commit be8b02f
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 e108481 commit be8b02f
File tree
30 files changed
+1237
-40
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_middle/src/query
- rustc_mir_build/src/thir/cx
- rustc_parse/src/parser
- rustc_passes/src
- rustc_privacy/src
- rustc_resolve/src
- src/tools
- clippy
- clippy_lints/src
- utils
- clippy_utils/src
- rustfmt/src
- tests/ui/structs
30 files changed
+1237
-40
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1455 | 1455 | | |
1456 | 1456 | | |
1457 | 1457 | | |
1458 | | - | |
1459 | | - | |
| 1458 | + | |
| 1459 | + | |
1460 | 1460 | | |
1461 | 1461 | | |
1462 | 1462 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1771 | 1771 | | |
1772 | 1772 | | |
1773 | 1773 | | |
1774 | | - | |
| 1774 | + | |
1775 | 1775 | | |
1776 | 1776 | | |
1777 | 1777 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1094 | 1094 | | |
1095 | 1095 | | |
1096 | 1096 | | |
1097 | | - | |
| 1097 | + | |
1098 | 1098 | | |
1099 | 1099 | | |
1100 | 1100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
358 | 358 | | |
359 | 359 | | |
360 | 360 | | |
361 | | - | |
| 361 | + | |
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
| |||
1419 | 1419 | | |
1420 | 1420 | | |
1421 | 1421 | | |
1422 | | - | |
| 1422 | + | |
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
| |||
1530 | 1530 | | |
1531 | 1531 | | |
1532 | 1532 | | |
1533 | | - | |
| 1533 | + | |
1534 | 1534 | | |
1535 | 1535 | | |
1536 | 1536 | | |
| |||
| 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 | |
|---|---|---|---|
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
370 | | - | |
| 370 | + | |
371 | 371 | | |
372 | 372 | | |
373 | 373 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2116 | 2116 | | |
2117 | 2117 | | |
2118 | 2118 | | |
2119 | | - | |
| 2119 | + | |
2120 | 2120 | | |
2121 | 2121 | | |
2122 | 2122 | | |
| |||
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 | | |
2235 | | - | |
| 2235 | + | |
2236 | 2236 | | |
2237 | 2237 | | |
2238 | 2238 | | |
| |||
2428 | 2428 | | |
2429 | 2429 | | |
2430 | 2430 | | |
2431 | | - | |
| 2431 | + | |
2432 | 2432 | | |
2433 | 2433 | | |
2434 | 2434 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
749 | 749 | | |
750 | 750 | | |
751 | 751 | | |
752 | | - | |
| 752 | + | |
753 | 753 | | |
754 | 754 | | |
755 | 755 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1226 | 1226 | | |
1227 | 1227 | | |
1228 | 1228 | | |
1229 | | - | |
| 1229 | + | |
1230 | 1230 | | |
1231 | 1231 | | |
1232 | 1232 | | |
| |||
| 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