Skip to content

Commit bc8b9a1

Browse files
committed
rustc_marker_type built in attribute created to have certain marker types not trigger dead code warnings from clippy
1 parent 17584a1 commit bc8b9a1

File tree

11 files changed

+40
-7
lines changed

11 files changed

+40
-7
lines changed

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcAsPtrParser {
1414
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcAsPtr;
1515
}
1616

17+
pub(crate) struct RustcNoDeadCodeWarningParser;
18+
impl<S: Stage> NoArgsAttributeParser<S> for RustcNoDeadCodeWarningParser {
19+
const PATH: &[Symbol] = &[sym::rustc_no_dead_code_warning];
20+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
21+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
22+
Allow(Target::Struct),
23+
Allow(Target::Enum),
24+
Allow(Target::TyAlias),
25+
]);
26+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoDeadCodeWarning;
27+
}
28+
1729
pub(crate) struct RustcPubTransparentParser;
1830
impl<S: Stage> NoArgsAttributeParser<S> for RustcPubTransparentParser {
1931
const PATH: &[Symbol] = &[sym::rustc_pub_transparent];

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ attribute_parsers!(
312312
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,
313313
Single<WithoutArgs<RustcMainParser>>,
314314
Single<WithoutArgs<RustcNeverReturnsNullPtrParser>>,
315+
Single<WithoutArgs<RustcNoDeadCodeWarningParser>>,
315316
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
316317
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
317318
Single<WithoutArgs<RustcNoMirInlineParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
14241424
rustc_must_match_exhaustively, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
14251425
"enums with `#[rustc_must_match_exhaustively]` must be matched on with a match block that mentions all variants explicitly"
14261426
),
1427+
rustc_attr!(
1428+
rustc_no_dead_code_warning, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes,
1429+
"`#[rustc_no_dead_code_warning]` attribute is used to mark certain types that do not need \
1430+
to propagate dead code warnings as field members in other types"
1431+
),
14271432

14281433
// ==========================================================================
14291434
// Internal attributes, Testing:

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,9 @@ pub enum AttributeKind {
15021502
diverging_block_default: Option<DivergingBlockBehavior>,
15031503
},
15041504

1505+
/// Represents `#[rustc_no_dead_code_warning]`
1506+
RustcNoDeadCodeWarning,
1507+
15051508
/// Represents `#[rustc_no_implicit_autorefs]`
15061509
RustcNoImplicitAutorefs,
15071510

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl AttributeKind {
161161
RustcMustMatchExhaustively(..) => Yes,
162162
RustcNeverReturnsNullPtr => Yes,
163163
RustcNeverTypeOptions { .. } => No,
164+
RustcNoDeadCodeWarning => Yes,
164165
RustcNoImplicitAutorefs => Yes,
165166
RustcNoImplicitBounds => No,
166167
RustcNoMirInline => Yes,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
351351
| AttributeKind::RustcMustMatchExhaustively(..)
352352
| AttributeKind::RustcNeverReturnsNullPtr
353353
| AttributeKind::RustcNeverTypeOptions {..}
354+
| AttributeKind::RustcNoDeadCodeWarning
354355
| AttributeKind::RustcNoImplicitAutorefs
355356
| AttributeKind::RustcNoImplicitBounds
356357
| AttributeKind::RustcNoMirInline

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ symbols! {
17651765
rustc_must_match_exhaustively,
17661766
rustc_never_returns_null_ptr,
17671767
rustc_never_type_options,
1768+
rustc_no_dead_code_warning,
17681769
rustc_no_implicit_autorefs,
17691770
rustc_no_implicit_bounds,
17701771
rustc_no_mir_inline,

library/core/src/marker.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ impl<T: PointeeSized> !Sync for *mut T {}
808808
/// [drop check]: Drop#drop-check
809809
#[lang = "phantom_data"]
810810
#[stable(feature = "rust1", since = "1.0.0")]
811+
#[rustc_no_dead_code_warning]
811812
pub struct PhantomData<T: PointeeSized>;
812813

813814
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1021,6 +1022,7 @@ pub auto trait Unpin {}
10211022
// will likely eventually be deprecated, and all new code should be using `UnsafePinned` instead.
10221023
#[stable(feature = "pin", since = "1.33.0")]
10231024
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
1025+
#[rustc_no_dead_code_warning]
10241026
pub struct PhantomPinned;
10251027

10261028
#[stable(feature = "pin", since = "1.33.0")]

src/tools/clippy/clippy_lints/src/missing_fields_in_debug.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use std::ops::ControlFlow;
22

33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::res::{MaybeDef, MaybeResPath};
5-
use clippy_utils::sym;
5+
use clippy_utils::{is_rustc_no_dead_code_warning_attr, sym};
66
use clippy_utils::visitors::{Visitable, for_each_expr};
77
use rustc_ast::LitKind;
88
use rustc_data_structures::fx::FxHashSet;
99
use rustc_hir::def::{DefKind, Res};
10-
use rustc_hir::{Block, Expr, ExprKind, Impl, Item, ItemKind, LangItem, Node, QPath, TyKind, VariantData};
10+
use rustc_hir::{Block, Expr, ExprKind, Impl, Item, ItemKind, Node, QPath, TyKind, VariantData};
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::ty::{Ty, TypeckResults};
1313
use rustc_session::declare_lint_pass;
@@ -184,7 +184,8 @@ fn check_struct<'tcx>(
184184
.iter()
185185
.filter_map(|field| {
186186
if field_accesses.contains(&field.ident.name)
187-
|| field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
187+
|| is_rustc_no_dead_code_warning_attr(cx.tcx, field.ty.basic_res())
188+
// We exclude certain types (e.g. PhantomData, PhantomPinned) marked with
188189
{
189190
None
190191
} else {

src/tools/clippy/clippy_lints/src/pub_underscore_fields.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_config::Conf;
22
use clippy_config::types::PubUnderscoreFieldsBehaviour;
3-
use clippy_utils::attrs::is_doc_hidden;
3+
use clippy_utils::attrs::{is_doc_hidden, is_rustc_no_dead_code_warning_attr};
44
use clippy_utils::diagnostics::span_lint_hir_and_then;
5-
use clippy_utils::res::{MaybeDef, MaybeResPath};
6-
use rustc_hir::{FieldDef, Item, ItemKind, LangItem};
5+
use clippy_utils::res::{MaybeResPath};
6+
use rustc_hir::{FieldDef, Item, ItemKind};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::impl_lint_pass;
99

@@ -76,7 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
7676
// We ignore fields that have `#[doc(hidden)]`.
7777
&& !is_doc_hidden(cx.tcx.hir_attrs(field.hir_id))
7878
// We ignore fields that are `PhantomData`.
79-
&& !field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
79+
&& !is_rustc_no_dead_code_warning_attr(cx.tcx, field.ty.basic_res())
8080
{
8181
span_lint_hir_and_then(
8282
cx,

0 commit comments

Comments
 (0)