Skip to content

Commit 5ee0fb1

Browse files
authored
Rollup merge of #103338 - l4l:enum-unreachable-pub, r=nagisa
Fix unreachable_pub suggestion for enum with fields Resolves #103317
2 parents 4596f4f + 137271a commit 5ee0fb1

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-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};
@@ -1423,7 +1423,11 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14231423
}
14241424

14251425
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
1426-
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
1426+
let map = cx.tcx.hir();
1427+
let def_id = map.local_def_id(field.hir_id);
1428+
if matches!(map.get(map.get_parent_node(field.hir_id)), Node::Variant(_)) {
1429+
return;
1430+
}
14271431
self.perform_lint(cx, "field", def_id, field.vis_span, false);
14281432
}
14291433

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

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

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
// run-rustfix
3+
4+
#[warn(unreachable_pub)]
5+
mod inner {
6+
#[allow(unused)]
7+
pub enum T {
8+
//~^ WARN unreachable `pub` item
9+
A(u8),
10+
X { a: f32, b: () },
11+
}
12+
}
13+
14+
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:7: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:4:8
12+
|
13+
LL | #[warn(unreachable_pub)]
14+
| ^^^^^^^^^^^^^^^
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)