Skip to content

Commit

Permalink
rustc_macros: Require that diagnostic structs are marked #[must_use]
Browse files Browse the repository at this point in the history
  • Loading branch information
Enselic committed Dec 15, 2023
1 parent 54943d6 commit 2f1a031
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/rustc_macros/src/diagnostics/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ impl<'a> DiagnosticDerive<'a> {
pub(crate) fn into_tokens(self) -> TokenStream {
let DiagnosticDerive { mut structure, mut builder } = self;

if matches!(structure.ast().data, syn::Data::Struct(_)) {
assert_eq!(structure.variants().len(), 1, "a struct can only have one variant");
if !structure.variants()[0]
.ast()
.attrs
.iter()
.any(|attr| attr.path().segments.last().unwrap().ident == "must_use")
{
span_err(
structure.ast().span().unwrap(),
"You must mark diagnostic structs with `#[must_use]`",
)
.emit();
}
}

let slugs = RefCell::new(Vec::new());
let implementation = builder.each_variant(&mut structure, |mut builder, variant| {
let preamble = builder.preamble(variant);
Expand Down

0 comments on commit 2f1a031

Please sign in to comment.