Skip to content

Commit 3e347eb

Browse files
committed
clippy built in attribute created to have certain marker types not trigger dead code warnings as field members from other structs
1 parent f29256d commit 3e347eb

5 files changed

Lines changed: 25 additions & 9 deletions

File tree

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+
#[clippy::no_dead_code_warning]
811812
pub struct PhantomData<T: PointeeSized>;
812813

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

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

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ 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_clippy_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};
11-
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_hir::{Block, Expr, ExprKind, Impl, Item, ItemKind, Node, QPath, TyKind, VariantData};
11+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_middle::ty::{Ty, TypeckResults};
1313
use rustc_session::declare_lint_pass;
1414
use rustc_span::{Span, Symbol};
@@ -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 certain types (e.g. PhantomData, PhantomPinned) marked with
188+
|| is_clippy_no_dead_code_warning_attr(cx.sess(), cx.tcx, field.ty.basic_res())
188189
{
189190
None
190191
} 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,10 +1,10 @@
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_clippy_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};
7-
use rustc_lint::{LateContext, LateLintPass};
5+
use clippy_utils::res::{MaybeResPath};
6+
use rustc_hir::{FieldDef, Item, ItemKind};
7+
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_session::impl_lint_pass;
99

1010
declare_clippy_lint! {
@@ -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_clippy_no_dead_code_warning_attr(cx.sess(), cx.tcx, field.ty.basic_res())
8080
{
8181
span_lint_hir_and_then(
8282
cx,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::source::SpanRangeExt;
44
use crate::{sym, tokenize_with_text};
55
use rustc_ast::attr::AttributeExt;
66
use rustc_errors::Applicability;
7+
use rustc_hir::def::Res;
78
use rustc_hir::find_attr;
89
use rustc_lexer::TokenKind;
910
use rustc_lint::LateContext;
@@ -35,6 +36,7 @@ pub fn get_builtin_attr<'a, A: AttributeExt + 'a>(
3536
// The following attributes are for the 3rd party crate authors.
3637
// See book/src/attribs.md
3738
| sym::has_significant_drop
39+
| sym::no_dead_code_warning
3840
| sym::format_args => None,
3941
_ => {
4042
sess.dcx().span_err(path_span, "usage of unknown attribute");
@@ -91,6 +93,16 @@ pub fn is_doc_hidden(attrs: &[impl AttributeExt]) -> bool {
9193
attrs.iter().any(AttributeExt::is_doc_hidden)
9294
}
9395

96+
/// Checks whether the original type is marked as `#[rustc_no_dead_code_warning]`
97+
pub fn is_clippy_no_dead_code_warning_attr<'a>(sess: &'a Session, tcx: TyCtxt<'_>, res: &Res) -> bool {
98+
res.opt_def_id().map(|def_id| get_builtin_attr(
99+
sess,
100+
#[allow(deprecated)]
101+
tcx.get_all_attrs(def_id),
102+
sym::no_dead_code_warning,
103+
).next().is_some()).unwrap_or(false)
104+
}
105+
94106
/// Checks whether the given ADT, or any of its fields/variants, are marked as `#[non_exhaustive]`
95107
pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
96108
adt.is_variant_list_non_exhaustive()

src/tools/clippy/clippy_utils/src/sym.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ generate! {
439439
next_if_eq,
440440
next_multiple_of,
441441
next_tuple,
442+
no_dead_code_warning,
442443
nth,
443444
ok,
444445
ok_or,

0 commit comments

Comments
 (0)