From eb80407d79a25cb9bdd98eca47764e57114d1d86 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 4 Jul 2022 20:46:59 +0900 Subject: [PATCH] suggest `#[derive(Default)]` to enums with `#[default]` --- compiler/rustc_resolve/src/diagnostics.rs | 9 +++++++++ src/test/ui/enum/suggest-default-attribute.rs | 8 ++++++++ src/test/ui/enum/suggest-default-attribute.stderr | 14 ++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/ui/enum/suggest-default-attribute.rs create mode 100644 src/test/ui/enum/suggest-default-attribute.stderr diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e8b7cee573490..4fbbd9deaebb4 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -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, diff --git a/src/test/ui/enum/suggest-default-attribute.rs b/src/test/ui/enum/suggest-default-attribute.rs new file mode 100644 index 0000000000000..33bd0d2408139 --- /dev/null +++ b/src/test/ui/enum/suggest-default-attribute.rs @@ -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() {} diff --git a/src/test/ui/enum/suggest-default-attribute.stderr b/src/test/ui/enum/suggest-default-attribute.stderr new file mode 100644 index 0000000000000..791f219e8f95f --- /dev/null +++ b/src/test/ui/enum/suggest-default-attribute.stderr @@ -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 +