Skip to content

Commit

Permalink
Rollup merge of #98873 - TaKO8Ki:suggest-default-derive-to-enum-with-…
Browse files Browse the repository at this point in the history
…default-attribute, r=fee1-dead

Suggest `#[derive(Default)]` to enums with `#[default]`

fixes #95226
  • Loading branch information
Dylan-DPC authored Jul 5, 2022
2 parents 6e5f1d4 + eb80407 commit 9a2274c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,15 @@ impl<'a> Resolver<'a> {
err.help("have you added the `#[macro_use]` on the module/import?");
return;
}
if ident.name == kw::Default
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
&& let Some(span) = self.opt_span(def_id)
{
err.span_help(
self.session.source_map().guess_head_span(span),
"consider adding `#[derive(Default)]` to this enum",
);
}
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
ident,
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/enum/suggest-default-attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub enum Test { //~ HELP consider adding `#[derive(Default)]` to this enum
#[default]
//~^ ERROR cannot find attribute `default` in this scope
First,
Second,
}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/enum/suggest-default-attribute.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: cannot find attribute `default` in this scope
--> $DIR/suggest-default-attribute.rs:2:7
|
LL | #[default]
| ^^^^^^^
|
help: consider adding `#[derive(Default)]` to this enum
--> $DIR/suggest-default-attribute.rs:1:1
|
LL | pub enum Test {
| ^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit 9a2274c

Please sign in to comment.