1
Fork 0

Use E0665 for missing #[default] error

Use orphaned error code for the same error it belonged to before.

```
error[E0665]: `#[derive(Default)]` on enum with no `#[default]`
  --> $DIR/macros-nonfatal-errors.rs:42:10
   |
LL |   #[derive(Default)]
   |            ^^^^^^^
LL | / enum NoDeclaredDefault {
LL | |     Foo,
LL | |     Bar,
LL | | }
   | |_- this enum needs a unit variant marked with `#[default]`
   |
   = note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)
help: make this unit variant default by placing `#[default]` on it
   |
LL |     #[default] Foo,
   |     ~~~~~~~~~~~~~~
help: make this unit variant default by placing `#[default]` on it
   |
LL |     #[default] Bar,
   |     ~~~~~~~~~~~~~~
```
This commit is contained in:
Esteban Küber 2024-12-21 02:43:09 +00:00
parent d520b18316
commit 94812f1c8f
6 changed files with 84 additions and 53 deletions

View file

@ -1,10 +1,9 @@
#### Note: this error code is no longer emitted by the compiler.
The `Default` trait was derived on an enum.
The `Default` trait was derived on an enum without specifying the default
variant.
Erroneous code example:
```compile_fail
```compile_fail,E0665
#[derive(Default)]
enum Food {
Sweet,
@ -16,8 +15,8 @@ The `Default` cannot be derived on an enum for the simple reason that the
compiler doesn't know which value to pick by default whereas it can for a
struct as long as all its fields implement the `Default` trait as well.
For the case where the desired default variant has no data, you can annotate
it with `#[default]` to derive it:
For the case where the desired default variant has no payload, you can
annotate it with `#[default]` to derive it:
```
#[derive(Default)]
@ -28,8 +27,8 @@ enum Food {
}
```
In the case where the default variant does have data, you will have to
implement `Default` on your enum "by hand":
In the case where the default variant does have a payload, you will have to
implement `Default` on your enum manually:
```
enum Food {