Skip to main content

rustc_attr_parsing/attributes/diagnostic/
on_unknown.rs

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