Skip to content

Commit 6a065f7

Browse files
committed
Fix unreachable_pub suggestion for enum with fields
1 parent 542febd commit 6a065f7

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

compiler/rustc_lint/src/builtin.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, Gate
4040
use rustc_hir as hir;
4141
use rustc_hir::def::{DefKind, Res};
4242
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
43-
use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, PatKind, PredicateOrigin};
43+
use rustc_hir::{ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin};
4444
use rustc_index::vec::Idx;
4545
use rustc_middle::lint::in_external_macro;
4646
use rustc_middle::ty::layout::{LayoutError, LayoutOf};
@@ -1430,7 +1430,11 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14301430
}
14311431

14321432
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
1433-
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
1433+
let map = cx.tcx.hir();
1434+
let def_id = map.local_def_id(field.hir_id);
1435+
if matches!(map.get(map.get_parent_node(field.hir_id)), Node::Variant(_)) {
1436+
return;
1437+
}
14341438
self.perform_lint(cx, "field", def_id, field.vis_span, false);
14351439
}
14361440

src/test/ui/lint/issue-103317.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// check-pass
2+
3+
#[warn(unreachable_pub)]
4+
#[allow(unused)]
5+
mod inner {
6+
pub enum T {
7+
//~^ WARN unreachable `pub` item
8+
A(u8),
9+
X { a: f32, b: () },
10+
}
11+
}
12+
13+
fn main() {}

src/test/ui/lint/issue-103317.stderr

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: unreachable `pub` item
2+
--> $DIR/issue-103317.rs:6:5
3+
|
4+
LL | pub enum T {
5+
| ---^^^^^^^
6+
| |
7+
| help: consider restricting its visibility: `pub(crate)`
8+
|
9+
= help: or consider exporting it for use by other crates
10+
note: the lint level is defined here
11+
--> $DIR/issue-103317.rs:3:8
12+
|
13+
LL | #[warn(unreachable_pub)]
14+
| ^^^^^^^^^^^^^^^
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)