Skip to content

Commit 069be2d

Browse files
committed
Port #[rustc_lint_opt_ty] to parsed attribute
1 parent a0c7262 commit 069be2d

File tree

8 files changed

+20
-28
lines changed

8 files changed

+20
-28
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
@@ -101,6 +101,15 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
101101
}
102102
}
103103

104+
pub(crate) struct RustcLintOptTyParser;
105+
106+
impl<S: Stage> NoArgsAttributeParser<S> for RustcLintOptTyParser {
107+
const PATH: &[Symbol] = &[sym::rustc_lint_opt_ty];
108+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
109+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
110+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcLintOptTy;
111+
}
112+
104113
pub(crate) struct RustcObjectLifetimeDefaultParser;
105114

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

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +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, RustcNoImplicitAutorefsParser,
65-
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
64+
RustcLegacyConstGenericsParser, RustcLintOptTyParser, RustcMainParser,
65+
RustcNoImplicitAutorefsParser, RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
6666
RustcSimdMonomorphizeLaneLimitParser,
6767
};
6868
use crate::attributes::semantics::MayDangleParser;
@@ -255,6 +255,7 @@ attribute_parsers!(
255255
Single<WithoutArgs<ProcMacroParser>>,
256256
Single<WithoutArgs<PubTransparentParser>>,
257257
Single<WithoutArgs<RustcCoherenceIsCoreParser>>,
258+
Single<WithoutArgs<RustcLintOptTyParser>>,
258259
Single<WithoutArgs<RustcMainParser>>,
259260
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
260261
Single<WithoutArgs<RustcPassIndirectlyInNonRusticAbisParser>>,

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
RustcNoImplicitAutorefs => Yes,
99100
RustcObjectLifetimeDefault => No,

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::RustcScalableVector { .. }
262263
| AttributeKind::RustcSimdMonomorphizeLaneLimit(..)
263264
| AttributeKind::RustcShouldNotBeCalledOnConstItems(..)
@@ -317,7 +318,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
317318
[sym::rustc_lint_diagnostics, ..] => {
318319
self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
319320
}
320-
[sym::rustc_lint_opt_ty, ..] => self.check_rustc_lint_opt_ty(attr, span, target),
321321
[sym::rustc_lint_opt_deny_field_access, ..] => {
322322
self.check_rustc_lint_opt_deny_field_access(attr, span, target)
323323
}
@@ -1255,16 +1255,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12551255
}
12561256
}
12571257

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