Skip to content

Commit 81d2036

Browse files
committed
Port #[rustc_lint_opt_ty] to parsed attribute
1 parent bbda675 commit 81d2036

File tree

8 files changed

+20
-27
lines changed

8 files changed

+20
-27
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
117117
}
118118
}
119119

120+
pub(crate) struct RustcLintOptTyParser;
121+
122+
impl<S: Stage> NoArgsAttributeParser<S> for RustcLintOptTyParser {
123+
const PATH: &[Symbol] = &[sym::rustc_lint_opt_ty];
124+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
125+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
126+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintOptTy;
127+
}
128+
120129
pub(crate) struct RustcObjectLifetimeDefaultParser;
121130

122131
impl<S: Stage> SingleAttributeParser<S> for RustcObjectLifetimeDefaultParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ use crate::attributes::prototype::CustomMirParser;
6161
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6262
use crate::attributes::rustc_internal::{
6363
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
64-
RustcLegacyConstGenericsParser, RustcMainParser, RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
64+
RustcLegacyConstGenericsParser, RustcLintOptTyParser, RustcMainParser,
65+
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser,
6566
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
6667
RustcSimdMonomorphizeLaneLimitParser,
6768
};
@@ -255,6 +256,7 @@ attribute_parsers!(
255256
Single<WithoutArgs<ProcMacroParser>>,
256257
Single<WithoutArgs<PubTransparentParser>>,
257258
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
259+
Single<WithoutArgs<RustcLintOptTyParser>>,
258260
Single<WithoutArgs<RustcMainParser>>,
259261
Single<WithoutArgs<RustcNeverReturnsNullPointerParser>>,
260262
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,9 @@ pub enum AttributeKind {
875875
/// Represents `#[rustc_legacy_const_generics]`
876876
RustcLegacyConstGenerics { fn_indexes: ThinVec<(usize, Span)>, attr_span: Span },
877877

878+
/// Represents `#[rustc_lint_opt_ty]`
879+
RustcLintOptTy,
880+
878881
/// Represents `#[rustc_main]`.
879882
RustcMain,
880883

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl AttributeKind {
9494
RustcLayoutScalarValidRangeEnd(..) => Yes,
9595
RustcLayoutScalarValidRangeStart(..) => Yes,
9696
RustcLegacyConstGenerics { .. } => Yes,
97+
RustcLintOptTy => Yes,
9798
RustcMain => No,
9899
RustcNeverReturnsNullPointer => Yes,
99100
RustcNoImplicitAutorefs => Yes,

compiler/rustc_lint/src/internal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
22
//! Clippy.
33
4+
use rustc_hir::attrs::AttributeKind;
45
use rustc_hir::def::Res;
56
use rustc_hir::def_id::DefId;
6-
use rustc_hir::{Expr, ExprKind, HirId};
7+
use rustc_hir::{Expr, ExprKind, HirId, find_attr};
78
use rustc_middle::ty::{self, GenericArgsRef, PredicatePolarity, Ty};
89
use rustc_session::{declare_lint_pass, declare_tool_lint};
910
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -658,7 +659,7 @@ impl LateLintPass<'_> for BadOptAccess {
658659
let Some(adt_def) = cx.typeck_results().expr_ty(base).ty_adt_def() else { return };
659660
// Skip types without `#[rustc_lint_opt_ty]` - only so that the rest of the lint can be
660661
// avoided.
661-
if !cx.tcx.has_attr(adt_def.did(), sym::rustc_lint_opt_ty) {
662+
if !find_attr!(cx.tcx.get_all_attrs(adt_def.did()), AttributeKind::RustcLintOptTy) {
662663
return;
663664
}
664665

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,6 @@ passes_rustc_lint_opt_deny_field_access =
477477
`#[rustc_lint_opt_deny_field_access]` should be applied to a field
478478
.label = not a field
479479
480-
passes_rustc_lint_opt_ty =
481-
`#[rustc_lint_opt_ty]` should be applied to a struct
482-
.label = not a struct
483-
484480
passes_rustc_pub_transparent =
485481
attribute should be applied to `#[repr(transparent)]` types
486482
.label = not a `#[repr(transparent)]` type

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
258258
| AttributeKind::RustcNoImplicitAutorefs
259259
| AttributeKind::RustcLayoutScalarValidRangeStart(..)
260260
| AttributeKind::RustcLayoutScalarValidRangeEnd(..)
261+
| AttributeKind::RustcLintOptTy
261262
| AttributeKind::RustcNeverReturnsNullPointer
262263
| AttributeKind::RustcScalableVector { .. }
263264
| AttributeKind::RustcSimdMonomorphizeLaneLimit(..)
@@ -315,7 +316,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
315316
[sym::rustc_lint_diagnostics, ..] => {
316317
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
317318
}
318-
[sym::rustc_lint_opt_ty, ..] => self.check_rustc_lint_opt_ty(attr, span, target),
319319
[sym::rustc_lint_opt_deny_field_access, ..] => {
320320
self.check_rustc_lint_opt_deny_field_access(attr, span, target)
321321
}
@@ -1253,16 +1253,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12531253
}
12541254
}
12551255

1256-
/// Checks that the `#[rustc_lint_opt_ty]` attribute is only applied to a struct.
1257-
fn check_rustc_lint_opt_ty(&self, attr: &Attribute, span: Span, target: Target) {
1258-
match target {
1259-
Target::Struct => {}
1260-
_ => {
1261-
self.dcx().emit_err(errors::RustcLintOptTy { attr_span: attr.span(), span });
1262-
}
1263-
}
1264-
}
1265-
12661256
/// Checks that the `#[rustc_lint_opt_deny_field_access]` attribute is only applied to a field.
12671257
fn check_rustc_lint_opt_deny_field_access(&self, attr: &Attribute, span: Span, target: Target) {
12681258
match target {

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,6 @@ pub(crate) struct UnusedMultiple {
412412
pub name: Symbol,
413413
}
414414

415-
#[derive(Diagnostic)]
416-
#[diag(passes_rustc_lint_opt_ty)]
417-
pub(crate) struct RustcLintOptTy {
418-
#[primary_span]
419-
pub attr_span: Span,
420-
#[label]
421-
pub span: Span,
422-
}
423-
424415
#[derive(Diagnostic)]
425416
#[diag(passes_rustc_lint_opt_deny_field_access)]
426417
pub(crate) struct RustcLintOptDenyFieldAccess {

0 commit comments

Comments
 (0)