]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs
Rollup merge of #76082 - jyn514:top-level-links, r=ollie27,GuillaumeGomez
[rust.git] / src / tools / clippy / clippy_lints / src / redundant_pub_crate.rs
1 use crate::utils::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};
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for items declared `pub(crate)` that are not crate visible because they
9     /// are inside a private module.
10     ///
11     /// **Why is this bad?** Writing `pub(crate)` is misleading when it's redundant due to the parent
12     /// module's visibility.
13     ///
14     /// **Known problems:** None.
15     ///
16     /// **Example:**
17     ///
18     /// ```rust
19     /// mod internal {
20     ///     pub(crate) fn internal_fn() { }
21     /// }
22     /// ```
23     /// This function is not visible outside the module and it can be declared with `pub` or
24     /// private visibility
25     /// ```rust
26     /// mod internal {
27     ///     pub fn internal_fn() { }
28     /// }
29     /// ```
30     pub REDUNDANT_PUB_CRATE,
31     nursery,
32     "Using `pub(crate)` visibility on items that are not crate visible due to the visibility of the module that contains them."
33 }
34
35 #[derive(Default)]
36 pub struct RedundantPubCrate {
37     is_exported: Vec<bool>,
38 }
39
40 impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
41
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.hir_id) {
46                 if let Some(false) = self.is_exported.last() {
47                     let span = item.span.with_hi(item.ident.span.hi());
48                     let def_id = cx.tcx.hir().local_def_id(item.hir_id);
49                     let descr = cx.tcx.def_kind(def_id).descr(def_id.to_def_id());
50                     span_lint_and_then(
51                         cx,
52                         REDUNDANT_PUB_CRATE,
53                         span,
54                         &format!("pub(crate) {} inside private module", descr),
55                         |diag| {
56                             diag.span_suggestion(
57                                 item.vis.span,
58                                 "consider using",
59                                 "pub".to_string(),
60                                 Applicability::MachineApplicable,
61                             );
62                         },
63                     )
64                 }
65             }
66         }
67
68         if let ItemKind::Mod { .. } = item.kind {
69             self.is_exported.push(cx.access_levels.is_exported(item.hir_id));
70         }
71     }
72
73     fn check_item_post(&mut self, _cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
74         if let ItemKind::Mod { .. } = item.kind {
75             self.is_exported.pop().expect("unbalanced check_item/check_item_post");
76         }
77     }
78 }