about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-10-31 14:52:55 +0530
committerGitHub <noreply@github.com>2022-10-31 14:52:55 +0530
commit5ee0fb1c688c38a9c2649104ca28e442f0698be3 (patch)
treef8321789785e0cf1bdbd535b2ed9bac34e55dff9
parent4596f4f8b565bdd02d3b99d1ab12ff09146a93de (diff)
parent137271ad73585428bf0a69271a888f81c254c8c4 (diff)
downloadrust-5ee0fb1c688c38a9c2649104ca28e442f0698be3.tar.gz
rust-5ee0fb1c688c38a9c2649104ca28e442f0698be3.zip
Rollup merge of #103338 - l4l:enum-unreachable-pub, r=nagisa
Fix unreachable_pub suggestion for enum with fields

Resolves #103317
-rw-r--r--compiler/rustc_lint/src/builtin.rs8
-rw-r--r--src/test/ui/lint/issue-103317.fixed14
-rw-r--r--src/test/ui/lint/issue-103317.rs14
-rw-r--r--src/test/ui/lint/issue-103317.stderr17
4 files changed, 51 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs
index d425adf47f0..27c04d82811 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};
@@ -1423,7 +1423,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.fixed b/src/test/ui/lint/issue-103317.fixed
new file mode 100644
index 00000000000..5a987423e5b
--- /dev/null
+++ b/src/test/ui/lint/issue-103317.fixed
@@ -0,0 +1,14 @@
+// check-pass
+// run-rustfix
+
+#[warn(unreachable_pub)]
+mod inner {
+    #[allow(unused)]
+    pub(crate) enum T {
+        //~^ WARN unreachable `pub` item
+        A(u8),
+        X { a: f32, b: () },
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/lint/issue-103317.rs b/src/test/ui/lint/issue-103317.rs
new file mode 100644
index 00000000000..c2ba939e13c
--- /dev/null
+++ b/src/test/ui/lint/issue-103317.rs
@@ -0,0 +1,14 @@
+// check-pass
+// run-rustfix
+
+#[warn(unreachable_pub)]
+mod inner {
+    #[allow(unused)]
+    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..9c982ddc346
--- /dev/null
+++ b/src/test/ui/lint/issue-103317.stderr
@@ -0,0 +1,17 @@
+warning: unreachable `pub` item
+  --> $DIR/issue-103317.rs:7: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:4:8
+   |
+LL | #[warn(unreachable_pub)]
+   |        ^^^^^^^^^^^^^^^
+
+warning: 1 warning emitted
+