Skip to content

Commit 9f0d57d

Browse files
committed
rustc_marker_type built in attribute created to have certain marker types not trigger dead code warnings from clippy
1 parent b6100cc commit 9f0d57d

File tree

15 files changed

+61
-25
lines changed

15 files changed

+61
-25
lines changed

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

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

17+
pub(crate) struct RustcMarkerTypeParser;
18+
impl<S: Stage> NoArgsAttributeParser<S> for RustcMarkerTypeParser {
19+
const PATH: &[Symbol] = &[sym::rustc_marker_type];
20+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
21+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
22+
const CREATE: fn(Span) -> AttributeKind =
23+
|span| AttributeKind::RustcMarkerType(sym::rustc_marker_type, span);
24+
}
25+
1726
pub(crate) struct RustcPubTransparentParser;
1827
impl<S: Stage> NoArgsAttributeParser<S> for RustcPubTransparentParser {
1928
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
@@ -308,6 +308,7 @@ attribute_parsers!(
308308
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
309309
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,
310310
Single<WithoutArgs<RustcMainParser>>,
311+
Single<WithoutArgs<RustcMarkerTypeParser>>,
311312
Single<WithoutArgs<RustcNeverReturnsNullPtrParser>>,
312313
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
313314
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
14181418
rustc_must_match_exhaustively, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
14191419
"enums with `#[rustc_must_match_exhaustively]` must be matched on with a match block that mentions all variants explicitly"
14201420
),
1421+
rustc_attr!(
1422+
rustc_marker_type, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes,
1423+
"the `#[rustc_marker_type]` attribute is used by the standard library to mark certain marker types \
1424+
that do not need dead code warnings"
1425+
),
14211426

14221427
// ==========================================================================
14231428
// 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
@@ -1465,6 +1465,9 @@ pub enum AttributeKind {
14651465
/// Represents `#[rustc_main]`.
14661466
RustcMain,
14671467

1468+
/// Represents `#[rustc_marker_type]`
1469+
RustcMarkerType(Symbol, Span),
1470+
14681471
/// Represents `#[rustc_mir]`.
14691472
RustcMir(ThinVec<RustcMirKind>),
14701473

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl AttributeKind {
155155
RustcLintUntrackedQueryInformation => Yes,
156156
RustcMacroTransparency(..) => Yes,
157157
RustcMain => No,
158+
RustcMarkerType(..) => Yes,
158159
RustcMir(..) => Yes,
159160
RustcMustImplementOneOf { .. } => No,
160161
RustcMustMatchExhaustively(..) => Yes,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
339339
| AttributeKind::RustcLintUntrackedQueryInformation
340340
| AttributeKind::RustcMacroTransparency(_)
341341
| AttributeKind::RustcMain
342+
| AttributeKind::RustcMarkerType(..)
342343
| AttributeKind::RustcMir(_)
343344
| AttributeKind::RustcMustMatchExhaustively(..)
344345
| AttributeKind::RustcNeverReturnsNullPtr

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,7 @@ symbols! {
17531753
rustc_lint_untracked_query_information,
17541754
rustc_macro_transparency,
17551755
rustc_main,
1756+
rustc_marker_type,
17561757
rustc_mir,
17571758
rustc_must_implement_one_of,
17581759
rustc_must_match_exhaustively,

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_marker_type]
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_marker_type]
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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::{has_rustc_marker_type_attr, sym};
66
use clippy_utils::visitors::{Visitable, for_each_expr};
77
use rustc_ast::LitKind;
88
use rustc_data_structures::fx::FxHashSet;
@@ -185,6 +185,8 @@ fn check_struct<'tcx>(
185185
.filter_map(|field| {
186186
if field_accesses.contains(&field.ident.name)
187187
|| field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
188+
// We exclude marker types (e.g. PhantomData, PhantomPinned) from emitting warnings
189+
|| has_rustc_marker_type_attr(cx.tcx, field.ty.basic_res())
188190
{
189191
None
190192
} else {

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

Lines changed: 5 additions & 5 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, has_rustc_marker_type_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

@@ -75,8 +75,8 @@ impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
7575
if field.ident.as_str().starts_with('_') && is_visible(field)
7676
// We ignore fields that have `#[doc(hidden)]`.
7777
&& !is_doc_hidden(cx.tcx.hir_attrs(field.hir_id))
78-
// We ignore fields that are `PhantomData`.
79-
&& !field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
78+
// We exclude marker types (e.g. PhantomData, PhantomPinned) from emitting warnings.
79+
&& !has_rustc_marker_type_attr(cx.tcx, field.ty.basic_res())
8080
{
8181
span_lint_hir_and_then(
8282
cx,

0 commit comments

Comments
 (0)