rustc_attr_parsing/attributes/
lint_helpers.rs1use rustc_attr_ir::{ReprAttr, find_attr};
2use rustc_feature::AttributeStability;
3
4use super::prelude::*;
5use crate::diagnostics::{InvalidCBufferLength, 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 LintCBufferLengthParser;
22
23impl SingleAttributeParser for LintCBufferLengthParser {
24 const PATH: &[Symbol] = &[sym::lint, sym::c_buffer_length];
25 const ALLOWED_TARGETS: AllowedTargets<'_> =
26 AllowedTargets::AllowList(&[Allow(Target::Fn), Allow(Target::ForeignFn)]);
27 const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
word: false,
list: Some(&["pointer, length"]),
one_of: &[],
name_value_str: None,
docs: None,
}template!(List: &["pointer, length"]);
28 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::lint_c_buffer_length;
AttributeStability::Unstable {
gate_name: rustc_span::sym::lint_c_buffer_length,
notes: &[],
}
}unstable!(lint_c_buffer_length);
29
30 fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
31 let parsed = try {
32 let [pointer, length] = args.as_list()?.sub_parsers().as_array()?;
33 let pointer = pointer.meta_item_no_args()?.ident()?;
34 let length = length.meta_item_no_args()?.ident()?;
35 (pointer.normalize_to_macro_rules() != length.normalize_to_macro_rules())
36 .then_some((pointer, length))?
37 };
38 let Some((pointer, length)) = parsed else {
39 args.ignore_args();
40 cx.emit_err(InvalidCBufferLength { span: cx.attr_span });
41 return None;
42 };
43
44 Some(AttributeKind::LintCBufferLength { pointer, length })
45 }
46}
47
48pub(crate) struct LintNeverReturnsNullPtrParser;
49
50impl NoArgsAttributeParser for LintNeverReturnsNullPtrParser {
51 const PATH: &[Symbol] = &[sym::lint, sym::never_returns_null_ptr];
52 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
53 Allow(Target::Fn),
54 Allow(Target::Method(MethodKind::Inherent)),
55 Allow(Target::Method(MethodKind::Trait { body: false })),
56 Allow(Target::Method(MethodKind::Trait { body: true })),
57 Allow(Target::Method(MethodKind::TraitImpl)),
58 ]);
59 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
60
61 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintNeverReturnsNullPtr;
62}
63
64pub(crate) struct RustcPubTransparentParser;
65impl NoArgsAttributeParser for RustcPubTransparentParser {
66 const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
67 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
68 Allow(Target::Struct),
69 Allow(Target::Enum),
70 Allow(Target::Union),
71 ]);
72 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
73 const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcPubTransparent;
74
75 fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
76 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!(
78 cx.parsed_attrs,
79 Repr { reprs, .. } if reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent)
80 );
81 if !is_transparent {
82 cx.emit_err(RustcPubTransparent { span: cx.target_span, attr_span });
83 }
84 }
85}
86
87pub(crate) struct LintPassByValueParser;
88impl NoArgsAttributeParser for LintPassByValueParser {
89 const PATH: &[Symbol] = &[sym::lint, sym::pass_by_value];
90 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
91 Allow(Target::Struct),
92 Allow(Target::Enum),
93 Allow(Target::TyAlias),
94 ]);
95 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
96 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintPassByValue;
97}
98
99pub(crate) struct LintShouldNotBeCalledOnConstItemsParser;
100impl NoArgsAttributeParser for LintShouldNotBeCalledOnConstItemsParser {
101 const PATH: &[Symbol] = &[sym::lint, sym::should_not_be_called_on_const_items];
102 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
103 Allow(Target::Method(MethodKind::Inherent)),
104 Allow(Target::Method(MethodKind::TraitImpl)),
105 ]);
106 const STABILITY: AttributeStability = {
_ = rustc_feature::Features::rustc_attrs;
AttributeStability::Unstable {
gate_name: rustc_span::sym::rustc_attrs,
notes: &[],
}
}unstable!(rustc_attrs);
107 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::LintShouldNotBeCalledOnConstItems;
108}
109
110pub(crate) struct AutomaticallyDerivedParser;
111impl NoArgsAttributeParser for AutomaticallyDerivedParser {
112 const PATH: &[Symbol] = &[sym::automatically_derived];
113 const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
114 const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowListWarnRest(&[
115 Allow(Target::Impl { of_trait: true }),
116 Error(Target::Crate),
117 Error(Target::WherePredicate),
118 Error(Target::Param),
119 ]);
120 const STABILITY: AttributeStability = AttributeStability::Stable;
121 const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::AutomaticallyDerived;
122}