Skip to content

Commit 250f636

Browse files
authored
Rollup merge of rust-lang#98873 - TaKO8Ki:suggest-default-derive-to-enum-with-default-attribute, r=fee1-dead
Suggest `#[derive(Default)]` to enums with `#[default]` fixes rust-lang#95226
2 parents 7f27201 + eb80407 commit 250f636

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,15 @@ impl<'a> Resolver<'a> {
14951495
err.help("have you added the `#[macro_use]` on the module/import?");
14961496
return;
14971497
}
1498+
if ident.name == kw::Default
1499+
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
1500+
&& let Some(span) = self.opt_span(def_id)
1501+
{
1502+
err.span_help(
1503+
self.session.source_map().guess_head_span(span),
1504+
"consider adding `#[derive(Default)]` to this enum",
1505+
);
1506+
}
14981507
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
14991508
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
15001509
ident,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub enum Test { //~ HELP consider adding `#[derive(Default)]` to this enum
2+
#[default]
3+
//~^ ERROR cannot find attribute `default` in this scope
4+
First,
5+
Second,
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot find attribute `default` in this scope
2+
--> $DIR/suggest-default-attribute.rs:2:7
3+
|
4+
LL | #[default]
5+
| ^^^^^^^
6+
|
7+
help: consider adding `#[derive(Default)]` to this enum
8+
--> $DIR/suggest-default-attribute.rs:1:1
9+
|
10+
LL | pub enum Test {
11+
| ^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)