Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unknown.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 OnUnknownParser {
    #[inline]
    fn default() -> OnUnknownParser {
        OnUnknownParser {
            span: ::core::default::Default::default(),
            directive: ::core::default::Default::default(),
        }
    }
}Default)]
8pub(crate) struct OnUnknownParser {
9    span: Option<Span>,
10    directive: Option<(Span, Directive)>,
11}
12
13impl AttributeParser for OnUnknownParser {
14    const ATTRIBUTES: AcceptMapping<Self> = &[(
15        &[sym::diagnostic, sym::on_unknown],
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_unknown() {
        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_unknown,
                nightly_build,
            }, span);
        return;
    }
};gate_diagnostic_attr!(diagnostic_on_unknown);
20
21            let span = cx.attr_span;
22            this.span = Some(span);
23            let mode = Mode::DiagnosticOnUnknown;
24
25            let Some(items) = parse_list(cx, args, mode) else { return };
26
27            if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
28                merge_directives(cx, &mut this.directive, (span, directive));
29            };
30        },
31    )];
32    // "Allowed" for all targets, but noop for all but use statements.
33    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
34        Allow(Target::Use),
35        Allow(Target::Mod),
36        Allow(Target::Crate),
37    ]);
38
39    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
40        if let Some(_span) = self.span {
41            Some(AttributeKind::OnUnknown { directive: self.directive.map(|d| Box::new(d.1)) })
42        } else {
43            None
44        }
45    }
46}