]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/redundant_pub_crate.rs
Fix lint registration
[rust.git] / clippy_lints / src / redundant_pub_crate.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use rustc_errors::Applicability;
3 use rustc_hir::def::{DefKind, Res};
4 use rustc_hir::{Item, ItemKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::ty;
7 use rustc_session::{declare_tool_lint, impl_lint_pass};
8 use rustc_span::def_id::CRATE_DEF_ID;
9 use rustc_span::hygiene::MacroKind;
10
11 declare_clippy_lint! {
12     /// ### What it does
13     /// Checks for items declared `pub(crate)` that are not crate visible because they
14     /// are inside a private module.
15     ///
16     /// ### Why is this bad?
17     /// Writing `pub(crate)` is misleading when it's redundant due to the parent
18     /// module's visibility.
19     ///
20     /// ### Example
21     /// ```rust
22     /// mod internal {
23     ///     pub(crate) fn internal_fn() { }
24     /// }
25     /// ```
26     /// This function is not visible outside the module and it can be declared with `pub` or
27     /// private visibility
28     /// ```rust
29     /// mod internal {
30     ///     pub fn internal_fn() { }
31     /// }
32     /// ```
33     #[clippy::version = "1.44.0"]
34     pub REDUNDANT_PUB_CRATE,
35     nursery,
36     "Using `pub(crate)` visibility on items that are not crate visible due to the visibility of the module that contains them."
37 }
38
39 #[derive(Default)]
40 pub struct RedundantPubCrate {
41     is_exported: Vec<bool>,
42 }
43
44 impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
45
46 impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
47     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
48         if_chain! {
49             if cx.tcx.visibility(item.def_id) == ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id());
50             if !cx.access_levels.is_exported(item.def_id) && self.is_exported.last() == Some(&false);
51             if is_not_macro_export(item);
52             then {
53                 let span = item.span.with_hi(item.ident.span.hi());
54                 let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id());
55                 span_lint_and_then(
56                     cx,
57                     REDUNDANT_PUB_CRATE,
58                     span,
59                     &format!("pub(crate) {} inside private module", descr),
60                     |diag| {
61                         diag.span_suggestion(
62                             item.vis_span,
63                             "consider using",
64                             "pub".to_string(),
65                             Applicability::MachineApplicable,
66                         );
67                     },
68                 );
69             }
70         }
71
72         if let ItemKind::Mod { .. } = item.kind {
73             self.is_exported.push(cx.access_levels.is_exported(item.def_id));
74         }
75     }
76
77     fn check_item_post(&mut self, _cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
78         if let ItemKind::Mod { .. } = item.kind {
79             self.is_exported.pop().expect("unbalanced check_item/check_item_post");
80         }
81     }
82 }
83
84 fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool {
85     if let ItemKind::Use(path, _) = item.kind {
86         if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res {
87             return false;
88         }
89     } else if let ItemKind::Macro(..) = item.kind {
90         return false;
91     }
92
93     true
94 }