rustc_attr_parsing/attributes/diagnostic/
on_unknown.rs1use rustc_feature::AttributeStability;
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 args.ignore_args();
23 return;
24 }
25 let span = cx.attr_span;
26 self.span = Some(span);
27
28 let early = #[allow(non_exhaustive_omitted_patterns)] match cx.should_emit {
ShouldEmit::Nothing => true,
_ => false,
}matches!(cx.should_emit, ShouldEmit::Nothing);
31
32 if !early && !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
Target::Use => true,
_ => false,
}matches!(cx.target, Target::Use) {
33 let target_span = cx.target_span;
34 cx.emit_lint(
35 MISPLACED_DIAGNOSTIC_ATTRIBUTES,
36 DiagnosticOnUnknownOnlyForImports { target_span },
37 span,
38 );
39 return;
40 }
41
42 let Some(items) = parse_list(cx, args, mode) else { return };
43
44 if let Some(directive) = parse_directive_items(cx, mode, items.mixed(), true) {
45 merge_directives(cx, &mut self.directive, (span, directive));
46 };
47 }
48}
49
50impl AttributeParser for OnUnknownParser {
51 const ATTRIBUTES: AcceptMapping<Self> = &[(
52 &[sym::diagnostic, sym::on_unknown],
53 ::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 = "...""#]),
54 AttributeStability::Stable, |this, cx, args| {
56 this.parse(cx, args, Mode::DiagnosticOnUnknown);
57 },
58 )];
59 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 { directive: self.directive.map(|d| Box::new(d.1)) })
65 } else {
66 None
67 }
68 }
69}