Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest adding a derive for #[default] applied to variants #99002

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,10 +1499,16 @@ impl<'a> Resolver<'a> {
&& 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",
);
let source_map = self.session.source_map();
let head_span = source_map.guess_head_span(span);
if let Ok(head) = source_map.span_to_snippet(head_span) {
err.span_suggestion(head_span, "consider adding a derive", format!("#[derive(Default)]\n{head}"), Applicability::MaybeIncorrect);
} else {
err.span_help(
head_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(
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/enum/suggest-default-attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub enum Test { //~ HELP consider adding `#[derive(Default)]` to this enum
pub enum Test { //~ HELP consider adding a derive
#[default]
//~^ ERROR cannot find attribute `default` in this scope
First,
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/enum/suggest-default-attribute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ error: cannot find attribute `default` in this scope
LL | #[default]
| ^^^^^^^
|
help: consider adding `#[derive(Default)]` to this enum
--> $DIR/suggest-default-attribute.rs:1:1
help: consider adding a derive
|
LL + #[derive(Default)]
LL ~ pub enum Test {
|
LL | pub enum Test {
| ^^^^^^^^^^^^^

error: aborting due to previous error