Skip to content

Commit 88b6ff7

Browse files
committed
rustc_macros: Require that diagnostic structs are marked #[must_use]
1 parent 603f710 commit 88b6ff7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic.rs

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ impl<'a> DiagnosticDerive<'a> {
3030
pub(crate) fn into_tokens(self) -> TokenStream {
3131
let DiagnosticDerive { mut structure, mut builder } = self;
3232

33+
if matches!(structure.ast().data, syn::Data::Struct(_)) {
34+
assert_eq!(structure.variants().len(), 1, "a struct can only have one variant");
35+
if !structure.variants()[0]
36+
.ast()
37+
.attrs
38+
.iter()
39+
.any(|attr| attr.path().segments.last().unwrap().ident == "must_use")
40+
{
41+
span_err(
42+
structure.ast().span().unwrap(),
43+
"You must mark diagnostic structs with `#[must_use]`",
44+
)
45+
.emit();
46+
}
47+
}
48+
3349
let slugs = RefCell::new(Vec::new());
3450
let implementation = builder.each_variant(&mut structure, |mut builder, variant| {
3551
let preamble = builder.preamble(variant);

0 commit comments

Comments
 (0)