Skip to content

Commit

Permalink
Restrict visibility check to specific HIR items
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Oct 31, 2024
1 parent ec49af7 commit 3fe6228
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 19 deletions.
44 changes: 26 additions & 18 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,26 +1300,34 @@ impl UnreachablePub {
{
// prefer suggesting `pub(super)` instead of `pub(crate)` when possible
let new_vis = match cx.tcx.opt_parent(def_id.into()) {
Some(parent_def_id) => match cx.tcx.visibility(parent_def_id) {
// parent is either `pub(crate)`, `pub(self)` or `pub(in ...)`
ty::Visibility::Restricted(restricted_id) => {
if let Some(local_parent_def_id) = parent_def_id.as_local()
&& restricted_id
== cx.tcx.parent_module_from_def_id(local_parent_def_id).into()
{
// parent is `pub(self)`, current item can only be used by parent
"pub(super)"
} else {
// parent is `pub(crate)` or `pub(in ...)`, current item accessible
// from more place then parent
Some(parent_def_id) => match cx.tcx.def_kind(parent_def_id) {
// parent is a module, or an impl/extern block, check it's visibility
DefKind::Mod | DefKind::Impl { .. } | DefKind::ForeignMod => match cx
.tcx
.visibility(parent_def_id)
{
// parent is either `pub(crate)`, `pub(self)` or `pub(in ...)`
ty::Visibility::Restricted(restricted_id) => {
if let Some(local_parent_def_id) = parent_def_id.as_local()
&& restricted_id
== cx.tcx.parent_module_from_def_id(local_parent_def_id).into()
{
// parent is `pub(self)`, current item can only be used by parent
"pub(super)"
} else {
// parent is `pub(crate)` or `pub(in ...)`, current item accessible
// from more place then parent
"pub(crate)"
}
}
// parent is `pub`
ty::Visibility::Public => {
// current item may be used from anywhere in the crate
"pub(crate)"
}
}
// parent is `pub`
ty::Visibility::Public => {
// current item may be used from anywhere in the crate
"pub(crate)"
}
},
// current item can't be accessed outside of the current block
_ => "",
},
None => "pub(crate)",
};
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/lint/unreachable_pub.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ mod private_mod {
pub(crate) static NITROGEN: usize = 2; //~ WARNING unreachable_pub
}

fn foo() {
const {
struct Foo; //~ WARNING unreachable_pub
};
}

enum Weird {
Variant = {
struct Foo; //~ WARNING unreachable_pub
0
},
}

// items leaked through signatures (see `get_neon` below) are OK
pub struct Neon {}

Expand All @@ -84,4 +97,5 @@ fn main() {
let _ = private_mod::beryllium();
let _ = private_mod::crate_in_private::CARBON;
let _ = private_mod::pub_in_private::NITROGEN;
let _ = unsafe { private_mod::catalyze() };
}
14 changes: 14 additions & 0 deletions tests/ui/lint/unreachable_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ mod private_mod {
pub static NITROGEN: usize = 2; //~ WARNING unreachable_pub
}

fn foo() {
const {
pub struct Foo; //~ WARNING unreachable_pub
};
}

enum Weird {
Variant = {
pub struct Foo; //~ WARNING unreachable_pub
0
},
}

// items leaked through signatures (see `get_neon` below) are OK
pub struct Neon {}

Expand All @@ -84,4 +97,5 @@ fn main() {
let _ = private_mod::beryllium();
let _ = private_mod::crate_in_private::CARBON;
let _ = private_mod::pub_in_private::NITROGEN;
let _ = unsafe { private_mod::catalyze() };
}
22 changes: 21 additions & 1 deletion tests/ui/lint/unreachable_pub.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ LL | pub mod pub_in_private {
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:67:13
|
LL | pub struct Foo;
| ---^^^^^^^^^^^
| |
| help: consider restricting its visibility
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:73:13
|
LL | pub struct Foo;
| ---^^^^^^^^^^^
| |
| help: consider restricting its visibility
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:53:9
|
Expand Down Expand Up @@ -186,5 +206,5 @@ LL | pub static NITROGEN: usize = 2;
|
= help: or consider exporting it for use by other crates

warning: 18 warnings emitted
warning: 20 warnings emitted

0 comments on commit 3fe6228

Please sign in to comment.