Skip to main content

rustc_attr_parsing/attributes/diagnostic/
do_not_recommend.rs

1use rustc_feature::{AttributeStability, AttributeTemplate, template};
2use rustc_hir::Target;
3use rustc_hir::attrs::AttributeKind;
4use rustc_session::lint::builtin::{
5    MALFORMED_DIAGNOSTIC_ATTRIBUTES, MISPLACED_DIAGNOSTIC_ATTRIBUTES,
6};
7use rustc_span::{Symbol, sym};
8
9use crate::attributes::{OnDuplicate, SingleAttributeParser};
10use crate::context::AcceptContext;
11use crate::errors::IncorrectDoNotRecommendLocation;
12use crate::parser::ArgParser;
13use crate::target_checking::{ALL_TARGETS, AllowedTargets};
14
15pub(crate) struct DoNotRecommendParser;
16impl SingleAttributeParser for DoNotRecommendParser {
17    const PATH: &[Symbol] = &[sym::diagnostic, sym::do_not_recommend];
18    const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
19    // "Allowed" on any target, noop on all but trait impls
20    const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
21    const TEMPLATE: AttributeTemplate = ::rustc_feature::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word /*doesn't matter */);
22    const STABILITY: AttributeStability = AttributeStability::Stable;
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                crate::errors::DoNotRecommendDoesNotExpectArgs,
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                IncorrectDoNotRecommendLocation { target_span },
39                attr_span,
40            );
41            return None;
42        }
43
44        Some(AttributeKind::DoNotRecommend)
45    }
46}