Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unmatch_args.rs

1use rustc_hir::attrs::diagnostic::Directive;
2use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
3
4use crate::attributes::diagnostic::*;
5use crate::attributes::prelude::*;
6use crate::errors::DiagnosticOnUnmatchArgsOnlyForMacros;
7
8#[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)]
9pub(crate) struct OnUnmatchArgsParser {
10    span: Option<Span>,
11    directive: Option<(Span, Directive)>,
12}
13
14impl AttributeParser for OnUnmatchArgsParser {
15    const ATTRIBUTES: AcceptMapping<Self> = &[(
16        &[sym::diagnostic, sym::on_unmatch_args],
17        ::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 = "...""#]),
18        |this, cx, args| {
19            if !cx.features().diagnostic_on_unmatch_args() {
20                return;
21            }
22
23            let span = cx.attr_span;
24            this.span = Some(span);
25
26            if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::MacroDef => true,
    _ => false,
}matches!(cx.target, Target::MacroDef) {
27                cx.emit_lint(
28                    MISPLACED_DIAGNOSTIC_ATTRIBUTES,
29                    DiagnosticOnUnmatchArgsOnlyForMacros,
30                    span,
31                );
32                return;
33            }
34
35            let mode = Mode::DiagnosticOnUnmatchArgs;
36            let Some(items) = parse_list(cx, args, mode) else { return };
37
38            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
39                return;
40            };
41            merge_directives(cx, &mut this.directive, (span, directive));
42        },
43    )];
44
45    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
46
47    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
48        if let Some(_span) = self.span {
49            Some(AttributeKind::OnUnmatchArgs { directive: self.directive.map(|d| Box::new(d.1)) })
50        } else {
51            None
52        }
53    }
54}