Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unmatched_args.rs

1use rustc_feature::AttributeStability;
2use rustc_hir::attrs::diagnostic::Directive;
3
4use crate::attributes::diagnostic::*;
5use crate::attributes::prelude::*;
6
7#[derive(#[automatically_derived]
impl ::core::default::Default for OnUnmatchedArgsParser {
    #[inline]
    fn default() -> OnUnmatchedArgsParser {
        OnUnmatchedArgsParser {
            span: ::core::default::Default::default(),
            directive: ::core::default::Default::default(),
        }
    }
}Default)]
8pub(crate) struct OnUnmatchedArgsParser {
9    span: Option<Span>,
10    directive: Option<(Span, Directive)>,
11}
12
13impl AttributeParser for OnUnmatchedArgsParser {
14    const ATTRIBUTES: AcceptMapping<Self> = &[(
15        &[sym::diagnostic, sym::on_unmatched_args],
16        crate::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 = "...""#]),
17        AttributeStability::Stable, // Unstable, stability checked manually in the parser
18        |this, cx, args| {
19            if !cx.features().diagnostic_on_unmatched_args() {
20                // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
21                args.ignore_args();
22                return;
23            }
24
25            let span = cx.attr_span;
26            this.span = Some(span);
27
28            let mode = Mode::DiagnosticOnUnmatchedArgs;
29            let Some(items) = parse_list(cx, args, mode) else { return };
30
31            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
32                return;
33            };
34            merge_directives(cx, &mut this.directive, (span, directive));
35        },
36    )];
37
38    const ALLOWED_TARGETS: AllowedTargets<'_> =
39        AllowedTargets::AllowListWarnRest(&[Allow(Target::MacroDef)]);
40
41    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
42        if let Some(_span) = self.span {
43            Some(AttributeKind::OnUnmatchedArgs {
44                directive: self.directive.map(|d| Box::new(d.1)),
45            })
46        } else {
47            None
48        }
49    }
50}