`ast::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, `Trait`, `TraitAlias`, `MacroDef`, `Delegation`. - It's always empty for these item kinds: `Use`, `ForeignMod`, `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`. There is a similar story for `AssocItemKind` and `ForeignItemKind`. Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - `ast::Item` got 8 bytes bigger. This could be avoided by boxing the fields within some of the `ast::ItemKind` variants (specifically: `Struct`, `Union`, `Enum`). I might do that in a follow-up; this commit is big enough already. - For the visitors: `FnKind` no longer needs an `ident` field because the `Fn` within how has one. - In the parser, the `ItemInfo` typedef is no longer needed. It was used in various places to return an `Ident` alongside an `ItemKind`, but now the `Ident` (if present) is within the `ItemKind`. - In a few places I renamed identifier variables called `name` (or `foo_name`) as `ident` (or `foo_ident`), to better match the type, and because `name` is normally used for `Symbol`s. It's confusing to see something like `foo_name.name`.
74 lines
2.5 KiB
Rust
74 lines
2.5 KiB
Rust
use clippy_utils::diagnostics::span_lint_and_then;
|
||
use rustc_ast::ast::{Item, ItemKind};
|
||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||
use rustc_session::declare_lint_pass;
|
||
|
||
declare_clippy_lint! {
|
||
/// ### What it does
|
||
/// Checks whether some but not all fields of a `struct` are public.
|
||
///
|
||
/// Either make all fields of a type public, or make none of them public
|
||
///
|
||
/// ### Why restrict this?
|
||
/// Most types should either be:
|
||
/// * Abstract data types: complex objects with opaque implementation which guard
|
||
/// interior invariants and expose intentionally limited API to the outside world.
|
||
/// * Data: relatively simple objects which group a bunch of related attributes together,
|
||
/// but have no invariants.
|
||
///
|
||
/// ### Example
|
||
/// ```no_run
|
||
/// pub struct Color {
|
||
/// pub r: u8,
|
||
/// pub g: u8,
|
||
/// b: u8,
|
||
/// }
|
||
/// ```
|
||
/// Use instead:
|
||
/// ```no_run
|
||
/// pub struct Color {
|
||
/// pub r: u8,
|
||
/// pub g: u8,
|
||
/// pub b: u8,
|
||
/// }
|
||
/// ```
|
||
#[clippy::version = "1.66.0"]
|
||
pub PARTIAL_PUB_FIELDS,
|
||
restriction,
|
||
"partial fields of a struct are public"
|
||
}
|
||
declare_lint_pass!(PartialPubFields => [PARTIAL_PUB_FIELDS]);
|
||
|
||
impl EarlyLintPass for PartialPubFields {
|
||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
||
let ItemKind::Struct(_, ref st, _) = item.kind else {
|
||
return;
|
||
};
|
||
|
||
let mut fields = st.fields().iter();
|
||
let Some(first_field) = fields.next() else {
|
||
// Empty struct.
|
||
return;
|
||
};
|
||
let all_pub = first_field.vis.kind.is_pub();
|
||
let all_priv = !all_pub;
|
||
|
||
let msg = "mixed usage of pub and non-pub fields";
|
||
|
||
for field in fields {
|
||
if all_priv && field.vis.kind.is_pub() {
|
||
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
|
||
span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| {
|
||
diag.help("consider using private field here");
|
||
});
|
||
return;
|
||
} else if all_pub && !field.vis.kind.is_pub() {
|
||
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
|
||
span_lint_and_then(cx, PARTIAL_PUB_FIELDS, field.vis.span, msg, |diag| {
|
||
diag.help("consider using public field here");
|
||
});
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|