Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unknown.rs

1use rustc_hir::attrs::diagnostic::Directive;
2use rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES;
3
4use crate::ShouldEmit;
5use crate::attributes::diagnostic::*;
6use crate::attributes::prelude::*;
7use crate::errors::DiagnosticOnUnknownOnlyForImports;
8
9#[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)]
10pub(crate) struct OnUnknownParser {
11    span: Option<Span>,
12    directive: Option<(Span, Directive)>,
13}
14
15impl OnUnknownParser {
16    fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
17        if let Some(features) = cx.features
18            && !features.diagnostic_on_unknown()
19        {
20            // `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
21            return;
22        }
23        let span = cx.attr_span;
24        self.span = Some(span);
25
26        // At early parsing we get passed `Target::Crate` regardless of the item we're on.
27        // Therefore, only do target checking if we can emit.
28        let early = #[allow(non_exhaustive_omitted_patterns)] match cx.should_emit {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(cx.should_emit, ShouldEmit::Nothing);
29
30        if !early && !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Use => true,
    _ => false,
}matches!(cx.target, Target::Use) {
31            let target_span = cx.target_span;
32            cx.emit_lint(
33                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
34                DiagnosticOnUnknownOnlyForImports { target_span },
35                span,
36            );
37            return;
38        }
39
40        let Some(items) = parse_list(cx, args, mode) else { return };
41
42        if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
43            merge_directives(cx, &mut self.directive, (span, directive));
44        };
45    }
46}
47
48impl AttributeParser for OnUnknownParser {
49    const ATTRIBUTES: AcceptMapping<Self> = &[(
50        &[sym::diagnostic, sym::on_unknown],
51        ::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 = "...""#]),
52        |this, cx, args| {
53            this.parse(cx, args, Mode::DiagnosticOnUnknown);
54        },
55    )];
56    // "Allowed" for all targets, but noop for all but use statements.
57    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
58
59    fn finalize(self, _cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
60        if let Some(_span) = self.span {
61            Some(AttributeKind::OnUnknown { directive: self.directive.map(|d| Box::new(d.1)) })
62        } else {
63            None
64        }
65    }
66}