Skip to content

Commit 0156e08

Browse files
committed
Don't warn on pub fields of pub(restricted) structs
1 parent a83cf56 commit 0156e08

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

compiler/rustc_lint/src/builtin.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1424,8 +1424,24 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14241424
}
14251425

14261426
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
1427-
if matches!(cx.tcx.parent_hir_node(field.hir_id), Node::Variant(_)) {
1428-
return;
1427+
match cx.tcx.parent_hir_node(field.hir_id) {
1428+
Node::Variant(_) => return,
1429+
// We don't want to lint on struct/union fields whose parent def is
1430+
// not public. We therefore check if the parent is a struct/union and
1431+
// if it `pub` we ignore the field.
1432+
//
1433+
// ```
1434+
// pub(crate) struct Hydrogen {
1435+
// pub neutrons: usize, // should not lint
1436+
// }
1437+
// ```
1438+
Node::Item(item)
1439+
if item.is_struct_or_union()
1440+
&& !cx.tcx.visibility(item.owner_id.def_id).is_public() =>
1441+
{
1442+
return;
1443+
}
1444+
_ => {}
14291445
}
14301446
self.perform_lint(cx, "field", field.def_id, field.vis_span, false);
14311447
}

tests/ui/lint/unreachable_pub.rs

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ mod private_mod {
1515
// (... but not more-restricted fields)
1616
pub(crate) electrons: usize
1717
}
18+
pub(crate) struct Calcium {
19+
pub neutrons: usize, // the visibility is clearly defined by the struct above
20+
// no need to cluter the field definition
21+
}
1822
impl Hydrogen {
1923
// impls, too
2024
pub fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub

tests/ui/lint/unreachable_pub.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ LL | pub neutrons: usize,
4242
| help: consider restricting its visibility: `pub(crate)`
4343

4444
warning: unreachable `pub` item
45-
--> $DIR/unreachable_pub.rs:20:9
45+
--> $DIR/unreachable_pub.rs:24:9
4646
|
4747
LL | pub fn count_neutrons(&self) -> usize { self.neutrons }
4848
| ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4949
| |
5050
| help: consider restricting its visibility: `pub(crate)`
5151

5252
warning: unreachable `pub` item
53-
--> $DIR/unreachable_pub.rs:29:5
53+
--> $DIR/unreachable_pub.rs:33:5
5454
|
5555
LL | pub enum Helium {}
5656
| ---^^^^^^^^^^^^
@@ -60,7 +60,7 @@ LL | pub enum Helium {}
6060
= help: or consider exporting it for use by other crates
6161

6262
warning: unreachable `pub` item
63-
--> $DIR/unreachable_pub.rs:30:5
63+
--> $DIR/unreachable_pub.rs:34:5
6464
|
6565
LL | pub union Lithium { c1: usize, c2: u8 }
6666
| ---^^^^^^^^^^^^^^
@@ -70,7 +70,7 @@ LL | pub union Lithium { c1: usize, c2: u8 }
7070
= help: or consider exporting it for use by other crates
7171

7272
warning: unreachable `pub` item
73-
--> $DIR/unreachable_pub.rs:31:5
73+
--> $DIR/unreachable_pub.rs:35:5
7474
|
7575
LL | pub fn beryllium() {}
7676
| ---^^^^^^^^^^^^^^^
@@ -80,7 +80,7 @@ LL | pub fn beryllium() {}
8080
= help: or consider exporting it for use by other crates
8181

8282
warning: unreachable `pub` item
83-
--> $DIR/unreachable_pub.rs:32:5
83+
--> $DIR/unreachable_pub.rs:36:5
8484
|
8585
LL | pub trait Boron {}
8686
| ---^^^^^^^^^^^^
@@ -90,7 +90,7 @@ LL | pub trait Boron {}
9090
= help: or consider exporting it for use by other crates
9191

9292
warning: unreachable `pub` item
93-
--> $DIR/unreachable_pub.rs:33:5
93+
--> $DIR/unreachable_pub.rs:37:5
9494
|
9595
LL | pub const CARBON: usize = 1;
9696
| ---^^^^^^^^^^^^^^^^^^^^
@@ -100,7 +100,7 @@ LL | pub const CARBON: usize = 1;
100100
= help: or consider exporting it for use by other crates
101101

102102
warning: unreachable `pub` item
103-
--> $DIR/unreachable_pub.rs:34:5
103+
--> $DIR/unreachable_pub.rs:38:5
104104
|
105105
LL | pub static NITROGEN: usize = 2;
106106
| ---^^^^^^^^^^^^^^^^^^^^^^^
@@ -110,7 +110,7 @@ LL | pub static NITROGEN: usize = 2;
110110
= help: or consider exporting it for use by other crates
111111

112112
warning: unreachable `pub` item
113-
--> $DIR/unreachable_pub.rs:35:5
113+
--> $DIR/unreachable_pub.rs:39:5
114114
|
115115
LL | pub type Oxygen = bool;
116116
| ---^^^^^^^^^^^^
@@ -120,7 +120,7 @@ LL | pub type Oxygen = bool;
120120
= help: or consider exporting it for use by other crates
121121

122122
warning: unreachable `pub` item
123-
--> $DIR/unreachable_pub.rs:38:47
123+
--> $DIR/unreachable_pub.rs:42:47
124124
|
125125
LL | ($visibility: vis, $name: ident) => { $visibility struct $name {} }
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -135,7 +135,7 @@ LL | define_empty_struct_with_visibility!(pub, Fluorine);
135135
= note: this warning originates in the macro `define_empty_struct_with_visibility` (in Nightly builds, run with -Z macro-backtrace for more info)
136136

137137
warning: unreachable `pub` item
138-
--> $DIR/unreachable_pub.rs:44:9
138+
--> $DIR/unreachable_pub.rs:48:9
139139
|
140140
LL | pub fn catalyze() -> bool;
141141
| ---^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)