Skip to content

Commit 1a9ff0c

Browse files
authored
Rollup merge of rust-lang#157782 - asder8215:phantom_pinned_dead_code, r=BoxyUwU,samueltardieu
Added `PhantomPinned` diagnostic item and prevented dead field warning on `PhantomPinned` This PR closes rust-lang#154888. See rust-lang#154978 and in [clippy#17056](rust-lang/rust-clippy#17056) for prior history on working on this issue. From discussing with clippy team, we thought it'd be appropriate to use a diagnostic item attribute to mark `PhantomPinned` and prevent dead field warnings from being emitted in `missing_fields_in_debug.rs` and `pub_underscore_fields.rs`. If you want me to do the clippy changes separately within the clippy repository, I can do that.
2 parents 5f662cc + 372057a commit 1a9ff0c

6 files changed

Lines changed: 21 additions & 13 deletions

File tree

clippy_lints/src/missing_fields_in_debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ 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+
|| field.ty.basic_res().is_diag_item(cx, sym::PhantomPinned)
188189
{
189190
None
190191
} else {

clippy_lints/src/pub_underscore_fields.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_config::types::PubUnderscoreFieldsBehaviour;
33
use clippy_utils::attrs::is_doc_hidden;
44
use clippy_utils::diagnostics::span_lint_hir_and_then;
55
use clippy_utils::res::{MaybeDef, MaybeResPath};
6+
use clippy_utils::sym::PhantomPinned;
67
use rustc_hir::{FieldDef, Item, ItemKind, LangItem};
78
use rustc_lint::{LateContext, LateLintPass};
89
use rustc_session::impl_lint_pass;
@@ -75,8 +76,9 @@ impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields {
7576
if field.ident.as_str().starts_with('_') && is_visible(field)
7677
// We ignore fields that have `#[doc(hidden)]`.
7778
&& !is_doc_hidden(cx.tcx.hir_attrs(field.hir_id))
78-
// We ignore fields that are `PhantomData`.
79+
// We ignore fields that are `PhantomData` and `PhantomPinned`.
7980
&& !field.ty.basic_res().is_lang_item(cx, LangItem::PhantomData)
81+
&& !field.ty.basic_res().is_diag_item(cx, PhantomPinned)
8082
{
8183
span_lint_hir_and_then(
8284
cx,

clippy_utils/src/sym.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ generate! {
100100
Path,
101101
PathBuf,
102102
PathLookup,
103+
PhantomPinned,
103104
RangeBounds,
104105
RefCellRef,
105106
RefCellRefMut,

tests/ui-toml/pub_underscore_fields/pub_underscore_fields.all_pub_fields.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,47 @@ LL | pub _b: u8,
99
= help: to override `-D warnings` add `#[allow(clippy::pub_underscore_fields)]`
1010

1111
error: field marked as public but also inferred as unused because it's prefixed with `_`
12-
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:23:13
12+
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:24:13
1313
|
1414
LL | pub(in crate::inner) _f: Option<()>,
1515
| ^^^^^^^^^^^^^^^^^^^^^^^
1616
|
1717
= help: consider removing the underscore, or making the field private
1818

1919
error: field marked as public but also inferred as unused because it's prefixed with `_`
20-
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:28:13
20+
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:29:13
2121
|
2222
LL | pub _g: String,
2323
| ^^^^^^
2424
|
2525
= help: consider removing the underscore, or making the field private
2626

2727
error: field marked as public but also inferred as unused because it's prefixed with `_`
28-
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:36:9
28+
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:37:9
2929
|
3030
LL | pub _a: usize,
3131
| ^^^^^^
3232
|
3333
= help: consider removing the underscore, or making the field private
3434

3535
error: field marked as public but also inferred as unused because it's prefixed with `_`
36-
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:44:9
36+
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:45:9
3737
|
3838
LL | pub _c: i64,
3939
| ^^^^^^
4040
|
4141
= help: consider removing the underscore, or making the field private
4242

4343
error: field marked as public but also inferred as unused because it's prefixed with `_`
44-
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:48:9
44+
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:49:9
4545
|
4646
LL | pub _e: Option<u8>,
4747
| ^^^^^^
4848
|
4949
= help: consider removing the underscore, or making the field private
5050

5151
error: field marked as public but also inferred as unused because it's prefixed with `_`
52-
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:62:9
52+
--> tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs:63:9
5353
|
5454
LL | pub(crate) _b: Option<String>,
5555
| ^^^^^^^^^^^^^

tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#![warn(clippy::pub_underscore_fields)]
66

7-
use std::marker::PhantomData;
7+
use std::marker::{PhantomData, PhantomPinned};
88

99
pub mod inner {
1010
use std::marker;
@@ -15,6 +15,7 @@ pub mod inner {
1515
//~^ pub_underscore_fields
1616
_c: i32,
1717
pub _mark: marker::PhantomData<u8>,
18+
pub _pinned: marker::PhantomPinned,
1819
}
1920

2021
mod inner2 {
@@ -68,6 +69,7 @@ fn main() {
6869
r#pub: bool,
6970
_pub: String,
7071
pub(crate) _mark: PhantomData<u8>,
72+
pub(crate) _pinned: PhantomPinned,
7173
}
7274

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

tests/ui/missing_fields_in_debug.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![warn(clippy::missing_fields_in_debug)]
33

44
use std::fmt;
5-
use std::marker::PhantomData;
5+
use std::marker::{PhantomData, PhantomPinned};
66
use std::ops::Deref;
77
use std::thread::LocalKey;
88

@@ -179,16 +179,18 @@ mod comment1175473620 {
179179
}
180180

181181
// https://github.com/rust-lang/rust-clippy/pull/10616#discussion_r1175488757
182-
// PhantomData is an exception and does not need to be included
183-
struct WithPD {
182+
// https://github.com/rust-lang/rust/issues/154888
183+
// PhantomData & PhantomPinned are exceptions and do not need to be included
184+
struct WithPDPP {
184185
a: u8,
185186
b: u8,
186187
c: PhantomData<String>,
188+
d: PhantomPinned,
187189
}
188190

189-
impl fmt::Debug for WithPD {
191+
impl fmt::Debug for WithPDPP {
190192
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191-
f.debug_struct("WithPD")
193+
f.debug_struct("WithPDPP")
192194
.field("a", &self.a)
193195
.field("b", &self.b)
194196
.finish()

0 commit comments

Comments
 (0)