2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2020-03-20 22:49:15 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-04-22 22:03:23 +00:00
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2022-02-13 10:30:48 +00:00
|
|
|
use rustc_hir::{Item, ItemKind};
|
2020-03-15 17:38:20 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-02-13 10:30:48 +00:00
|
|
|
use rustc_middle::ty;
|
2020-03-15 17:38:20 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2022-02-13 10:30:48 +00:00
|
|
|
use rustc_span::def_id::CRATE_DEF_ID;
|
2022-04-22 22:03:23 +00:00
|
|
|
use rustc_span::hygiene::MacroKind;
|
2020-03-15 17:38:20 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for items declared `pub(crate)` that are not crate visible because they
|
2020-03-15 17:38:20 +00:00
|
|
|
/// are inside a private module.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Writing `pub(crate)` is misleading when it's redundant due to the parent
|
2020-03-15 17:38:20 +00:00
|
|
|
/// module's visibility.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2020-03-15 17:38:20 +00:00
|
|
|
/// ```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() { }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.44.0"]
|
2020-03-15 17:38:20 +00:00
|
|
|
pub REDUNDANT_PUB_CRATE,
|
2020-03-25 17:09:25 +00:00
|
|
|
nursery,
|
2020-03-15 17:38:20 +00: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 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
2022-04-22 22:03:23 +00:00
|
|
|
if_chain! {
|
2022-05-05 12:01:37 +00:00
|
|
|
if cx.tcx.visibility(item.def_id) == ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id());
|
2022-04-22 22:03:23 +00:00
|
|
|
if !cx.access_levels.is_exported(item.def_id) && self.is_exported.last() == Some(&false);
|
|
|
|
if is_not_macro_export(item);
|
|
|
|
then {
|
2021-10-07 09:21:30 +00:00
|
|
|
let span = item.span.with_hi(item.ident.span.hi());
|
|
|
|
let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id());
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_PUB_CRATE,
|
|
|
|
span,
|
|
|
|
&format!("pub(crate) {} inside private module", descr),
|
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2022-02-13 10:30:48 +00:00
|
|
|
item.vis_span,
|
2021-10-07 09:21:30 +00:00
|
|
|
"consider using",
|
|
|
|
"pub".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2020-03-15 17:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let ItemKind::Mod { .. } = item.kind {
|
2021-07-28 22:07:32 +00:00
|
|
|
self.is_exported.push(cx.access_levels.is_exported(item.def_id));
|
2020-03-15 17:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_item_post(&mut self, _cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
2020-03-15 17:38:20 +00:00
|
|
|
if let ItemKind::Mod { .. } = item.kind {
|
|
|
|
self.is_exported.pop().expect("unbalanced check_item/check_item_post");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 22:03:23 +00:00
|
|
|
|
|
|
|
fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool {
|
|
|
|
if let ItemKind::Use(path, _) = item.kind {
|
|
|
|
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res {
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-05 12:01:37 +00:00
|
|
|
} else if let ItemKind::Macro(..) = item.kind {
|
|
|
|
return false;
|
2022-04-22 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|