Skip to content

Commit

Permalink
Don't warn on pub fields of pub(restricted) structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jun 5, 2024
1 parent a83cf56 commit 0156e08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
20 changes: 18 additions & 2 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1424,8 +1424,24 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
}

fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
if matches!(cx.tcx.parent_hir_node(field.hir_id), Node::Variant(_)) {
return;
match cx.tcx.parent_hir_node(field.hir_id) {
Node::Variant(_) => return,
// We don't want to lint on struct/union fields whose parent def is
// not public. We therefore check if the parent is a struct/union and
// if it `pub` we ignore the field.
//
// ```
// pub(crate) struct Hydrogen {
// pub neutrons: usize, // should not lint
// }
// ```
Node::Item(item)
if item.is_struct_or_union()
&& !cx.tcx.visibility(item.owner_id.def_id).is_public() =>
{
return;
}
_ => {}
}
self.perform_lint(cx, "field", field.def_id, field.vis_span, false);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/lint/unreachable_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ mod private_mod {
// (... but not more-restricted fields)
pub(crate) electrons: usize
}
pub(crate) struct Calcium {
pub neutrons: usize, // the visibility is clearly defined by the struct above
// no need to cluter the field definition
}
impl Hydrogen {
// impls, too
pub fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/lint/unreachable_pub.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ LL | pub neutrons: usize,
| help: consider restricting its visibility: `pub(crate)`

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

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

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

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

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

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

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

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

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

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:44:9
--> $DIR/unreachable_pub.rs:48:9
|
LL | pub fn catalyze() -> bool;
| ---^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 0156e08

Please # to comment.