Skip to main content

rustc_attr_parsing/attributes/diagnostic/
do_not_recommend.rs

1use rustc_feature::AttributeStability;
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::diagnostics::IncorrectDoNotRecommendLocation;
12use crate::parser::ArgParser;
13use crate::target_checking::{ALL_TARGETS, AllowedTargets};
14use crate::{AttributeTemplate, template};
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 = crate::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word /*doesn't matter */);
23    const STABILITY: AttributeStability = AttributeStability::Stable;
24
25    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
26        let attr_span = cx.attr_span;
27        if !#[allow(non_exhaustive_omitted_patterns)] match args {
    ArgParser::NoArgs => true,
    _ => false,
}matches!(args, ArgParser::NoArgs) {
28            cx.emit_lint(
29                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
30                crate::diagnostics::DoNotRecommendDoesNotExpectArgs,
31                attr_span,
32            );
33        }
34
35        if !#[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::Impl { of_trait: true } => true,
    _ => false,
}matches!(cx.target, Target::Impl { of_trait: true }) {
36            let target_span = cx.target_span;
37            cx.emit_lint(
38                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
39                IncorrectDoNotRecommendLocation { target_span },
40                attr_span,
41            );
42            return None;
43        }
44
45        Some(AttributeKind::DoNotRecommend)
46    }
47}