1 use crate::utils::span_lint_and_help;
2 use rustc_hir::{Item, ItemKind, VisibilityKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{declare_tool_lint, impl_lint_pass};
7 /// **What it does:** Checks for items declared `pub(crate)` that are not crate visible because they
8 /// are inside a private module.
10 /// **Why is this bad?** Writing `pub(crate)` is misleading when it's redundant due to the parent
11 /// module's visibility.
13 /// **Known problems:** None.
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 pub REDUNDANT_PUB_CRATE,
31 "Using `pub(crate)` visibility on items that are not crate visible due to the visibility of the module that contains them."
35 pub struct RedundantPubCrate {
36 is_exported: Vec<bool>,
39 impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
41 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPubCrate {
42 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'tcx>) {
43 if let VisibilityKind::Crate { .. } = item.vis.node {
44 if !cx.access_levels.is_exported(item.hir_id) {
45 if let Some(false) = self.is_exported.last() {
50 &format!("pub(crate) {} inside private module", item.kind.descr()),
51 "consider using `pub` instead of `pub(crate)`",
57 if let ItemKind::Mod { .. } = item.kind {
58 self.is_exported.push(cx.access_levels.is_exported(item.hir_id));
62 fn check_item_post(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'tcx>) {
63 if let ItemKind::Mod { .. } = item.kind {
64 self.is_exported.pop().expect("unbalanced check_item/check_item_post");