Skip to content

Commit 2f75c54

Browse files
committed
diagnostics: suggest deriving Default for enums
1 parent fffc4fc commit 2f75c54

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,8 +3300,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33003300
};
33013301
if let Some(diagnostic_name) = self.tcx.get_diagnostic_name(trait_pred.def_id()) {
33023302
let can_derive = match diagnostic_name {
3303-
sym::Default => !adt.is_enum(),
3304-
sym::Eq
3303+
sym::Default
3304+
| sym::Eq
33053305
| sym::PartialEq
33063306
| sym::Ord
33073307
| sym::PartialOrd

tests/ui/suggestions/derive-trait-for-method-call.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ LL | impl<X: Clone + Default + , Y: Clone + Default> Foo<X, Y> {
2525
| | | unsatisfied trait bound introduced here
2626
| | unsatisfied trait bound introduced here
2727
| unsatisfied trait bound introduced here
28-
note: the trait `Default` must be implemented
29-
--> $SRC_DIR/core/src/default.rs:LL:COL
30-
help: consider annotating `Enum` with `#[derive(Clone)]`
28+
help: consider annotating `CloneEnum` with `#[derive(Default)]`
3129
|
32-
LL + #[derive(Clone)]
30+
LL + #[derive(Default)]
31+
LL | enum CloneEnum {
32+
|
33+
help: consider annotating `Enum` with `#[derive(Clone, Default)]`
34+
|
35+
LL + #[derive(Clone, Default)]
3336
LL | enum Enum {
3437
|
3538

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// A test showing that we suggest deriving `Default` for enums.
2+
enum MyEnum {
3+
A,
4+
}
5+
6+
trait Foo {
7+
fn bar(&self) {}
8+
}
9+
impl<T: Default> Foo for T {}
10+
11+
fn main() {
12+
let x = MyEnum::A;
13+
x.bar();
14+
//~^ ERROR the method `bar` exists for enum `MyEnum`, but its trait bounds were not satisfied
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0599]: the method `bar` exists for enum `MyEnum`, but its trait bounds were not satisfied
2+
--> $DIR/suggest-derive-default-for-enums.rs:13:7
3+
|
4+
LL | enum MyEnum {
5+
| ----------- method `bar` not found for this enum because it doesn't satisfy `MyEnum: Default` or `MyEnum: Foo`
6+
...
7+
LL | x.bar();
8+
| ^^^ method cannot be called on `MyEnum` due to unsatisfied trait bounds
9+
|
10+
note: trait bound `MyEnum: Default` was not satisfied
11+
--> $DIR/suggest-derive-default-for-enums.rs:9:9
12+
|
13+
LL | impl<T: Default> Foo for T {}
14+
| ^^^^^^^ --- -
15+
| |
16+
| unsatisfied trait bound introduced here
17+
help: consider annotating `MyEnum` with `#[derive(Default)]`
18+
|
19+
LL + #[derive(Default)]
20+
LL | enum MyEnum {
21+
|
22+
23+
error: aborting due to 1 previous error
24+
25+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)