Skip to content

Commit

Permalink
Also add logic to handle anon-const in unreachable_pub
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Nov 2, 2024
1 parent fca6a56 commit 435695a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
25 changes: 24 additions & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,30 @@ impl UnreachablePub {
}
} else {
// current item can't be reach from crate, probably in an anon-const
"pub(crate)"
if let Some(direct_parent) = cx.tcx.opt_local_parent(def_id.into()) {
if matches!(
cx.tcx.def_kind(direct_parent),
DefKind::AnonConst | DefKind::InlineConst
) {
// current item at top level const-anon
""
} else if let Some(direct_parent_parent) =
cx.tcx.opt_local_parent(direct_parent)
&& matches!(
cx.tcx.def_kind(direct_parent_parent),
DefKind::AnonConst | DefKind::InlineConst
)
{
// current item is restricted to the parent of it's parent (aka super)
"pub(super)"
} else {
// current item is not restricted to the parent of it's parent
"pub(crate)"
}
} else {
// current item has no parent, is that even possible?
"pub(crate)"
}
};

if vis_span.from_expansion() {
Expand Down
11 changes: 8 additions & 3 deletions tests/ui/lint/unreachable_pub.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,24 @@ mod private_mod {

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

enum Weird {
Variant = {
pub(crate) struct Foo; //~ WARNING unreachable_pub
struct Foo; //~ WARNING unreachable_pub

mod tmp {
pub(crate) struct Bar; //~ WARNING unreachable_pub
pub(super) struct Bar; //~ WARNING unreachable_pub

pub(super) mod inner { //~ WARNING unreachable_pub
pub(crate) struct Inner; //~ WARNING unreachable_pub
}
}

let _ = tmp::Bar;
let _ = tmp::inner::Inner;

0
},
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/lint/unreachable_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ mod private_mod {

mod tmp {
pub struct Bar; //~ WARNING unreachable_pub

pub mod inner { //~ WARNING unreachable_pub
pub struct Inner; //~ WARNING unreachable_pub
}
}

let _ = tmp::Bar;
let _ = tmp::inner::Inner;

0
},
Expand Down
34 changes: 27 additions & 7 deletions tests/ui/lint/unreachable_pub.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ warning: unreachable `pub` item
LL | pub struct Foo;
| ---^^^^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
| help: consider restricting its visibility
|
= help: or consider exporting it for use by other crates

Expand All @@ -162,12 +162,12 @@ warning: unreachable `pub` item
LL | pub struct Foo;
| ---^^^^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
| help: consider restricting its visibility
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:86:13
--> $DIR/unreachable_pub.rs:91:13
|
LL | pub use fpu_precision::set_precision;
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -222,12 +222,32 @@ warning: unreachable `pub` item
LL | pub struct Bar;
| ---^^^^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
| help: consider restricting its visibility: `pub(super)`
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:89:9
--> $DIR/unreachable_pub.rs:79:17
|
LL | pub mod inner {
| ---^^^^^^^^^^
| |
| help: consider restricting its visibility: `pub(super)`
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:80:21
|
LL | pub struct Inner;
| ---^^^^^^^^^^^^^
| |
| help: consider restricting its visibility: `pub(crate)`
|
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:94:9
|
LL | pub fn set_precision<T>() {}
| ---^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -237,7 +257,7 @@ LL | pub fn set_precision<T>() {}
= help: or consider exporting it for use by other crates

warning: unreachable `pub` item
--> $DIR/unreachable_pub.rs:90:9
--> $DIR/unreachable_pub.rs:95:9
|
LL | pub fn set_micro_precision<T>() {}
| ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -246,5 +266,5 @@ LL | pub fn set_micro_precision<T>() {}
|
= help: or consider exporting it for use by other crates

warning: 24 warnings emitted
warning: 26 warnings emitted

0 comments on commit 435695a

Please sign in to comment.