Skip to content

Commit 435695a

Browse files
committed
Also add logic to handle anon-const in unreachable_pub
1 parent fca6a56 commit 435695a

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,30 @@ impl UnreachablePub {
13361336
}
13371337
} else {
13381338
// current item can't be reach from crate, probably in an anon-const
1339-
"pub(crate)"
1339+
if let Some(direct_parent) = cx.tcx.opt_local_parent(def_id.into()) {
1340+
if matches!(
1341+
cx.tcx.def_kind(direct_parent),
1342+
DefKind::AnonConst | DefKind::InlineConst
1343+
) {
1344+
// current item at top level const-anon
1345+
""
1346+
} else if let Some(direct_parent_parent) =
1347+
cx.tcx.opt_local_parent(direct_parent)
1348+
&& matches!(
1349+
cx.tcx.def_kind(direct_parent_parent),
1350+
DefKind::AnonConst | DefKind::InlineConst
1351+
)
1352+
{
1353+
// current item is restricted to the parent of it's parent (aka super)
1354+
"pub(super)"
1355+
} else {
1356+
// current item is not restricted to the parent of it's parent
1357+
"pub(crate)"
1358+
}
1359+
} else {
1360+
// current item has no parent, is that even possible?
1361+
"pub(crate)"
1362+
}
13401363
};
13411364

13421365
if vis_span.from_expansion() {

tests/ui/lint/unreachable_pub.fixed

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,24 @@ mod private_mod {
6565

6666
fn foo() {
6767
const {
68-
pub(crate) struct Foo; //~ WARNING unreachable_pub
68+
struct Foo; //~ WARNING unreachable_pub
6969
};
7070
}
7171

7272
enum Weird {
7373
Variant = {
74-
pub(crate) struct Foo; //~ WARNING unreachable_pub
74+
struct Foo; //~ WARNING unreachable_pub
7575

7676
mod tmp {
77-
pub(crate) struct Bar; //~ WARNING unreachable_pub
77+
pub(super) struct Bar; //~ WARNING unreachable_pub
78+
79+
pub(super) mod inner { //~ WARNING unreachable_pub
80+
pub(crate) struct Inner; //~ WARNING unreachable_pub
81+
}
7882
}
7983

8084
let _ = tmp::Bar;
85+
let _ = tmp::inner::Inner;
8186

8287
0
8388
},

tests/ui/lint/unreachable_pub.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,14 @@ mod private_mod {
7575

7676
mod tmp {
7777
pub struct Bar; //~ WARNING unreachable_pub
78+
79+
pub mod inner { //~ WARNING unreachable_pub
80+
pub struct Inner; //~ WARNING unreachable_pub
81+
}
7882
}
7983

8084
let _ = tmp::Bar;
85+
let _ = tmp::inner::Inner;
8186

8287
0
8388
},

tests/ui/lint/unreachable_pub.stderr

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ warning: unreachable `pub` item
152152
LL | pub struct Foo;
153153
| ---^^^^^^^^^^^
154154
| |
155-
| help: consider restricting its visibility: `pub(crate)`
155+
| help: consider restricting its visibility
156156
|
157157
= help: or consider exporting it for use by other crates
158158

@@ -162,12 +162,12 @@ warning: unreachable `pub` item
162162
LL | pub struct Foo;
163163
| ---^^^^^^^^^^^
164164
| |
165-
| help: consider restricting its visibility: `pub(crate)`
165+
| help: consider restricting its visibility
166166
|
167167
= help: or consider exporting it for use by other crates
168168

169169
warning: unreachable `pub` item
170-
--> $DIR/unreachable_pub.rs:86:13
170+
--> $DIR/unreachable_pub.rs:91:13
171171
|
172172
LL | pub use fpu_precision::set_precision;
173173
| --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -222,12 +222,32 @@ warning: unreachable `pub` item
222222
LL | pub struct Bar;
223223
| ---^^^^^^^^^^^
224224
| |
225-
| help: consider restricting its visibility: `pub(crate)`
225+
| help: consider restricting its visibility: `pub(super)`
226226
|
227227
= help: or consider exporting it for use by other crates
228228

229229
warning: unreachable `pub` item
230-
--> $DIR/unreachable_pub.rs:89:9
230+
--> $DIR/unreachable_pub.rs:79:17
231+
|
232+
LL | pub mod inner {
233+
| ---^^^^^^^^^^
234+
| |
235+
| help: consider restricting its visibility: `pub(super)`
236+
|
237+
= help: or consider exporting it for use by other crates
238+
239+
warning: unreachable `pub` item
240+
--> $DIR/unreachable_pub.rs:80:21
241+
|
242+
LL | pub struct Inner;
243+
| ---^^^^^^^^^^^^^
244+
| |
245+
| help: consider restricting its visibility: `pub(crate)`
246+
|
247+
= help: or consider exporting it for use by other crates
248+
249+
warning: unreachable `pub` item
250+
--> $DIR/unreachable_pub.rs:94:9
231251
|
232252
LL | pub fn set_precision<T>() {}
233253
| ---^^^^^^^^^^^^^^^^^^^^^^
@@ -237,7 +257,7 @@ LL | pub fn set_precision<T>() {}
237257
= help: or consider exporting it for use by other crates
238258

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

249-
warning: 24 warnings emitted
269+
warning: 26 warnings emitted
250270

0 commit comments

Comments
 (0)