Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unmatched_args.rs

1use rustc_attr_ir::diagnostic::Directive;
2use rustc_feature::AttributeStability;
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 below
18        |this, cx, args| {
19            {
    if let Some(features) = cx.features_option() &&
            !features.diagnostic_on_unmatched_args() {
        args.ignore_args();
        let nightly_build = cx.sess.is_nightly_build();
        let span = cx.attr_span;
        cx.emit_lint(rustc_lint_defs::builtin::UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
            crate::diagnostics::UnstableDiagnosticAttribute {
                feature: sym::diagnostic_on_unmatched_args,
                nightly_build,
            }, span);
        return;
    }
};gate_diagnostic_attr!(diagnostic_on_unmatched_args);
20
21            let span = cx.attr_span;
22            this.span = Some(span);
23
24            let mode = Mode::DiagnosticOnUnmatchedArgs;
25            let Some(items) = parse_list(cx, args, mode) else { return };
26
27            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
28                return;
29            };
30            merge_directives(cx, &mut this.directive, (span, directive));
31        },
32    )];
33
34    const ALLOWED_TARGETS: AllowedTargets<'_> =
35        AllowedTargets::AllowListWarnRest(&[Allow(Target::MacroDef)]);
36
37    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
38        if let Some(_span) = self.span {
39            Some(AttributeKind::OnUnmatchedArgs {
40                directive: self.directive.map(|d| Box::new(d.1)),
41            })
42        } else {
43            None
44        }
45    }
46}