- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
Closed
Labels
A-attributesArea: Attributes (`#[…]`, `#![…]`)Area: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)T-langRelevant to the language teamRelevant to the language team
Description
The following code:
fn main() {
	struct Foo {}
	#[derive(Copy)] impl Foo {}
}produces the following error
error[E0774]: `derive` may only be applied to structs, enums and unions
 --> src/main.rs:3:2
  |
3 |     #[derive(Copy)] impl Foo {}
  |     ^^^^^^^^^^^^^^^
However, applying #[derive] to a let statement just gives an "unused attribute" warning:
fn main() {
    #[derive(Copy)] let _ = 1;
}warning: unused attribute
 --> src/main.rs:2:5
  |
2 |     #[derive(Copy)] let _ = 1;
  |     ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_attributes)]` on by default
A #[derive] attribute doesn't make any more sense on a statement than it does on an item. I think we should handle these two cases in the same way.
There's been a move towards forbidding attributes on targets that don't make sense: #77015. Unless anyone objects, I think we should consistently forbid #[derive] on all invalid targets.
petrochenkov, jyn514 and taiki-e
Metadata
Metadata
Assignees
Labels
A-attributesArea: Attributes (`#[…]`, `#![…]`)Area: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)T-langRelevant to the language teamRelevant to the language team