|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt}; |
| 2 | +use rustc_ast::ast::{Item, VisibilityKind}; |
| 3 | +use rustc_errors::Applicability; |
| 4 | +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; |
| 5 | +use rustc_middle::lint::in_external_macro; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::{symbol::kw, Span}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks for usage of `pub(self)` and `pub(in self)`. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// It's unnecessary, omitting the `pub` entirely will give the same results. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// ```rust,ignore |
| 18 | + /// pub(self) type OptBox<T> = Option<Box<T>>; |
| 19 | + /// ``` |
| 20 | + /// Use instead: |
| 21 | + /// ```rust,ignore |
| 22 | + /// type OptBox<T> = Option<Box<T>>; |
| 23 | + /// ``` |
| 24 | + #[clippy::version = "1.72.0"] |
| 25 | + pub NEEDLESS_PUB_SELF, |
| 26 | + style, |
| 27 | + "checks for usage of `pub(self)` and `pub(in self)`." |
| 28 | +} |
| 29 | +declare_clippy_lint! { |
| 30 | + /// ### What it does |
| 31 | + /// Checks for usage of `pub(<loc>)` with `in`. |
| 32 | + /// |
| 33 | + /// ### Why is this bad? |
| 34 | + /// Consistency. Use it or don't, just be consistent about it. |
| 35 | + /// |
| 36 | + /// Also see the `pub_without_shorthand` lint for an alternative. |
| 37 | + /// |
| 38 | + /// ### Example |
| 39 | + /// ```rust,ignore |
| 40 | + /// pub(super) type OptBox<T> = Option<Box<T>>; |
| 41 | + /// ``` |
| 42 | + /// Use instead: |
| 43 | + /// ```rust,ignore |
| 44 | + /// pub(in super) type OptBox<T> = Option<Box<T>>; |
| 45 | + /// ``` |
| 46 | + #[clippy::version = "1.72.0"] |
| 47 | + pub PUB_WITH_SHORTHAND, |
| 48 | + restriction, |
| 49 | + "disallows usage of `pub(<loc>)`, without `in`" |
| 50 | +} |
| 51 | +declare_clippy_lint! { |
| 52 | + /// ### What it does |
| 53 | + /// Checks for usage of `pub(<loc>)` without `in`. |
| 54 | + /// |
| 55 | + /// Note: As you cannot write a module's path in `pub(<loc>)`, this will only trigger on |
| 56 | + /// `pub(super)` and the like. |
| 57 | + /// |
| 58 | + /// ### Why is this bad? |
| 59 | + /// Consistency. Use it or don't, just be consistent about it. |
| 60 | + /// |
| 61 | + /// Also see the `pub_with_shorthand` lint for an alternative. |
| 62 | + /// |
| 63 | + /// ### Example |
| 64 | + /// ```rust,ignore |
| 65 | + /// pub(in super) type OptBox<T> = Option<Box<T>>; |
| 66 | + /// ``` |
| 67 | + /// Use instead: |
| 68 | + /// ```rust,ignore |
| 69 | + /// pub(super) type OptBox<T> = Option<Box<T>>; |
| 70 | + /// ``` |
| 71 | + #[clippy::version = "1.72.0"] |
| 72 | + pub PUB_WITHOUT_SHORTHAND, |
| 73 | + restriction, |
| 74 | + "disallows usage of `pub(in <loc>)` with `in`" |
| 75 | +} |
| 76 | +declare_lint_pass!(Visibility => [NEEDLESS_PUB_SELF, PUB_WITH_SHORTHAND, PUB_WITHOUT_SHORTHAND]); |
| 77 | + |
| 78 | +impl EarlyLintPass for Visibility { |
| 79 | + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { |
| 80 | + if !in_external_macro(cx.sess(), item.span) |
| 81 | + && let VisibilityKind::Restricted { path, shorthand, .. } = &item.vis.kind |
| 82 | + { |
| 83 | + if **path == kw::SelfLower && let Some(false) = is_from_proc_macro(cx, item.vis.span) { |
| 84 | + span_lint_and_sugg( |
| 85 | + cx, |
| 86 | + NEEDLESS_PUB_SELF, |
| 87 | + item.vis.span, |
| 88 | + &format!("unnecessary `pub({}self)`", if *shorthand { "" } else { "in " }), |
| 89 | + "remove it", |
| 90 | + String::new(), |
| 91 | + Applicability::MachineApplicable, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + if (**path == kw::Super || **path == kw::SelfLower || **path == kw::Crate) |
| 96 | + && !*shorthand |
| 97 | + && let [.., last] = &*path.segments |
| 98 | + && let Some(false) = is_from_proc_macro(cx, item.vis.span) |
| 99 | + { |
| 100 | + span_lint_and_sugg( |
| 101 | + cx, |
| 102 | + PUB_WITHOUT_SHORTHAND, |
| 103 | + item.vis.span, |
| 104 | + "usage of `pub` with `in`", |
| 105 | + "remove it", |
| 106 | + format!("pub({})", last.ident), |
| 107 | + Applicability::MachineApplicable, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + if *shorthand |
| 112 | + && let [.., last] = &*path.segments |
| 113 | + && let Some(false) = is_from_proc_macro(cx, item.vis.span) |
| 114 | + { |
| 115 | + span_lint_and_sugg( |
| 116 | + cx, |
| 117 | + PUB_WITH_SHORTHAND, |
| 118 | + item.vis.span, |
| 119 | + "usage of `pub` without `in`", |
| 120 | + "add it", |
| 121 | + format!("pub(in {})", last.ident), |
| 122 | + Applicability::MachineApplicable, |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn is_from_proc_macro(cx: &EarlyContext<'_>, span: Span) -> Option<bool> { |
| 130 | + snippet_opt(cx, span).map(|s| !s.starts_with("pub")) |
| 131 | +} |
0 commit comments