Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unmatch_args.rs

1use rustc_feature::AttributeStability;
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 AttributeParser for OnUnmatchArgsParser {
16    const ATTRIBUTES: AcceptMapping<Self> = &[(
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        AttributeStability::Stable, // Unstable, stability checked manually in the parser
20        |this, cx, args| {
21            if !cx.features().diagnostic_on_unmatch_args() {
22                return;
23            }
24
25            let span = cx.attr_span;
26            this.span = Some(span);
27
28            if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::MacroDef => true,
    _ => false,
}matches!(cx.target, Target::MacroDef) {
29                cx.emit_lint(
30                    MISPLACED_DIAGNOSTIC_ATTRIBUTES,
31                    DiagnosticOnUnmatchArgsOnlyForMacros,
32                    span,
33                );
34                return;
35            }
36
37            let mode = Mode::DiagnosticOnUnmatchArgs;
38            let Some(items) = parse_list(cx, args, mode) else { return };
39
40            let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) else {
41                return;
42            };
43            merge_directives(cx, &mut this.directive, (span, directive));
44        },
45    )];
46
47    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
48
49    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
50        if let Some(_span) = self.span {
51            Some(AttributeKind::OnUnmatchArgs { directive: self.directive.map(|d| Box::new(d.1)) })
52        } else {
53            None
54        }
55    }
56}