Skip to content

Commit 38978eb

Browse files
authored
Unrolled build for rust-lang#132426
Rollup merge of rust-lang#132426 - Urgau:unreach_pub-super, r=petrochenkov Prefer `pub(super)` in `unreachable_pub` lint suggestion This PR updates the `unreachable_pub` lint suggestion to prefer `pub(super)` instead of `pub(crate)` when possible. cc `@petrochenkov` r? `@nnethercote`
2 parents 2128d8d + 0bc622d commit 38978eb

File tree

5 files changed

+307
-16
lines changed

5 files changed

+307
-16
lines changed

compiler/rustc_lint/src/builtin.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1298,12 +1298,30 @@ impl UnreachablePub {
12981298
let mut applicability = Applicability::MachineApplicable;
12991299
if cx.tcx.visibility(def_id).is_public() && !cx.effective_visibilities.is_reachable(def_id)
13001300
{
1301+
// prefer suggesting `pub(super)` instead of `pub(crate)` when possible,
1302+
// except when `pub(super) == pub(crate)`
1303+
let new_vis = if let Some(ty::Visibility::Restricted(restricted_did)) =
1304+
cx.effective_visibilities.effective_vis(def_id).map(|effective_vis| {
1305+
effective_vis.at_level(rustc_middle::middle::privacy::Level::Reachable)
1306+
})
1307+
&& let parent_parent = cx.tcx.parent_module_from_def_id(
1308+
cx.tcx.parent_module_from_def_id(def_id.into()).into(),
1309+
)
1310+
&& *restricted_did == parent_parent.to_local_def_id()
1311+
&& !restricted_did.to_def_id().is_crate_root()
1312+
{
1313+
"pub(super)"
1314+
} else {
1315+
"pub(crate)"
1316+
};
1317+
13011318
if vis_span.from_expansion() {
13021319
applicability = Applicability::MaybeIncorrect;
13031320
}
13041321
let def_span = cx.tcx.def_span(def_id);
13051322
cx.emit_span_lint(UNREACHABLE_PUB, def_span, BuiltinUnreachablePub {
13061323
what,
1324+
new_vis,
13071325
suggestion: (vis_span, applicability),
13081326
help: exportable,
13091327
});

compiler/rustc_lint/src/lints.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ impl<'a> LintDiagnostic<'a, ()> for BuiltinUngatedAsyncFnTrackCaller<'_> {
254254
#[diag(lint_builtin_unreachable_pub)]
255255
pub(crate) struct BuiltinUnreachablePub<'a> {
256256
pub what: &'a str,
257-
#[suggestion(code = "pub(crate)")]
257+
pub new_vis: &'a str,
258+
#[suggestion(code = "{new_vis}")]
258259
pub suggestion: (Span, Applicability),
259260
#[help]
260261
pub help: bool,

tests/ui/lint/unreachable_pub.fixed

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//@ check-pass
2+
//@ edition: 2018
3+
//@ run-rustfix
4+
5+
#![allow(unused)]
6+
#![warn(unreachable_pub)]
7+
8+
mod private_mod {
9+
// non-leaked `pub` items in private module should be linted
10+
pub(crate) use std::fmt; //~ WARNING unreachable_pub
11+
pub(crate) use std::env::{Args}; // braced-use has different item spans than unbraced
12+
//~^ WARNING unreachable_pub
13+
14+
// we lint on struct definition
15+
pub(crate) struct Hydrogen { //~ WARNING unreachable_pub
16+
// but not on fields, even if they are `pub` as putting `pub(crate)`
17+
// it would clutter the source code for little value
18+
pub neutrons: usize,
19+
pub(crate) electrons: usize
20+
}
21+
pub(crate) struct Calcium {
22+
pub neutrons: usize,
23+
}
24+
impl Hydrogen {
25+
// impls, too
26+
pub(crate) fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub
27+
pub(crate) fn count_electrons(&self) -> usize { self.electrons }
28+
}
29+
impl Clone for Hydrogen {
30+
fn clone(&self) -> Hydrogen {
31+
Hydrogen { neutrons: self.neutrons, electrons: self.electrons }
32+
}
33+
}
34+
35+
pub(crate) enum Helium {} //~ WARNING unreachable_pub
36+
pub(crate) union Lithium { c1: usize, c2: u8 } //~ WARNING unreachable_pub
37+
pub(crate) fn beryllium() {} //~ WARNING unreachable_pub
38+
pub(crate) trait Boron {} //~ WARNING unreachable_pub
39+
pub(crate) const CARBON: usize = 1; //~ WARNING unreachable_pub
40+
pub(crate) static NITROGEN: usize = 2; //~ WARNING unreachable_pub
41+
pub(crate) type Oxygen = bool; //~ WARNING unreachable_pub
42+
43+
macro_rules! define_empty_struct_with_visibility {
44+
($visibility: vis, $name: ident) => { $visibility struct $name {} }
45+
//~^ WARNING unreachable_pub
46+
}
47+
define_empty_struct_with_visibility!(pub(crate), Fluorine);
48+
49+
extern "C" {
50+
pub(crate) fn catalyze() -> bool; //~ WARNING unreachable_pub
51+
}
52+
53+
mod private_in_private {
54+
pub(super) enum Helium {} //~ WARNING unreachable_pub
55+
pub(super) fn beryllium() {} //~ WARNING unreachable_pub
56+
}
57+
58+
pub(crate) mod crate_in_private {
59+
pub(crate) const CARBON: usize = 1; //~ WARNING unreachable_pub
60+
}
61+
62+
pub(crate) mod pub_in_private { //~ WARNING unreachable_pub
63+
pub(crate) static NITROGEN: usize = 2; //~ WARNING unreachable_pub
64+
}
65+
66+
fn foo() {
67+
const {
68+
pub(crate) struct Foo; //~ WARNING unreachable_pub
69+
};
70+
}
71+
72+
enum Weird {
73+
Variant = {
74+
pub(crate) struct Foo; //~ WARNING unreachable_pub
75+
76+
mod tmp {
77+
pub(crate) struct Bar; //~ WARNING unreachable_pub
78+
}
79+
80+
let _ = tmp::Bar;
81+
82+
0
83+
},
84+
}
85+
86+
pub(crate) use fpu_precision::set_precision; //~ WARNING unreachable_pub
87+
88+
mod fpu_precision {
89+
pub(crate) fn set_precision<T>() {} //~ WARNING unreachable_pub
90+
pub(super) fn set_micro_precision<T>() {} //~ WARNING unreachable_pub
91+
}
92+
93+
// items leaked through signatures (see `get_neon` below) are OK
94+
pub struct Neon {}
95+
96+
// crate-visible items are OK
97+
pub(crate) struct Sodium {}
98+
}
99+
100+
pub mod public_mod {
101+
// module is public: these are OK, too
102+
pub struct Magnesium {}
103+
pub(crate) struct Aluminum {}
104+
}
105+
106+
pub fn get_neon() -> private_mod::Neon {
107+
private_mod::Neon {}
108+
}
109+
110+
fn main() {
111+
let _ = get_neon();
112+
let _ = private_mod::beryllium();
113+
let _ = private_mod::crate_in_private::CARBON;
114+
let _ = private_mod::pub_in_private::NITROGEN;
115+
let _ = unsafe { private_mod::catalyze() };
116+
}

tests/ui/lint/unreachable_pub.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ check-pass
2+
//@ edition: 2018
3+
//@ run-rustfix
24

35
#![allow(unused)]
46
#![warn(unreachable_pub)]
@@ -48,6 +50,46 @@ mod private_mod {
4850
pub fn catalyze() -> bool; //~ WARNING unreachable_pub
4951
}
5052

53+
mod private_in_private {
54+
pub enum Helium {} //~ WARNING unreachable_pub
55+
pub fn beryllium() {} //~ WARNING unreachable_pub
56+
}
57+
58+
pub(crate) mod crate_in_private {
59+
pub const CARBON: usize = 1; //~ WARNING unreachable_pub
60+
}
61+
62+
pub mod pub_in_private { //~ WARNING unreachable_pub
63+
pub static NITROGEN: usize = 2; //~ WARNING unreachable_pub
64+
}
65+
66+
fn foo() {
67+
const {
68+
pub struct Foo; //~ WARNING unreachable_pub
69+
};
70+
}
71+
72+
enum Weird {
73+
Variant = {
74+
pub struct Foo; //~ WARNING unreachable_pub
75+
76+
mod tmp {
77+
pub struct Bar; //~ WARNING unreachable_pub
78+
}
79+
80+
let _ = tmp::Bar;
81+
82+
0
83+
},
84+
}
85+
86+
pub use fpu_precision::set_precision; //~ WARNING unreachable_pub
87+
88+
mod fpu_precision {
89+
pub fn set_precision<T>() {} //~ WARNING unreachable_pub
90+
pub fn set_micro_precision<T>() {} //~ WARNING unreachable_pub
91+
}
92+
5193
// items leaked through signatures (see `get_neon` below) are OK
5294
pub struct Neon {}
5395

@@ -67,4 +109,8 @@ pub fn get_neon() -> private_mod::Neon {
67109

68110
fn main() {
69111
let _ = get_neon();
112+
let _ = private_mod::beryllium();
113+
let _ = private_mod::crate_in_private::CARBON;
114+
let _ = private_mod::pub_in_private::NITROGEN;
115+
let _ = unsafe { private_mod::catalyze() };
70116
}

0 commit comments

Comments
 (0)