From 6a065f78c43c8ee8f0a9a0607eb33f8e23ed69f9 Mon Sep 17 00:00:00 2001 From: Kitsu Date: Fri, 21 Oct 2022 12:50:28 +0300 Subject: [PATCH] Fix unreachable_pub suggestion for enum with fields --- compiler/rustc_lint/src/builtin.rs | 8 ++++++-- src/test/ui/lint/issue-103317.rs | 13 +++++++++++++ src/test/ui/lint/issue-103317.stderr | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/lint/issue-103317.rs create mode 100644 src/test/ui/lint/issue-103317.stderr diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 886e25f2d78..dfd66452189 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -40,7 +40,7 @@ use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, Gate use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID}; -use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, PatKind, PredicateOrigin}; +use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin}; use rustc_index::vec::Idx; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::layout::{LayoutError, LayoutOf}; @@ -1430,7 +1430,11 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub { } fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) { - let def_id = cx.tcx.hir().local_def_id(field.hir_id); + let map = cx.tcx.hir(); + let def_id = map.local_def_id(field.hir_id); + if matches!(map.get(map.get_parent_node(field.hir_id)), Node::Variant(_)) { + return; + } self.perform_lint(cx, "field", def_id, field.vis_span, false); } diff --git a/src/test/ui/lint/issue-103317.rs b/src/test/ui/lint/issue-103317.rs new file mode 100644 index 00000000000..b39b441a0cf --- /dev/null +++ b/src/test/ui/lint/issue-103317.rs @@ -0,0 +1,13 @@ +// check-pass + +#[warn(unreachable_pub)] +#[allow(unused)] +mod inner { + pub enum T { + //~^ WARN unreachable `pub` item + A(u8), + X { a: f32, b: () }, + } +} + +fn main() {} diff --git a/src/test/ui/lint/issue-103317.stderr b/src/test/ui/lint/issue-103317.stderr new file mode 100644 index 00000000000..9fdd6a570e6 --- /dev/null +++ b/src/test/ui/lint/issue-103317.stderr @@ -0,0 +1,17 @@ +warning: unreachable `pub` item + --> $DIR/issue-103317.rs:6:5 + | +LL | pub enum T { + | ---^^^^^^^ + | | + | help: consider restricting its visibility: `pub(crate)` + | + = help: or consider exporting it for use by other crates +note: the lint level is defined here + --> $DIR/issue-103317.rs:3:8 + | +LL | #[warn(unreachable_pub)] + | ^^^^^^^^^^^^^^^ + +warning: 1 warning emitted +