1 use clippy_utils::diagnostics::span_lint_and_then;
2 use rustc_errors::Applicability;
3 use rustc_hir::{Item, ItemKind, VisibilityKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 /// Checks for items declared `pub(crate)` that are not crate visible because they
10 /// are inside a private module.
12 /// ### Why is this bad?
13 /// Writing `pub(crate)` is misleading when it's redundant due to the parent
14 /// module's visibility.
19 /// pub(crate) fn internal_fn() { }
22 /// This function is not visible outside the module and it can be declared with `pub` or
23 /// private visibility
26 /// pub fn internal_fn() { }
29 #[clippy::version = "1.44.0"]
30 pub REDUNDANT_PUB_CRATE,
32 "Using `pub(crate)` visibility on items that are not crate visible due to the visibility of the module that contains them."
36 pub struct RedundantPubCrate {
37 is_exported: Vec<bool>,
40 impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
42 impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
43 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
44 if let VisibilityKind::Crate { .. } = item.vis.node {
45 if !cx.access_levels.is_exported(item.def_id) && self.is_exported.last() == Some(&false) {
46 let span = item.span.with_hi(item.ident.span.hi());
47 let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id());
52 &format!("pub(crate) {} inside private module", descr),
58 Applicability::MachineApplicable,
65 if let ItemKind::Mod { .. } = item.kind {
66 self.is_exported.push(cx.access_levels.is_exported(item.def_id));
70 fn check_item_post(&mut self, _cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
71 if let ItemKind::Mod { .. } = item.kind {
72 self.is_exported.pop().expect("unbalanced check_item/check_item_post");