rustc_attr_parsing/attributes/diagnostic/
on_unmatch_args.rs1use rustc_errors::Diagnostic;
2use rustc_hir::attrs::diagnostic::Directive;
3use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
4
5use crate::attributes::diagnostic::*;
6use crate::attributes::prelude::*;
7use crate::errors::DiagnosticOnUnmatchArgsOnlyForMacros;
8
9#[derive(#[automatically_derived]
impl ::core::default::Default for OnUnmatchArgsParser {
#[inline]
fn default() -> OnUnmatchArgsParser {
OnUnmatchArgsParser {
span: ::core::default::Default::default(),
directive: ::core::default::Default::default(),
}
}
}Default)]
10pub(crate) struct OnUnmatchArgsParser {
11 span: Option<Span>,
12 directive: Option<(Span, Directive)>,
13}
14
15impl<S: Stage> AttributeParser<S> for OnUnmatchArgsParser {
16 const ATTRIBUTES: AcceptMapping<Self, S> = &[(
17 &[sym::diagnostic, sym::on_unmatch_args],
18 ::rustc_feature::AttributeTemplate {
word: false,
list: Some(&[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
19 |this, cx, args| {
20 if !cx.features().diagnostic_on_unmatch_args() {
21 return;
22 }
23
24 let span = cx.attr_span;
25 this.span = Some(span);
26
27 if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
Target::MacroDef => true,
_ => false,
}matches!(cx.target, Target::MacroDef) {
28 cx.emit_lint(
29 MISPLACED_DIAGNOSTIC_ATTRIBUTES,
30 move |dcx, level| DiagnosticOnUnmatchArgsOnlyForMacros.into_diag(dcx, level),
31 span,
32 );
33 return;
34 }
35
36 let mode = Mode::DiagnosticOnUnmatchArgs;
37 let Some(items) = parse_list(cx, args, mode) else { return };
38
39 let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
40 return;
41 };
42 merge_directives(cx, &mut this.directive, (span, directive));
43 },
44 )];
45
46 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
47
48 fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
49 if let Some(span) = self.span {
50 Some(AttributeKind::OnUnmatchArgs {
51 span,
52 directive: self.directive.map(|d| Box::new(d.1)),
53 })
54 } else {
55 None
56 }
57 }
58}