From 2f1a0315318433ba420e72204acad8a703885871 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 15 Dec 2023 22:52:57 +0100 Subject: [PATCH] rustc_macros: Require that diagnostic structs are marked `#[must_use]` --- .../rustc_macros/src/diagnostics/diagnostic.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic.rs b/compiler/rustc_macros/src/diagnostics/diagnostic.rs index 31ad9cdb21619..9d547702e9edb 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic.rs @@ -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);