2020-03-20 17:49:15 -05:00
|
|
|
use crate::utils::span_lint_and_then;
|
|
|
|
use rustc_errors::Applicability;
|
2020-03-15 12:38:20 -05:00
|
|
|
use rustc_hir::{Item, ItemKind, VisibilityKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for items declared `pub(crate)` that are not crate visible because they
|
|
|
|
/// are inside a private module.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Writing `pub(crate)` is misleading when it's redundant due to the parent
|
|
|
|
/// module's visibility.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// mod internal {
|
|
|
|
/// pub(crate) fn internal_fn() { }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// This function is not visible outside the module and it can be declared with `pub` or
|
|
|
|
/// private visibility
|
|
|
|
/// ```rust
|
|
|
|
/// mod internal {
|
|
|
|
/// pub fn internal_fn() { }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub REDUNDANT_PUB_CRATE,
|
2020-03-25 12:09:25 -05:00
|
|
|
nursery,
|
2020-03-15 12:38:20 -05:00
|
|
|
"Using `pub(crate)` visibility on items that are not crate visible due to the visibility of the module that contains them."
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct RedundantPubCrate {
|
|
|
|
is_exported: Vec<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
2020-03-15 12:38:20 -05:00
|
|
|
if let VisibilityKind::Crate { .. } = item.vis.node {
|
|
|
|
if !cx.access_levels.is_exported(item.hir_id) {
|
|
|
|
if let Some(false) = self.is_exported.last() {
|
2020-03-20 17:49:15 -05:00
|
|
|
let span = item.span.with_hi(item.ident.span.hi());
|
2020-04-26 03:12:14 -05:00
|
|
|
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
|
|
|
|
let descr = cx.tcx.def_kind(def_id).descr(def_id.to_def_id());
|
2020-03-20 17:49:15 -05:00
|
|
|
span_lint_and_then(
|
2020-03-15 12:38:20 -05:00
|
|
|
cx,
|
|
|
|
REDUNDANT_PUB_CRATE,
|
2020-03-20 17:49:15 -05:00
|
|
|
span,
|
2020-04-26 03:12:14 -05:00
|
|
|
&format!("pub(crate) {} inside private module", descr),
|
2020-04-17 01:08:00 -05:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2020-03-20 17:49:15 -05:00
|
|
|
item.vis.span,
|
|
|
|
"consider using",
|
|
|
|
"pub".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
2020-03-15 12:38:20 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let ItemKind::Mod { .. } = item.kind {
|
|
|
|
self.is_exported.push(cx.access_levels.is_exported(item.hir_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_item_post(&mut self, _cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
2020-03-15 12:38:20 -05:00
|
|
|
if let ItemKind::Mod { .. } = item.kind {
|
|
|
|
self.is_exported.pop().expect("unbalanced check_item/check_item_post");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|