Skip to content

Commit 260b712

Browse files
committed
rustc_marker_type built in attribute created to have certain marker types not trigger dead code warnings from clippy
1 parent 30d0309 commit 260b712

File tree

11 files changed

+35
-11
lines changed

11 files changed

+35
-11
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
755755
),
756756
rustc_attr!(rustc_force_inline,"`#[rustc_force_inline]` forces a free function to be inlined"),
757757
rustc_attr!(rustc_scalable_vector,"`#[rustc_scalable_vector]` defines a scalable vector type"),
758+
rustc_attr!( rustc_marker_type, "the `#[rustc_marker_type]` attribute is used by the standard library to mark certain marker types \
759+
that do not need dead code warnings"
760+
),
758761

759762
// ==========================================================================
760763
// 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
@@ -1604,6 +1604,9 @@ pub enum AttributeKind {
16041604
/// Represents `#[rustc_main]`.
16051605
RustcMain,
16061606

1607+
/// Represents `[#rustc_marker_type]`
1608+
RustcMarkerType,
1609+
16071610
/// Represents `#[rustc_mir]`.
16081611
RustcMir(ThinVec<RustcMirKind>),
16091612

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
RustcNeverReturnsNullPtr => Yes,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
334334
| AttributeKind::RustcLintUntrackedQueryInformation
335335
| AttributeKind::RustcMacroTransparency(_)
336336
| AttributeKind::RustcMain
337+
| AttributeKind::RustcMarkerType
337338
| AttributeKind::RustcMir(_)
338339
| AttributeKind::RustcNeverReturnsNullPtr
339340
| AttributeKind::RustcNeverTypeOptions {..}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,7 @@ symbols! {
17471747
rustc_lint_untracked_query_information,
17481748
rustc_macro_transparency,
17491749
rustc_main,
1750+
rustc_marker_type,
17501751
rustc_mir,
17511752
rustc_must_implement_one_of,
17521753
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 & 2 deletions
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::{is_marker_type, sym};
66
use clippy_utils::visitors::{Visitable, for_each_expr};
77
use rustc_ast::LitKind;
88
use rustc_data_structures::fx::FxHashSet;
@@ -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+
// We exclude marker types (e.g. PhantomData, PhantomPinned) from emitting warnings
188+
|| is_marker_type(cx.tcx.hir_attrs(field.hir_id))
188189
{
189190
None
190191
} else {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,17 @@ impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
7070
},
7171
};
7272

73+
// These specific phantom language items do not need to trigger pub underscore field
74+
// warnings
75+
let phantom_types = [LangItem::PhantomData, LangItem::PhantomPinned];
76+
7377
for field in variant_data.fields() {
7478
// Only pertains to fields that start with an underscore, and are public.
7579
if field.ident.as_str().starts_with('_') && is_visible(field)
7680
// We ignore fields that have `#[doc(hidden)]`.
7781
&& !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)
82+
// We exclude marker types (e.g. PhantomData, PhantomPinned) from emitting warnings
83+
&& !is_marker_type(cx.tcx.hir_attrs(field.hir_id))
8084
{
8185
span_lint_hir_and_then(
8286
cx,

src/tools/clippy/clippy_utils/src/attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
9191
attrs.iter().any(AttributeExt::is_doc_hidden)
9292
}
9393

94+
pub fn is_marker_type(attrs: &[impl AttributeExt]) -> bool {
95+
attrs.iter().any(AttributeExt::name? == sym::rustc_marker_type)
96+
}
97+
9498
/// Checks whether the given ADT, or any of its fields/variants, are marked as `#[non_exhaustive]`
9599
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
96100
adt.is_variant_list_non_exhaustive()

src/tools/clippy/tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![warn(clippy::pub_underscore_fields)]
77

88
use std::marker::PhantomData;
9+
use std::marker::PhantonPinned;
910

1011
pub mod inner {
1112
use std::marker;
@@ -15,7 +16,8 @@ pub mod inner {
1516
pub _b: u8,
1617
//~^ pub_underscore_fields
1718
_c: i32,
18-
pub _mark: marker::PhantomData<u8>,
19+
pub _mark_pinned: marker::PhantomPinned,
20+
pub _mark_data: marker::PhantomData<u8>,
1921
}
2022

2123
mod inner2 {
@@ -68,7 +70,8 @@ fn main() {
6870
pub struct NamedPub {
6971
r#pub: bool,
7072
_pub: String,
71-
pub(crate) _mark: PhantomData<u8>,
73+
pub _mark_pinned: marker::PhantomPinned,
74+
pub _mark_data: marker::PhantomData<u8>,
7275
}
7376

7477
// shouldn't warn when `#[allow]` is used on field level

0 commit comments

Comments
 (0)