diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-06-25 21:33:41 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-25 21:33:41 +0200 |
| commit | 58bbade92104c5d60eadb0c3415df8fc22a77fe9 (patch) | |
| tree | c1e4035df1eb8000a92b7c71af0d3c25c6ecadd8 /compiler | |
| parent | c290e9de32e8ba6a673ef125fde40eadd395d170 (diff) | |
| parent | 6997b6876d62b7c42faf12af3164c87ab733be13 (diff) | |
| download | rust-58bbade92104c5d60eadb0c3415df8fc22a77fe9.tar.gz rust-58bbade92104c5d60eadb0c3415df8fc22a77fe9.zip | |
Rollup merge of #126302 - mu001999-contrib:ignore/default, r=michaelwoerister
Detect unused structs which derived Default
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.
This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using
r? <reviewer name>
-->
Fixes #98871
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_passes/src/dead.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 7c7700dd859..2c86949685b 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -399,6 +399,31 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { return false; } + // don't ignore impls for Enums and pub Structs whose methods don't have self receiver, + // cause external crate may call such methods to construct values of these types + if let Some(local_impl_of) = impl_of.as_local() + && let Some(local_def_id) = def_id.as_local() + && let Some(fn_sig) = + self.tcx.hir().fn_sig_by_hir_id(self.tcx.local_def_id_to_hir_id(local_def_id)) + && matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None) + && let TyKind::Path(hir::QPath::Resolved(_, path)) = + self.tcx.hir().expect_item(local_impl_of).expect_impl().self_ty.kind + && let Res::Def(def_kind, did) = path.res + { + match def_kind { + // for example, #[derive(Default)] pub struct T(i32); + // external crate can call T::default() to construct T, + // so that don't ignore impl Default for pub Enum and Structs + DefKind::Struct | DefKind::Union if self.tcx.visibility(did).is_public() => { + return false; + } + // don't ignore impl Default for Enums, + // cause we don't know which variant is constructed + DefKind::Enum => return false, + _ => (), + }; + } + if let Some(trait_of) = self.tcx.trait_id_of_impl(impl_of) && self.tcx.has_attr(trait_of, sym::rustc_trivial_field_reads) { |
