|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use rustc_ast::ast::{Item, ItemKind, VisibilityKind}; |
| 3 | +use rustc_lint::{EarlyContext, EarlyLintPass}; |
| 4 | +use rustc_session::declare_lint_pass; |
| 5 | + |
| 6 | +declare_clippy_lint! { |
| 7 | + /// ### What it does |
| 8 | + /// Checks for usage of scoped visibility modifiers, like `pub(crate)`, on fields. These |
| 9 | + /// make a field visible within a scope between public and private. |
| 10 | + /// |
| 11 | + /// ### Why restrict this? |
| 12 | + /// Scoped visibility modifiers cause a field to be accessible within some scope between |
| 13 | + /// public and private, potentially within an entire crate. This allows for fields to be |
| 14 | + /// non-private while upholding internal invariants, but can be a code smell. Scoped visibility |
| 15 | + /// requires checking a greater area, potentially an entire crate, to verify that an invariant |
| 16 | + /// is upheld, and global analysis requires a lot of effort. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```no_run |
| 20 | + /// pub mod public_module { |
| 21 | + /// struct MyStruct { |
| 22 | + /// pub(crate) first_field: bool, |
| 23 | + /// pub(super) second_field: bool |
| 24 | + /// } |
| 25 | + /// } |
| 26 | + /// ``` |
| 27 | + /// Use instead: |
| 28 | + /// ```no_run |
| 29 | + /// pub mod public_module { |
| 30 | + /// struct MyStruct { |
| 31 | + /// first_field: bool, |
| 32 | + /// second_field: bool |
| 33 | + /// } |
| 34 | + /// impl MyStruct { |
| 35 | + /// pub(crate) fn get_first_field(&self) -> bool { |
| 36 | + /// self.first_field |
| 37 | + /// } |
| 38 | + /// pub(super) fn get_second_field(&self) -> bool { |
| 39 | + /// self.second_field |
| 40 | + /// } |
| 41 | + /// } |
| 42 | + /// } |
| 43 | + /// ``` |
| 44 | + #[clippy::version = "1.78.0"] |
| 45 | + pub FIELD_SCOPED_VISIBILITY_MODIFIERS, |
| 46 | + restriction, |
| 47 | + "checks for usage of a scoped visibility modifier, like `pub(crate)`, on fields" |
| 48 | +} |
| 49 | + |
| 50 | +declare_lint_pass!(FieldScopedVisibilityModifiers => [FIELD_SCOPED_VISIBILITY_MODIFIERS]); |
| 51 | + |
| 52 | +impl EarlyLintPass for FieldScopedVisibilityModifiers { |
| 53 | + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { |
| 54 | + let ItemKind::Struct(ref st, _) = item.kind else { |
| 55 | + return; |
| 56 | + }; |
| 57 | + for field in st.fields() { |
| 58 | + let VisibilityKind::Restricted { path, .. } = &field.vis.kind else { |
| 59 | + continue; |
| 60 | + }; |
| 61 | + if !path.segments.is_empty() && path.segments[0].ident.name == rustc_span::symbol::kw::SelfLower { |
| 62 | + // pub(self) is equivalent to not using pub at all, so we ignore it |
| 63 | + continue; |
| 64 | + } |
| 65 | + span_lint_and_help( |
| 66 | + cx, |
| 67 | + FIELD_SCOPED_VISIBILITY_MODIFIERS, |
| 68 | + field.vis.span, |
| 69 | + "scoped visibility modifier on a field", |
| 70 | + None, |
| 71 | + "consider making the field private and adding a scoped visibility method for it", |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments