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