Skip to content

Suggest a code fix when an attribute macro is incorrectly used as a derive macro #109589

Closed
@LukeMathWalker

Description

@LukeMathWalker

Code

#[proc_macro_attribute]
pub fn attribute_macro(_metadata: TokenStream, input: TokenStream) -> TokenStream {
    input
}

Current output

error: expected derive macro, found attribute macro `my_macro::attribute_macro`
  |
1 | #[derive(my_macro::attribute_macro)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^ not a derive macro

Desired output

(The actual desired output should have colours and minuses on the removed derive, but to give the idea)

error: expected derive macro, found attribute macro `my_macro::attribute_macro`
  |
1 | #[derive(my_macro::attribute_macro)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^ not a derive macro
  = help: remove the "derive":

  #[my_macro::attribute_macro]

Rationale and extra context

Many users are not familiar with the terminology being used by the compiler error (i.e. the different flavours of procedural macro, derive vs attribute) and might be confused as to what they need to do in order to fix the error.
Since the fix is straight-forward (i.e. remove the derive), the compiler should show a code snippet that fixes the issue.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions