Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcAsPtrParser {
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcAsPtr;
}

pub(crate) struct RustcNoDeadCodeWarningParser;
impl<S: Stage> NoArgsAttributeParser<S> for RustcNoDeadCodeWarningParser {
const PATH: &[Symbol] = &[sym::rustc_no_dead_code_warning];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
Allow(Target::Struct),
Allow(Target::Enum),
Copy link
Copy Markdown
Contributor Author

@asder8215 asder8215 Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

Added that the attribute can be applied to enums and type aliases. I wasn't sure how to go about testing it in the rustc's tests/ui; it's not giving me warning when I do something similar in clippy_lints like:

struct S {
    unused_field: i32,
}

impl fmt::Debug for S5 {
    //~ WARN unused field
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("S")
            .finish()
    }
}

Is this something I would have to test for only on the clippy side of things?

Allow(Target::TyAlias),
]);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNoDeadCodeWarning;
}

pub(crate) struct RustcPubTransparentParser;
impl<S: Stage> NoArgsAttributeParser<S> for RustcPubTransparentParser {
const PATH: &[Symbol] = &[sym::rustc_pub_transparent];
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ attribute_parsers!(
Single<WithoutArgs<RustcLintUntrackedQueryInformationParser>>,
Single<WithoutArgs<RustcMainParser>>,
Single<WithoutArgs<RustcNeverReturnsNullPtrParser>>,
Single<WithoutArgs<RustcNoDeadCodeWarningParser>>,
Single<WithoutArgs<RustcNoImplicitAutorefsParser>>,
Single<WithoutArgs<RustcNoImplicitBoundsParser>>,
Single<WithoutArgs<RustcNoMirInlineParser>>,
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_must_match_exhaustively,
"enums with `#[rustc_must_match_exhaustively]` must be matched on with a match block that mentions all variants explicitly"
),
rustc_attr!(
rustc_no_dead_code_warning,
"`#[rustc_no_dead_code_warning]` attribute is used to mark certain types that do not need \
to propagate dead code warnings as field members in other types"
),

// ==========================================================================
// Internal attributes, Testing:
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,9 @@ pub enum AttributeKind {
diverging_block_default: Option<DivergingBlockBehavior>,
},

/// Represents `#[rustc_no_dead_code_warning]`
RustcNoDeadCodeWarning,

/// Represents `#[rustc_no_implicit_autorefs]`
RustcNoImplicitAutorefs,

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl AttributeKind {
RustcMustMatchExhaustively(..) => Yes,
RustcNeverReturnsNullPtr => Yes,
RustcNeverTypeOptions { .. } => No,
RustcNoDeadCodeWarning => Yes,
RustcNoImplicitAutorefs => Yes,
RustcNoImplicitBounds => No,
RustcNoMirInline => Yes,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
| AttributeKind::RustcMustMatchExhaustively(..)
| AttributeKind::RustcNeverReturnsNullPtr
| AttributeKind::RustcNeverTypeOptions {..}
| AttributeKind::RustcNoDeadCodeWarning
| AttributeKind::RustcNoImplicitAutorefs
| AttributeKind::RustcNoImplicitBounds
| AttributeKind::RustcNoMirInline
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,7 @@ symbols! {
rustc_must_match_exhaustively,
rustc_never_returns_null_ptr,
rustc_never_type_options,
rustc_no_dead_code_warning,
rustc_no_implicit_autorefs,
rustc_no_implicit_bounds,
rustc_no_mir_inline,
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ impl<T: PointeeSized> !Sync for *mut T {}
/// [drop check]: Drop#drop-check
#[lang = "phantom_data"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_no_dead_code_warning]
pub struct PhantomData<T: PointeeSized>;

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

#[stable(feature = "pin", since = "1.33.0")]
Expand Down
7 changes: 4 additions & 3 deletions src/tools/clippy/clippy_lints/src/missing_fields_in_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::ops::ControlFlow;

use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::res::{MaybeDef, MaybeResPath};
use clippy_utils::sym;
use clippy_utils::{is_rustc_no_dead_code_warning_attr, sym};
use clippy_utils::visitors::{Visitable, for_each_expr};
use rustc_ast::LitKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Block, Expr, ExprKind, Impl, Item, ItemKind, LangItem, Node, QPath, TyKind, VariantData};
use rustc_hir::{Block, Expr, ExprKind, Impl, Item, ItemKind, Node, QPath, TyKind, VariantData};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{Ty, TypeckResults};
use rustc_session::declare_lint_pass;
Expand Down Expand Up @@ -184,7 +184,8 @@ fn check_struct<'tcx>(
.iter()
.filter_map(|field| {
if field_accesses.contains(&field.ident.name)
|| field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
|| is_rustc_no_dead_code_warning_attr(cx.tcx, field.ty.basic_res())
// We exclude certain types (e.g. PhantomData, PhantomPinned) marked with
{
None
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/tools/clippy/clippy_lints/src/pub_underscore_fields.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clippy_config::Conf;
use clippy_config::types::PubUnderscoreFieldsBehaviour;
use clippy_utils::attrs::is_doc_hidden;
use clippy_utils::attrs::{is_doc_hidden, is_rustc_no_dead_code_warning_attr};
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::res::{MaybeDef, MaybeResPath};
use rustc_hir::{FieldDef, Item, ItemKind, LangItem};
use clippy_utils::res::{MaybeResPath};
use rustc_hir::{FieldDef, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;

Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
// We ignore fields that have `#[doc(hidden)]`.
&& !is_doc_hidden(cx.tcx.hir_attrs(field.hir_id))
// We ignore fields that are `PhantomData`.
&& !field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
&& !is_rustc_no_dead_code_warning_attr(cx.tcx, field.ty.basic_res())
{
span_lint_hir_and_then(
cx,
Expand Down
6 changes: 6 additions & 0 deletions src/tools/clippy/clippy_utils/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::source::SpanRangeExt;
use crate::{sym, tokenize_with_text};
use rustc_ast::attr::AttributeExt;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::find_attr;
use rustc_lexer::TokenKind;
use rustc_lint::LateContext;
Expand Down Expand Up @@ -91,6 +92,11 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
attrs.iter().any(AttributeExt::is_doc_hidden)
}

/// Checks whether the original type is marked as `#[rustc_no_dead_code_warning]`
pub fn is_rustc_no_dead_code_warning_attr(tcx: TyCtxt<'_>, res: &Res) -> bool {
res.opt_def_id().map(|def_id| find_attr!(tcx, def_id, RustcNoDeadCodeWarning)).unwrap_or(false)
}

/// Checks whether the given ADT, or any of its fields/variants, are marked as `#[non_exhaustive]`
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
adt.is_variant_list_non_exhaustive()
Expand Down
Loading