Skip to content

Commit

Permalink
Fix unreachable_pub suggestion for enum with fields
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l committed Oct 21, 2022
1 parent 542febd commit 6a065f7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/lint/issue-103317.rs
Original file line number Diff line number Diff line change
@@ -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() {}
17 changes: 17 additions & 0 deletions src/test/ui/lint/issue-103317.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6a065f7

Please sign in to comment.