|
| 1 | +use clippy_config::types::PubUnderscoreFieldsBehaviour; |
| 2 | +use clippy_utils::attrs::is_doc_hidden; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 4 | +use clippy_utils::is_path_lang_item; |
| 5 | +use rustc_hir::{FieldDef, Item, ItemKind, LangItem}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::impl_lint_pass; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks whether any field of the struct is prefixed with an `_` (underscore) and also marked |
| 12 | + /// `pub` (public) |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// Fields prefixed with an `_` are inferred as unused, which suggests it should not be marked |
| 16 | + /// as `pub`, because marking it as `pub` infers it will be used. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```rust |
| 20 | + /// struct FileHandle { |
| 21 | + /// pub _descriptor: usize, |
| 22 | + /// } |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```rust |
| 26 | + /// struct FileHandle { |
| 27 | + /// _descriptor: usize, |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + /// |
| 31 | + /// OR |
| 32 | + /// |
| 33 | + /// ```rust |
| 34 | + /// struct FileHandle { |
| 35 | + /// pub descriptor: usize, |
| 36 | + /// } |
| 37 | + /// ``` |
| 38 | + #[clippy::version = "1.77.0"] |
| 39 | + pub PUB_UNDERSCORE_FIELDS, |
| 40 | + pedantic, |
| 41 | + "struct field prefixed with underscore and marked public" |
| 42 | +} |
| 43 | + |
| 44 | +pub struct PubUnderscoreFields { |
| 45 | + pub behavior: PubUnderscoreFieldsBehaviour, |
| 46 | +} |
| 47 | +impl_lint_pass!(PubUnderscoreFields => [PUB_UNDERSCORE_FIELDS]); |
| 48 | + |
| 49 | +impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields { |
| 50 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { |
| 51 | + // This lint only pertains to structs. |
| 52 | + let ItemKind::Struct(variant_data, _) = &item.kind else { |
| 53 | + return; |
| 54 | + }; |
| 55 | + |
| 56 | + let is_visible = |field: &FieldDef<'_>| match self.behavior { |
| 57 | + PubUnderscoreFieldsBehaviour::PublicallyExported => cx.effective_visibilities.is_reachable(field.def_id), |
| 58 | + PubUnderscoreFieldsBehaviour::AllPubFields => { |
| 59 | + // If there is a visibility span then the field is marked pub in some way. |
| 60 | + !field.vis_span.is_empty() |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + for field in variant_data.fields() { |
| 65 | + // Only pertains to fields that start with an underscore, and are public. |
| 66 | + if field.ident.as_str().starts_with('_') && is_visible(field) |
| 67 | + // We ignore fields that have `#[doc(hidden)]`. |
| 68 | + && !is_doc_hidden(cx.tcx.hir().attrs(field.hir_id)) |
| 69 | + // We ignore fields that are `PhantomData`. |
| 70 | + && !is_path_lang_item(cx, field.ty, LangItem::PhantomData) |
| 71 | + { |
| 72 | + span_lint_and_help( |
| 73 | + cx, |
| 74 | + PUB_UNDERSCORE_FIELDS, |
| 75 | + field.vis_span.to(field.ident.span), |
| 76 | + "field marked as public but also inferred as unused because it's prefixed with `_`", |
| 77 | + None, |
| 78 | + "consider removing the underscore, or making the field private", |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments