Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unmatched_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::diagnostics::DiagnosticOnUnmatchedArgsOnlyForMacros;
8
9#[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)]
10pub(crate) struct OnUnmatchedArgsParser {
11    span: Option<Span>,
12    directive: Option<(Span, Directive)>,
13}
14
15impl AttributeParser for OnUnmatchedArgsParser {
16    const ATTRIBUTES: AcceptMapping<Self> = &[(
17        &[sym::diagnostic, sym::on_unmatched_args],
18        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 = "...""#]),
19        AttributeStability::Stable, // Unstable, stability checked manually in the parser
20        |this, cx, args| {
21            if !cx.features().diagnostic_on_unmatched_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                    DiagnosticOnUnmatchedArgsOnlyForMacros,
32                    span,
33                );
34                return;
35            }
36
37            let mode = Mode::DiagnosticOnUnmatchedArgs;
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::OnUnmatchedArgs {
52                directive: self.directive.map(|d| Box::new(d.1)),
53            })
54        } else {
55            None
56        }
57    }
58}