Revert "Rollup merge of #128104 - mu001999-contrib:fix/128053, r=petr… · rust-lang-ci/rust@efdf219 (original) (raw)
5 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -73,26 +73,24 @@ fn adt_of<'tcx>(ty: &hir::Ty<'tcx>) -> Option<(LocalDefId, DefKind)> { | ||
73 | 73 | } |
74 | 74 | |
75 | 75 | fn struct_all_fields_are_public(tcx: TyCtxt<'_>, id: LocalDefId) -> bool { |
76 | -let adt_def = tcx.adt_def(id); | |
77 | - | |
78 | -// skip types contain fields of unit and never type, | |
79 | -// it's usually intentional to make the type not constructible | |
80 | -let not_require_constructor = adt_def.all_fields().any(|field | |
76 | +// treat PhantomData and positional ZST as public, | |
77 | +// we don't want to lint types which only have them, | |
78 | +// cause it's a common way to use such types to check things like well-formedness | |
79 | + tcx.adt_def(id).all_fields().all(|field | |
81 | 80 | let field_type = tcx.type_of(field.did).instantiate_identity(); |
82 | - field_type.is_unit() | | |
83 | -}); | |
84 | - | |
85 | - not_require_constructor | |
86 | - | | |
87 | -let field_type = tcx.type_of(field.did).instantiate_identity(); | |
88 | -// skip fields of PhantomData, | |
89 | -// cause it's a common way to check things like well-formedness | |
90 | -if field_type.is_phantom_data() { | |
91 | -return true; | |
92 | -} | |
93 | - | |
94 | - field.vis.is_public() | |
95 | -}) | |
81 | +if field_type.is_phantom_data() { | |
82 | +return true; | |
83 | +} | |
84 | +let is_positional = field.name.as_str().starts_with(|c: char | |
85 | +if is_positional | |
86 | + && tcx | |
87 | +.layout_of(tcx.param_env(field.did).and(field_type)) | |
88 | +.map_or(true, |layout | |
89 | +{ | |
90 | +return true; | |
91 | +} | |
92 | + field.vis.is_public() | |
93 | +}) | |
96 | 94 | } |
97 | 95 | |
98 | 96 | /// check struct and its fields are public or not, |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
1 | 1 | #![forbid(dead_code)] |
2 | 2 | |
3 | 3 | #[derive(Debug)] |
4 | -pub struct Whatever { | |
4 | +pub struct Whatever { //~ ERROR struct `Whatever` is never constructed | |
5 | 5 | pub field0: (), |
6 | -field1: (), //~ ERROR fields `field1`, `field2`, `field3`, and `field4` are never read | |
6 | +field1: (), | |
7 | 7 | field2: (), |
8 | 8 | field3: (), |
9 | 9 | field4: (), |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,9 @@ | ||
1 | -error: fields `field1`, `field2`, `field3`, and `field4` are never read | |
2 | - --> $DIR/clone-debug-dead-code-in-the-same-struct.rs:6:5 | |
1 | +error: struct `Whatever` is never constructed | |
2 | + --> $DIR/clone-debug-dead-code-in-the-same-struct.rs:4:12 | |
3 | 3 | | |
4 | 4 | LL | pub struct Whatever { |
5 | - | -------- fields in this struct | |
6 | -LL | pub field0: (), | |
7 | -LL | field1: (), | |
8 | - | ^^^^^^ | |
9 | -LL | field2: (), | |
10 | - | ^^^^^^ | |
11 | -LL | field3: (), | |
12 | - | ^^^^^^ | |
13 | -LL | field4: (), | |
14 | - | ^^^^^^ | |
5 | + | ^^^^^^^^ | |
15 | 6 | | |
16 | - = note: `Whatever` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis | |
17 | 7 | note: the lint level is defined here |
18 | 8 | --> $DIR/clone-debug-dead-code-in-the-same-struct.rs:1:11 |
19 | 9 | | |