Skip to main content

rustc_attr_parsing/attributes/
lint_helpers.rs

1use rustc_attr_ir::{ReprAttr, find_attr};
2use rustc_feature::AttributeStability;
3
4use super::prelude::*;
5use crate::diagnostics::RustcPubTransparent;
6
7pub(crate) struct LintAsPtrParser;
8impl NoArgsAttributeParser for LintAsPtrParser {
9    const PATH: &[Symbol] = &[sym::lint, sym::as_ptr];
10    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
11        Allow(Target::Fn),
12        Allow(Target::Method(MethodKind::Inherent)),
13        Allow(Target::Method(MethodKind::Trait { body: false })),
14        Allow(Target::Method(MethodKind::Trait { body: true })),
15        Allow(Target::Method(MethodKind::TraitImpl)),
16    ]);
17    const STABILITY: AttributeStability = {
    _ = rustc_feature::Features::rustc_attrs;
    AttributeStability::Unstable {
        gate_name: rustc_span::sym::rustc_attrs,
        notes: &[],
    }
}unstable!(rustc_attrs);
18    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintAsPtr;
19}
20
21pub(crate) struct LintNeverReturnsNullPtrParser;
22
23impl NoArgsAttributeParser for LintNeverReturnsNullPtrParser {
24    const PATH: &[Symbol] = &[sym::lint, sym::never_returns_null_ptr];
25    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
26        Allow(Target::Fn),
27        Allow(Target::Method(MethodKind::Inherent)),
28        Allow(Target::Method(MethodKind::Trait { body: false })),
29        Allow(Target::Method(MethodKind::Trait { body: true })),
30        Allow(Target::Method(MethodKind::TraitImpl)),
31    ]);
32    const STABILITY: AttributeStability = {
    _ = rustc_feature::Features::rustc_attrs;
    AttributeStability::Unstable {
        gate_name: rustc_span::sym::rustc_attrs,
        notes: &[],
    }
}unstable!(rustc_attrs);
33
34    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintNeverReturnsNullPtr;
35}
36
37pub(crate) struct RustcPubTransparentParser;
38impl NoArgsAttributeParser for RustcPubTransparentParser {
39    const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
40    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
41        Allow(Target::Struct),
42        Allow(Target::Enum),
43        Allow(Target::Union),
44    ]);
45    const STABILITY: AttributeStability = {
    _ = rustc_feature::Features::rustc_attrs;
    AttributeStability::Unstable {
        gate_name: rustc_span::sym::rustc_attrs,
        notes: &[],
    }
}unstable!(rustc_attrs);
46    const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcPubTransparent;
47
48    fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
49        // `#[rustc_pub_transparent]` may only be applied to `#[repr(transparent)]` types.
50        let is_transparent = {
    {
            'done:
                {
                for i in cx.parsed_attrs {
                    #[allow(unused_imports)]
                    use ::rustc_attr_ir::AttributeKind::*;
                    let i: &::rustc_attr_ir::Attribute = i;
                    match i {
                        ::rustc_attr_ir::Attribute::Parsed(Repr { reprs, .. }) if
                            reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent)
                            => {
                            break 'done Some(());
                        }
                        ::rustc_attr_ir::Attribute::Tool(..) |
                            ::rustc_attr_ir::Attribute::DeriveHelper(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(
51            cx.parsed_attrs,
52            Repr { reprs, .. } if reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent)
53        );
54        if !is_transparent {
55            cx.emit_err(RustcPubTransparent { span: cx.target_span, attr_span });
56        }
57    }
58}
59
60pub(crate) struct RustcPassByValueParser;
61impl NoArgsAttributeParser for RustcPassByValueParser {
62    const PATH: &[Symbol] = &[sym::rustc_pass_by_value];
63    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
64        Allow(Target::Struct),
65        Allow(Target::Enum),
66        Allow(Target::TyAlias),
67    ]);
68    const STABILITY: AttributeStability = {
    _ = rustc_feature::Features::rustc_attrs;
    AttributeStability::Unstable {
        gate_name: rustc_span::sym::rustc_attrs,
        notes: &[],
    }
}unstable!(rustc_attrs);
69    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcPassByValue;
70}
71
72pub(crate) struct LintShouldNotBeCalledOnConstItemsParser;
73impl NoArgsAttributeParser for LintShouldNotBeCalledOnConstItemsParser {
74    const PATH: &[Symbol] = &[sym::lint, sym::should_not_be_called_on_const_items];
75    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
76        Allow(Target::Method(MethodKind::Inherent)),
77        Allow(Target::Method(MethodKind::TraitImpl)),
78    ]);
79    const STABILITY: AttributeStability = {
    _ = rustc_feature::Features::rustc_attrs;
    AttributeStability::Unstable {
        gate_name: rustc_span::sym::rustc_attrs,
        notes: &[],
    }
}unstable!(rustc_attrs);
80    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintShouldNotBeCalledOnConstItems;
81}
82
83pub(crate) struct AutomaticallyDerivedParser;
84impl NoArgsAttributeParser for AutomaticallyDerivedParser {
85    const PATH: &[Symbol] = &[sym::automatically_derived];
86    const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
87    const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
88        Allow(Target::Impl { of_trait: true }),
89        Error(Target::Crate),
90        Error(Target::WherePredicate),
91        Error(Target::Param),
92    ]);
93    const STABILITY: AttributeStability = AttributeStability::Stable;
94    const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::AutomaticallyDerived;
95}