Skip to main content

rustc_attr_parsing/attributes/diagnostic/
do_not_recommend.rs

1use rustc_errors::Diagnostic;
2use rustc_feature::{AttributeTemplate, template};
3use rustc_hir::Target;
4use rustc_hir::attrs::AttributeKind;
5use rustc_session::lint::builtin::{
6    MALFORMED_DIAGNOSTIC_ATTRIBUTES, MISPLACED_DIAGNOSTIC_ATTRIBUTES,
7};
8use rustc_span::{Symbol, sym};
9
10use crate::attributes::{OnDuplicate, SingleAttributeParser};
11use crate::context::AcceptContext;
12use crate::errors::IncorrectDoNotRecommendLocation;
13use crate::parser::ArgParser;
14use crate::target_checking::{ALL_TARGETS, AllowedTargets};
15
16pub(crate) struct DoNotRecommendParser;
17impl SingleAttributeParser for DoNotRecommendParser {
18    const PATH: &[Symbol] = &[sym::diagnostic, sym::do_not_recommend];
19    const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
20    // "Allowed" on any target, noop on all but trait impls
21    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
22    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word /*doesn't matter */);
23
24    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
25        let attr_span = cx.attr_span;
26        if !#[allow(non_exhaustive_omitted_patterns)] match args {
    ArgParser::NoArgs => true,
    _ => false,
}matches!(args, ArgParser::NoArgs) {
27            cx.emit_lint(
28                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
29                |dcx, level| crate::errors::DoNotRecommendDoesNotExpectArgs.into_diag(dcx, level),
30                attr_span,
31            );
32        }
33
34        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Impl { of_trait: true } => true,
    _ => false,
}matches!(cx.target, Target::Impl { of_trait: true }) {
35            let target_span = cx.target_span;
36            cx.emit_lint(
37                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
38                move |dcx, level| {
39                    IncorrectDoNotRecommendLocation { target_span }.into_diag(dcx, level)
40                },
41                attr_span,
42            );
43            return None;
44        }
45
46        Some(AttributeKind::DoNotRecommend { attr_span })
47    }
48}