1
Fork 0

Rollup merge of #126040 - Urgau:unreachable_pub-fields-less, r=petrochenkov

Don't warn on fields in the `unreachable_pub` lint

This PR restrict the `unreachable_pub` lint by not linting on `pub` fields of `pub(restricted)` structs and unions. This is done because that can quickly clutter the code for an uncertain value, in particular since the "real" visibility is defined by the parent (the struct it-self).

This is meant to address one of the last concern of the `unreachable_pub` lint.

r? ``@petrochenkov``
This commit is contained in:
Jubilee 2024-06-06 21:10:10 -07:00 committed by GitHub
commit 6a42df7517
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 29 deletions

View file

@ -51,7 +51,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_hir::intravisit::FnKind as HirFnKind;
use rustc_hir::{Body, FnDecl, GenericParamKind, Node, PatKind, PredicateOrigin};
use rustc_hir::{Body, FnDecl, GenericParamKind, PatKind, PredicateOrigin};
use rustc_middle::bug;
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::layout::LayoutOf;
@ -1423,11 +1423,20 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
self.perform_lint(cx, "item", foreign_item.owner_id.def_id, foreign_item.vis_span, true);
}
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
if matches!(cx.tcx.parent_hir_node(field.hir_id), Node::Variant(_)) {
return;
}
self.perform_lint(cx, "field", field.def_id, field.vis_span, false);
fn check_field_def(&mut self, _cx: &LateContext<'_>, _field: &hir::FieldDef<'_>) {
// - If an ADT definition is reported then we don't need to check fields
// (as it would add unnecessary complexity to the source code, the struct
// definition is in the immediate proximity to give the "real" visibility).
// - If an ADT is not reported because it's not `pub` - we don't need to
// check fields.
// - If an ADT is not reported because it's reachable - we also don't need
// to check fields because then they are reachable by construction if they
// are pub.
//
// Therefore in no case we check the fields.
//
// cf. https://github.com/rust-lang/rust/pull/126013#issuecomment-2152839205
// cf. https://github.com/rust-lang/rust/pull/126040#issuecomment-2152944506
}
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {