1
Fork 0

Rollup merge of #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`
This commit is contained in:
Matthias Krüger 2024-11-10 22:20:35 +01:00 committed by GitHub
commit 54cb1f7fe6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 307 additions and 16 deletions

View file

@ -1298,12 +1298,30 @@ impl UnreachablePub {
let mut applicability = Applicability::MachineApplicable;
if cx.tcx.visibility(def_id).is_public() && !cx.effective_visibilities.is_reachable(def_id)
{
// prefer suggesting `pub(super)` instead of `pub(crate)` when possible,
// except when `pub(super) == pub(crate)`
let new_vis = if let Some(ty::Visibility::Restricted(restricted_did)) =
cx.effective_visibilities.effective_vis(def_id).map(|effective_vis| {
effective_vis.at_level(rustc_middle::middle::privacy::Level::Reachable)
})
&& let parent_parent = cx.tcx.parent_module_from_def_id(
cx.tcx.parent_module_from_def_id(def_id.into()).into(),
)
&& *restricted_did == parent_parent.to_local_def_id()
&& !restricted_did.to_def_id().is_crate_root()
{
"pub(super)"
} else {
"pub(crate)"
};
if vis_span.from_expansion() {
applicability = Applicability::MaybeIncorrect;
}
let def_span = cx.tcx.def_span(def_id);
cx.emit_span_lint(UNREACHABLE_PUB, def_span, BuiltinUnreachablePub {
what,
new_vis,
suggestion: (vis_span, applicability),
help: exportable,
});

View file

@ -254,7 +254,8 @@ impl<'a> LintDiagnostic<'a, ()> for BuiltinUngatedAsyncFnTrackCaller<'_> {
#[diag(lint_builtin_unreachable_pub)]
pub(crate) struct BuiltinUnreachablePub<'a> {
pub what: &'a str,
#[suggestion(code = "pub(crate)")]
pub new_vis: &'a str,
#[suggestion(code = "{new_vis}")]
pub suggestion: (Span, Applicability),
#[help]
pub help: bool,