Skip to main content

rustc_attr_parsing/attributes/diagnostic/
do_not_recommend.rs

1use rustc_feature::{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
23    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
24        let attr_span = cx.attr_span;
25        if !#[allow(non_exhaustive_omitted_patterns)] match args {
    ArgParser::NoArgs => true,
    _ => false,
}matches!(args, ArgParser::NoArgs) {
26            cx.emit_lint(
27                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
28                crate::errors::DoNotRecommendDoesNotExpectArgs,
29                attr_span,
30            );
31        }
32
33        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Impl { of_trait: true } => true,
    _ => false,
}matches!(cx.target, Target::Impl { of_trait: true }) {
34            let target_span = cx.target_span;
35            cx.emit_lint(
36                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
37                IncorrectDoNotRecommendLocation { target_span },
38                attr_span,
39            );
40            return None;
41        }
42
43        Some(AttributeKind::DoNotRecommend { attr_span })
44    }
45}