Skip to content

Commit 36cbcae

Browse files
committed
rustc_marker_type built in attribute created to have certain marker types not trigger dead code warnings from clippy
1 parent dd82fd2 commit 36cbcae

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
@@ -307,6 +307,7 @@ attribute_parsers!(
307307
Single<WithoutArgs<RustcLintQueryInstabilityParser>>,
308308
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,
309309
Single<WithoutArgs<RustcMainParser>>,
310+
Single<WithoutArgs<RustcMarkerTypeParser>>,
310311
Single<WithoutArgs<RustcNeverReturnsNullPtrParser>>,
311312
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
312313
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
14141414
rustc_scalable_vector, Normal, template!(List: &["count"]), WarnFollowing, EncodeCrossCrate::Yes,
14151415
"`#[rustc_scalable_vector]` defines a scalable vector type"
14161416
),
1417+
rustc_attr!(
1418+
rustc_marker_type, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes,
1419+
"the `#[rustc_marker_type]` attribute is used by the standard library to mark certain marker types \
1420+
that do not need dead code warnings"
1421+
),
14171422

14181423
// ==========================================================================
14191424
// 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
@@ -1462,6 +1462,9 @@ pub enum AttributeKind {
14621462
/// Represents `#[rustc_main]`.
14631463
RustcMain,
14641464

1465+
/// Represents `#[rustc_marker_type]`
1466+
RustcMarkerType(Symbol, Span),
1467+
14651468
/// Represents `#[rustc_mir]`.
14661469
RustcMir(ThinVec<RustcMirKind>),
14671470

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

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

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
338338
| AttributeKind::RustcLintUntrackedQueryInformation
339339
| AttributeKind::RustcMacroTransparency(_)
340340
| AttributeKind::RustcMain
341+
| AttributeKind::RustcMarkerType(..)
341342
| AttributeKind::RustcMir(_)
342343
| AttributeKind::RustcNeverReturnsNullPtr
343344
| AttributeKind::RustcNeverTypeOptions {..}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,7 @@ symbols! {
17511751
rustc_lint_untracked_query_information,
17521752
rustc_macro_transparency,
17531753
rustc_main,
1754+
rustc_marker_type,
17541755
rustc_mir,
17551756
rustc_must_implement_one_of,
17561757
rustc_never_returns_null_ptr,

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)