diff options
Diffstat (limited to 'compiler/rustc_lint/src')
| -rw-r--r-- | compiler/rustc_lint/src/builtin.rs | 18 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/lints.rs | 3 |
2 files changed, 20 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 02d22ee49bd..130f3cb7c2a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -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, }); diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 38e52570e53..352155729e5 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -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, |
